UNPKG

@thi.ng/rstream-csp

Version:

@thi.ng/csp bridge module for @thi.ng/rstream

28 lines (27 loc) 662 B
import { LOGGER } from "@thi.ng/rstream/logger"; import { Stream } from "@thi.ng/rstream/stream"; const fromChannel = (src, opts) => { opts = { id: `channel-${src.id}`, closeChannel: true, ...opts }; return new Stream((stream) => { let isActive = true; (async () => { let x; while ((x = await src.read()) !== void 0) { if (!isActive) break; stream.next(x); x = null; } stream.done(); })(); return () => { if (opts.closeChannel !== false) { src.close(); LOGGER.info("closed channel", src.id); } isActive = false; }; }, opts); }; export { fromChannel };