-
Notifications
You must be signed in to change notification settings - Fork 3
/
browser-worker-thread-factory.ts
27 lines (23 loc) · 1.21 KB
/
browser-worker-thread-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import {IWorkerThreadFactory} from "../../common/worker/worker-thread-factory";
import {IWorkerThread} from "../../common/worker/worker-thread";
import {DynamicFunctionRegistry} from "../../common/function/dynamic-function-registry";
import {WebWorkerThreadSlaveCommunicationChannel} from "./web-worker-thread-slave-communication-channel";
import {DefaultWorkerThread} from "../../common/worker/default-worker-thread";
declare function require(module: string): any;
/* tslint:disable:no-var-requires */
const SlaveWorker = require("worker-loader?name=worker-slave!../worker-slave");
/**
* Thread factory that creates web worker based threads using {@link BrowserWorkerThread}.
*/
export class BrowserWorkerThreadFactory implements IWorkerThreadFactory {
constructor(private functionLookupTable: DynamicFunctionRegistry) {}
public spawn(): IWorkerThread {
if (!(window as any)["Worker"]) {
throw new Error("Missing Web Worker support");
}
const webWorker = new SlaveWorker();
const workerThread = new DefaultWorkerThread(this.functionLookupTable, new WebWorkerThreadSlaveCommunicationChannel(webWorker));
workerThread.initialize();
return workerThread;
}
}