Class ReadChannel<Read>

Creates a readable 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 MessageType = {type: "string", data: string};
const rChannel = new ReadChannel<MessageType>();

// Read a single item from "string" channel.
console.log(await rChannel.read.string());
// Read everything from the "string" channel.
for await (const item of rChannel.readAll.string()) {
console.log(item);
}

Type Parameters

Hierarchy

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<DataMessage<void>>;
} = ...

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 a 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 MessageType = {type: "string", data: string};
const rChannel = new ReadChannel<MessageType>();

console.log(await rChannel.read.string())
readAll: ChannelReadAll<Read> = ...

Read everything from a 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 MessageType = {type: "string", data: string};
const rChannel = new ReadChannel<MessageType>();

// Read everything from the "string" channel.
for await (const item of rChannel.readAll.string()) {
console.log(item);
}

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