mirror of
https://github.com/keymanapp/keyman.git
synced 2026-09-25 17:17:43 +00:00
chore(common/lmlayer): Merge branch 'refactor/common/lmlayer/headless-variant' into feat/common/lmlayer/headless-testing
This commit is contained in:
commit
4832ff43e7
2 changed files with 17 additions and 10 deletions
|
|
@ -23,7 +23,7 @@
|
|||
/// <reference path="node_modules/es6-shim/es6-shim.min.js" />
|
||||
/// <reference path="promise-store.ts" />
|
||||
/// <reference path="lmlayer-interface.ts" />
|
||||
/// <reference path="headless-workerFactory.ts" />
|
||||
/// <reference path="virtualized-workerFactory.ts" />
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue