import { DedicatedWorker } from "../shared/dedicatedWorker" ; import { EXAMPLE_MODULE_URL } from "../shared/constants" ; declare function postMessage ( data ) ; export class ExampleWorker { private _worker : Worker ; private constructor ( ) { } private static onInitialized ( ) { postMessage ( { message : "Got a module!" } ) ; } private static onMessage ( event ) { let data = event . data ; data . wentToWorker = true ; postMessage ( data ) ; } public static CreateAsync ( ) { return new Promise < ExampleWorker > ( ( resolve ) => { let exampleWorker = new ExampleWorker ( ) ; exampleWorker . _worker = DedicatedWorker . createFromLocation ( EXAMPLE_MODULE_URL , ExampleWorker . onInitialized , ExampleWorker . onMessage ) ; exampleWorker . _worker . onmessage = ( event ) => { exampleWorker . _worker . onmessage = DedicatedWorker . unexpectedMessageHandler ; resolve ( exampleWorker ) ; } ; } ) ; } public sendMessageAsync ( data ) { let promise = new Promise < any > ( ( resolve ) => { this . _worker . onmessage = ( event ) => { resolve ( event . data ) ; this . _worker . onmessage = DedicatedWorker . unexpectedMessageHandler ; } ; } ) ; this . _worker . postMessage ( data ) ; return promise ; } }