Class ReadWriteChannel<Read, Write>

ReadWriteChannel is a readable and writable Channel.

Example

An example worker script:

// Setup the channel to be "string" (denoted by the type) with data of type string (denoted by data).
type MyMessage = {type: "string", data: string};
const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();

(async () => {
// read each string from the 'string' channel (as declared by MyMessage).
for await (const item of rwChannel.readAll.string()) {
// write to the 'string' channel.
rwChannel.write.string(worker);
}
})();

Type Parameters

Hierarchy

  • Channel<Read, Write>
    • ReadWriteChannel

Constructors

Properties

Methods

Constructors

Properties

cancel: {
    reader: ChannelRelease<Read>;
} = ...

Propagate cancel on all readers

Example

const rChannel = new ReadChannel();

// cancels the "string" channel.
rChannel.cancel.reader.string();

Type declaration

close: {
    writer: ChannelRelease<Write>;
} = ...

Propagate close on all writers of channel

Example

const writer = new WriteChannel();

// closes the "string" channel.
writer.close.writer.string();

Type declaration

read: ChannelRead<Read> = ...

Read from the channel

Example

// Setup the channel to be "string" (denoted by the type) with data of type string (denoted by data).
type MyMessage = {type: "string", data: string};
const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();

// Read data of type string from the "string" channel.
const str = await rwChannel.read.string();
readAll: ChannelReadAll<Read> = ...

Read from the channel

Example

// Setup the channel to be "string" (denoted by the type) with data of type string (denoted by data).
type MyMessage = {type: "string", data: string};
const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();

for await (const item of rwChannel.readAll.string()) {
console.log(item);
}
write: ChannelWrite<Write> = ...

Write to the channel.

Example

// Setup the channel to be "string" (denoted by the type) with data of type string (denoted by data).
type MyMessage = {type: "string", data: string};
const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();

// writes to the string channel:
rwChannel.write.string("foo");

Methods

  • Connect the readFrom or writeTo worker/port to either recieve from or send to to a different message port.

    Parameters

    • target: "writeTo" | "readFrom"

      The worker/port to send the connection change to.

    • action: "change-reader" | "change-writer"

      Whether you want to change the "writable" end or the "readable" end of the target

    • connection: MessagePort

      The connection that should now be written to or read from.

    Returns void

  • Ends the communication of the channel. You can always restart the channel with .start().

    Returns void

  • Starts the channel. Called automatically from the constructor, but if you ever .end() the channel this will start it again.

    Returns void

Generated using TypeDoc