export class DedicatedWorker { private static createFromSources ( ... sources ) { let workerCode = "" ; for ( let idx = 0 ; idx < sources . length - 1 ; idx ++ ) { workerCode += sources [ idx ] . toString ( ) ; } workerCode += "(" + sources [ sources . length - 1 ] . toString ( ) + ")();" ; return new Worker ( window . URL . createObjectURL ( new Blob ( [ workerCode ] ) ) ) ; } public static createFromLocation ( jsUrl , onInitialized , onMessage ) { let moduleDefition = `Module = {
            locateFile: function (path) {
                return \"` + jsUrl . replace ( /.js$/ , ".wasm" ) + `\";
            },
            onRuntimeInitialized: function () {
                (` + onInitialized . toString ( ) + `)();
            }
        };` ; let messageHandler = "this.onmessage = " + onMessage . toString ( ) + ";" ; let importJavascript = "function importJavascript() { importScripts(\"" + jsUrl + "\"); }" ; return this . createFromSources ( moduleDefition , messageHandler , importJavascript ) ; } public static unexpectedMessageHandler ( event ) { throw Error ( "Unexpected message from WebWorker: " + event ) ; } }