UNPKG

@thi.ng/rstream

Version:

Reactive streams & subscription primitives for constructing dataflow graphs / pipelines

35 lines (34 loc) 859 B
import { isTransferable } from "@thi.ng/checks/is-transferable"; import { isTypedArray } from "@thi.ng/checks/is-typedarray"; import { LOGGER } from "./logger.js"; import { defWorker } from "./defworker.js"; const postWorker = (worker, transfer = false, terminate = 0) => { const _worker = defWorker(worker); return { next(x) { if (x instanceof Promise) { x.then((y) => this.next(y)); return; } let tx; if (transfer) { const ta = isTypedArray(x); if (ta || isTransferable(x)) { tx = [ta ? x.buffer : x]; } } _worker.postMessage(x, tx || []); }, done() { if (terminate > 0) { setTimeout(() => { LOGGER.info("terminating worker..."); _worker.terminate(); }, terminate); } } }; }; export { postWorker };