diff --git a/common/predictive-text/index.ts b/common/predictive-text/index.ts index 7a83cc91ce..bdcf5a8f34 100644 --- a/common/predictive-text/index.ts +++ b/common/predictive-text/index.ts @@ -23,7 +23,7 @@ /// /// /// -/// +/// namespace com.keyman.text.prediction { /** @@ -54,7 +54,7 @@ namespace com.keyman.text.prediction { * or file: URI. If uri is not provided, this will start the default Worker. */ constructor(capabilities: Capabilities) { - super(capabilities, new HeadlessWorkerFactory()); + super(capabilities, new VirtualizedWorkerFactory()); } } } diff --git a/common/predictive-text/headless-workerFactory.ts b/common/predictive-text/virtualized-workerFactory.ts similarity index 73% rename from common/predictive-text/headless-workerFactory.ts rename to common/predictive-text/virtualized-workerFactory.ts index f7752ff6e5..3c0fd1e72e 100644 --- a/common/predictive-text/headless-workerFactory.ts +++ b/common/predictive-text/virtualized-workerFactory.ts @@ -9,7 +9,7 @@ namespace com.keyman.text.prediction { var fs = require("fs"); var vm = require("vm"); - class HeadlessWorkerContext { + class VirtualizedWorkerContext { // The LMLayerWorker assigns itself to 'window', so we provide an alias. window: any; @@ -36,14 +36,20 @@ namespace com.keyman.text.prediction { } } - export class HeadlessWorker implements Worker { + /** + * Note: this does not create an actual Worker, separate process, or thread. Everything will + * be executed in-line on a virtualized context. + * + * In the future, it might be nice to use Node's Worker Threads implementation. + */ + export class VirtualizedWorker implements Worker { private _worker: any; // Really, the LMLayerWorker... but some of its types are problematic for headless. - private _workerContext: HeadlessWorkerContext; + private _workerContext: VirtualizedWorkerContext; constructor() { - this._workerContext = new HeadlessWorkerContext(); + this._workerContext = new VirtualizedWorkerContext(); // Needs to exist before setting up the worker; must exist by `.install()`. - this._workerContext.postMessage = this.transferMessage.bind(this); + this._workerContext.postMessage = this.workerPostMessage.bind(this); // Initialize the "worker". let script = LMLayerBase.unwrap(LMLayerWorkerCode); @@ -54,7 +60,8 @@ namespace com.keyman.text.prediction { this._worker = this._workerContext['LMLayerWorker']; } - private transferMessage(message: any, options: any) { + // Sends the worker's postMessage messages to the appropriate `onmessage` handler. + private workerPostMessage(message: any, options: any) { if(this.onmessage) { this.onmessage({data: message} as any as MessageEvent); } @@ -73,9 +80,9 @@ namespace com.keyman.text.prediction { } } - export class HeadlessWorkerFactory implements com.keyman.text.prediction.WorkerFactory { + export class VirtualizedWorkerFactory implements com.keyman.text.prediction.WorkerFactory { constructInstance(): Worker { - return new HeadlessWorker(); + return new VirtualizedWorker(); } } } \ No newline at end of file