import { BasePostMessageStream , PostMessageEvent , StreamData , } from './BasePostMessageStream' ; import { DEDICATED_WORKER_NAME } from './enums' ; interface WorkerParentStreamArgs { worker : Worker ; } /**
 * Parent-side dedicated web worker `postMessage` stream.
 */ export class WorkerParentPostMessageStream extends BasePostMessageStream { private _target : string ; private _worker : Worker ; param args . worker constructor ( { worker } ) { if ( ! worker ) { throw new Error ( 'Invalid input.' ) ; } super ( ) ; this . _target = DEDICATED_WORKER_NAME ; this . _worker = worker ; this . _worker . onmessage = this . _onMessage . bind ( this ) as any ; this . _handshake ( ) ; } protected _postMessage ( data ) { this . _worker . postMessage ( { target : this . _target , data , } ) ; } private _onMessage ( event ) { const message = event . data ; if ( typeof message !== 'object' || ! message . data ) { return ; } this . _onData ( message . data as StreamData ) ; } _destroy ( ) { this . _worker . onmessage = null ; this . _worker = null as any ; } }