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,QACzD,IAAI,EACJ,YAAY,IAAI,CAAC,CAClB;IACC,KAAK,IAAI,IAAI;IAKb;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,IAAI,EAAE,YAAY,IAAI,CAAC,CAI7B;IAEH;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,OAAO,EAAE,eAAe,IAAI,CAAC,CAOpC;CACH;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,8BACE,IAAI,SAAS,WAAW,EACxB,KAAK,SAAS,WAAW,CACzB,SAAQ,QAAQ,IAAI,EAAE,KAAK,CAAC;IAC5B,KAAK,IAAI,IAAI;IAKb;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,KAAK,EAAE,aAAa,KAAK,CAAC,CAIhC;IAEH;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,IAAI,EAAE,YAAY,IAAI,CAAC,CAI7B;IAEH;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,OAAO,EAAE,eAAe,IAAI,CAAC,CAOpC;CACH","sources":["src/src/types.ts","src/src/utils.ts","src/src/main.ts","src/main.ts"],"sourcesContent":[null,null,null,"/** @public @module main*/\nimport type {\n DataMessage,\n InternalReadQueue,\n InternalWriteQueue,\n InternalReadWriteQueue,\n NecessaryMessages,\n IChannelOptions,\n ChannelRelease,\n ChannelWrite,\n ChannelRead,\n ChannelReadAll,\n} from \"./types\";\nimport { consumeReader, writerIsClosed } from \"./utils\";\n\nexport abstract class Channel<\n Read extends DataMessage,\n Write extends DataMessage\n> {\n /** @internal */\n protected internalQueues!: {\n read?: InternalReadQueue<Read>;\n write?: InternalWriteQueue<Write>;\n transform?: InternalReadWriteQueue<Read, Write>;\n };\n\n /** Connect the readFrom or writeTo worker/port to either recieve from or send to to a different message port.\n * @param target The worker/port to send the `connection` change to.\n * @param action Whether you want to change the \"writable\" end or the \"readable\" end of the `target`\n * @param connection The connection that should now be written to or read from.\n */\n connect(\n target: \"readFrom\" | \"writeTo\",\n action: \"change-reader\" | \"change-writer\",\n connection: MessagePort\n ) {\n this._sendInternal(target, `worker-channel:${action}`, connection, [\n connection,\n ]);\n }\n\n /** @internal */\n private _sendInternal<T extends NecessaryMessages<Read, Write>[\"type\"]>(\n target: \"readFrom\" | \"writeTo\",\n type: T,\n data: (NecessaryMessages<Read, Write> & { type: T })[\"data\"],\n transfers?: Transferable[]\n ) {\n this[target]?.postMessage?.(\n {\n type,\n data,\n } as NecessaryMessages<Read, Write>,\n transfers ?? []\n );\n }\n\n constructor({\n controller = new AbortController(),\n writeTo = globalThis,\n readFrom = globalThis,\n }: IChannelOptions = {}) {\n this.controller = controller;\n // this.writeCommands = writeCommands;\n // this.readCommands = readCommands;\n this.writeTo = writeTo;\n this.readFrom = readFrom;\n this.listener = this.listener.bind(this);\n this.internalQueues = {};\n this.start();\n }\n /** @internal */\n protected readonly controller?: IChannelOptions[\"controller\"];\n /** @internal */\n protected writeTo: IChannelOptions[\"writeTo\"];\n /** @internal */\n protected readFrom: IChannelOptions[\"readFrom\"];\n\n /** @internal */\n private transforms?: Map<\n keyof InternalReadWriteQueue<Read, Write>,\n InternalReadWriteQueue<Read, Write>[keyof InternalReadWriteQueue<\n Read,\n Write\n >]\n >;\n\n /** @internal */\n private readables?: Map<\n keyof InternalReadQueue<Read>,\n InternalReadQueue<Read>[keyof InternalReadQueue<Read>]\n >;\n\n /** @internal */\n private writables?: Map<\n keyof InternalWriteQueue<Write>,\n InternalWriteQueue<Write>[keyof InternalWriteQueue<Write>]\n >;\n\n /** @internal */\n protected readWriteSetup() {\n this.transforms = new Map();\n this.internalQueues.transform = new Proxy(\n {} as InternalReadWriteQueue<Read, Write>,\n {\n get: (_obj, command) => {\n if (this.transforms?.has(command) ?? false) {\n const stream = this.transforms?.get(command);\n if (stream) return stream;\n }\n const transformStream = new TransformStream();\n this.transforms?.set(command, transformStream);\n return transformStream;\n },\n }\n );\n this.readSetup();\n this.writeSetup();\n }\n\n /** @internal */\n protected readSetup() {\n this.readables = new Map();\n this.internalQueues.read = new Proxy({} as InternalReadQueue<Read>, {\n get: (_obj, command) => {\n if (this.readables?.has(command) ?? false) {\n const stream = this.readables?.get(command);\n if (stream) return stream;\n }\n const readableStream = (\n this.internalQueues?.transform?.[\n command as keyof InternalReadWriteQueue<Read, Write>\n ]?.readable ?? new ReadableStream()\n ).getReader();\n this.readables?.set(command, readableStream);\n return readableStream;\n },\n });\n }\n\n /** @internal */\n protected writeSetup() {\n this.writables = new Map();\n this.internalQueues.write = new Proxy({} as InternalWriteQueue<Write>, {\n get: (_obj, command) => {\n if (this.writables?.has(command) ?? false) {\n const stream = this.writables?.get(command);\n if (stream) return stream;\n }\n const stream = (\n this.internalQueues?.transform?.[\n command as keyof InternalReadWriteQueue<Read, Write>\n ]?.writable ?? new WritableStream()\n ).getWriter();\n this.writables?.set(command, stream);\n return stream;\n },\n });\n }\n\n /** Propagate close on all writers of channel\n *\n * @example\n *\n * ```ts\n * const writer = new WriteChannel();\n *\n * // closes the \"string\" channel.\n * writer.close.writer.string();\n * ```\n */\n close = {\n writer: new Proxy({} as ChannelRelease<Write>, {\n get: (_target, command) => {\n return () => {\n (async () => {\n const writer = this.writables?.get(command);\n this._sendInternal(\n \"writeTo\",\n \"worker-channel:close-writer\",\n command\n );\n if (this.writeTo instanceof MessagePort) this.writeTo.close();\n if (writer && (await writerIsClosed(writer))) return;\n this.writables?.get(command)?.close();\n })();\n };\n },\n }),\n };\n\n /** Propagate cancel on all readers\n *\n * @example\n *\n * ```ts\n * const rChannel = new ReadChannel();\n *\n * // cancels the \"string\" channel.\n * rChannel.cancel.reader.string();\n * ```\n */\n cancel = {\n reader: new Proxy({} as ChannelRelease<Read>, {\n get: (_target, command) => {\n return () => {\n this.readables?.get(command)?.releaseLock();\n this._sendInternal(\n \"readFrom\",\n \"worker-channel:close-reader\",\n command\n );\n if (this.readFrom instanceof MessagePort) this.readFrom.close();\n };\n },\n }),\n };\n\n /** @internal */\n protected _send<T extends Write[\"type\"]>(\n type: T,\n data: (Write & { type: T })[\"data\"],\n transfer?: Transferable[]\n ) {\n return this.writeTo?.postMessage({ type, data }, transfer ?? []);\n }\n\n /** @internal */\n protected async _read<T extends Read[\"type\"]>(\n type: T\n ): Promise<Read[\"data\"] | undefined> {\n return (await this.internalQueues.read?.[type]?.read())?.value;\n }\n\n /** @internal */\n protected async *_readAll<T extends Read[\"type\"]>(\n type: T\n ): AsyncGenerator<(Read & { type: T })[\"data\"]> {\n const result = this.internalQueues.read?.[type];\n if (!result) return;\n yield* consumeReader(result);\n }\n\n /** @internal */\n protected async listen() {\n this.readFrom?.addEventListener?.(\n \"message\",\n this.listener as unknown as EventListener\n );\n }\n\n /** @internal */\n protected async unlisten() {\n this.readFrom?.removeEventListener?.(\n \"message\",\n this.listener as unknown as EventListener\n );\n }\n\n /** @internal */\n protected async listener(ev: MessageEvent<NecessaryMessages<Read, Write>>) {\n if (ev.data.type === \"worker-channel:change-reader\") {\n // sending inner listening channel\n this.unlisten();\n this.readFrom = ev.data.data;\n ev.data.data.start();\n this.listen();\n return;\n } else if (ev.data.type === \"worker-channel:change-writer\") {\n // Sending inner posting channel.\n // this._send(\"acknowledge\", true);\n this.writeTo = ev.data.data;\n return;\n } else if (ev.data.type === \"worker-channel:close-writer\") {\n this.close.writer[ev.data.data]();\n } else if (ev.data.type === \"worker-channel:close-reader\") {\n this.cancel.reader[ev.data.data]();\n } else {\n // send data accordingi to type.\n this.internalQueues.write?.[\n (ev.data as Write).type as Write[\"type\"]\n ]?.write((ev.data as Write).data);\n }\n }\n\n /** Starts the channel. Called automatically from the constructor,\n * but if you ever `.end()` the channel this will start it again.\n */\n start() {\n this.controller?.signal.addEventListener(\n \"abort\",\n () => {\n return this.end();\n },\n {\n once: true,\n }\n );\n }\n\n /** Ends the communication of the channel.\n * You can always restart the channel with `.start()`.\n */\n end() {\n this.readFrom?.removeEventListener(\n \"message\",\n this.listener as unknown as EventListener\n );\n this.readables?.clear();\n this.writables?.clear();\n this.transforms?.clear();\n }\n}\n\n/** Allows writing to a worker.\n *\n *\n * @example\n * An example worker script:\n *\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MyMessage = {type: \"string\", data: string};\n * const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();\n *\n * // writes to the string channel:\n * rwChannel.write.string(\"foo\");\n * ```\n */\nexport class WriteChannel<Write extends DataMessage> extends Channel<\n DataMessage<void>,\n Write\n> {\n start(): void {\n super.start();\n this.writeSetup();\n }\n /** Write to the channel.\n *\n * @example\n * An example worker script:\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MyMessage = {type: \"string\", data: string};\n * const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();\n *\n * // writes to the string channel:\n * rwChannel.write.string(\"foo\");\n * ```\n */\n readonly write: ChannelWrite<Write> = new Proxy({} as ChannelWrite<Write>, {\n get: (_target, command): ChannelWrite<Write>[keyof ChannelWrite<Write>] => {\n return (data, transfer) => this._send(command, data, transfer);\n },\n });\n}\n\n/** Creates a readable channel.\n *\n * @example\n * An example worker script:\n *\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MessageType = {type: \"string\", data: string};\n * const rChannel = new ReadChannel<MessageType>();\n *\n * // Read a single item from \"string\" channel.\n * console.log(await rChannel.read.string());\n * // Read everything from the \"string\" channel.\n * for await (const item of rChannel.readAll.string()) {\n * console.log(item);\n * }\n * ```\n * @public\n */\nexport class ReadChannel<Read extends DataMessage> extends Channel<\n Read,\n DataMessage<void>\n> {\n start(): void {\n super.start();\n this.listen();\n this.readWriteSetup();\n }\n /** Read from a channel.\n *\n * @example\n * An example worker script:\n *\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MessageType = {type: \"string\", data: string};\n * const rChannel = new ReadChannel<MessageType>();\n *\n * console.log(await rChannel.read.string())\n * ```\n */\n readonly read: ChannelRead<Read> = new Proxy({} as ChannelRead<Read>, {\n get: (_target, command) => {\n return () => this._read(command);\n },\n });\n\n /** Read everything from a channel.\n *\n * @example\n * An example worker script:\n *\n * ```ts\n *\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MessageType = {type: \"string\", data: string};\n * const rChannel = new ReadChannel<MessageType>();\n *\n * // Read everything from the \"string\" channel.\n * for await (const item of rChannel.readAll.string()) {\n * console.log(item);\n * }\n * ```\n */\n readonly readAll: ChannelReadAll<Read> = new Proxy(\n {} as ChannelReadAll<Read>,\n {\n get: (_target, command: Read[\"type\"]) => {\n return () => this._readAll(command);\n },\n }\n );\n}\n\n/** ReadWriteChannel is a readable and writable Channel.\n * @example\n *\n * An example worker script:\n *\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MyMessage = {type: \"string\", data: string};\n * const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();\n *\n * (async () => {\n * // read each string from the 'string' channel (as declared by MyMessage).\n * for await (const item of rwChannel.readAll.string()) {\n * // write to the 'string' channel.\n * rwChannel.write.string(worker);\n * }\n * })();\n * ```\n */\nexport class ReadWriteChannel<\n Read extends DataMessage,\n Write extends DataMessage\n> extends Channel<Read, Write> {\n start(): void {\n super.start();\n this.listen();\n this.readWriteSetup();\n }\n /** Write to the channel.\n *\n * @example\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MyMessage = {type: \"string\", data: string};\n * const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();\n *\n * // writes to the string channel:\n * rwChannel.write.string(\"foo\");\n * ```\n */\n readonly write: ChannelWrite<Write> = new Proxy({} as ChannelWrite<Write>, {\n get: (_target, command): ChannelWrite<Write>[keyof ChannelWrite<Write>] => {\n return (data, transfer) => this._send(command, data, transfer);\n },\n });\n\n /** Read from the channel\n *\n * @example\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MyMessage = {type: \"string\", data: string};\n * const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();\n *\n * // Read data of type string from the \"string\" channel.\n * const str = await rwChannel.read.string();\n * ```\n */\n readonly read: ChannelRead<Read> = new Proxy({} as ChannelRead<Read>, {\n get: (_target, command) => {\n return () => this._read(command);\n },\n });\n\n /** Read from the channel\n *\n * @example\n * ```ts\n * // Setup the channel to be \"string\" (denoted by the type) with data of type string (denoted by data).\n * type MyMessage = {type: \"string\", data: string};\n * const rwChannel = new ReadWriteChannel<MyMessage, MyMessage>();\n *\n * for await (const item of rwChannel.readAll.string()) {\n * console.log(item);\n * }\n * ```\n */\n readonly readAll: ChannelReadAll<Read> = new Proxy(\n {} as ChannelReadAll<Read>,\n {\n get: (_target, command) => {\n return () => this._readAll(command);\n },\n }\n );\n}\n\nexport * from \"./utils\";\nexport * from \"./types\";\n"],"names":[],"version":3,"file":"index.d.ts.map"}