From 0892ebe9df40c6a2e08a2509556cbc36333100dd Mon Sep 17 00:00:00 2001 From: jahorton Date: Thu, 16 Apr 2020 11:04:10 +0700 Subject: [PATCH] fix(common/lmlayer): virtualized postMessage --- common/predictive-text/virtualized-workerFactory.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/common/predictive-text/virtualized-workerFactory.ts b/common/predictive-text/virtualized-workerFactory.ts index 3c0fd1e72e..8cfdce76f4 100644 --- a/common/predictive-text/virtualized-workerFactory.ts +++ b/common/predictive-text/virtualized-workerFactory.ts @@ -72,7 +72,18 @@ namespace com.keyman.text.prediction { postMessage(message: any, transfer: any[]): void; postMessage(message: any, options?: PostMessageOptions): void; postMessage(message: any, options?: any) { - this._worker.onMessage({data: message} as any as MessageEvent); + let msgObj = {data: message}; + let msgJSON = JSON.stringify(msgObj); + + /* + * Execute the command within the virtualized worker's scope. The worker's returned + * `postMessage` calls will still reach outside, as they have a reference to `this` via + * `postMessage` (which we've set to a bound `this.workerPostMessage`). + * + * Among other things, this will allow the worker to use its internal namespaces without issue. + */ + let msgCommand = "onmessage(" + msgJSON + ")"; + this._workerContext.__importScriptString(msgCommand); } terminate(): void {