import { Observable , Subject , fromEvent , map , share , switchMapTo , tap , throttle } from "rxjs" ; /**
 * Worker message
 */ export interface WorkerMessage { type : unknown ; data ? : unknown ; } template T export interface WorkerHandler < T extends WorkerMessage > { tx$ : Subject < T > ; rx$ : Observable < T > ; } template T interface WatchOptions < T extends WorkerMessage > { tx$ : Observable < T > ; } param worker param options returns export function watchWorker < T extends WorkerMessage > ( worker , { tx$ } ) { const rx$ = fromEvent < MessageEvent > ( worker , "message" ) . pipe ( map ( ( { data } ) => data as T ) ) ; return tx$ . pipe ( throttle ( ( ) => rx$ , { leading : true , trailing : true } ) , tap ( message => worker . postMessage ( message ) ) , switchMapTo ( rx$ ) , share ( ) ) ; }