UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

1 lines 721 B
import{randomUUID as v4}from"crypto";import{WritableToPort}from"./writable_to_port.js";export function prepareWritableToPortStream(e,t,r=15e3){const o=v4();return new Promise(((a,s)=>{const i=setTimeout(s.bind(void 0,new Error(`Stream creation failed after ${r/1e3}s. Receiver failed to notify of its ready status. If you are using the Messenger API, make sure you are using the ".onStream()" listener on one of the receiving ends and that at least one receiver is accepting the stream with the "accept()" function.`)),r),n=r=>{"stream-ready-to-consume"===r.type&&r.id===o&&(clearTimeout(i),a(new WritableToPort(e,o,t)),e.off("message",n))};e.on("message",n);const m={type:"stream-start",id:o,meta:t};e.postMessage(m)}))}