mirror of
https://github.com/keymanapp/keyman.git
synced 2026-10-16 10:57:35 +00:00
29 lines
No EOL
960 B
TypeScript
29 lines
No EOL
960 B
TypeScript
/// <reference path="workerFactory.ts" />
|
|
|
|
namespace com.keyman.text.prediction {
|
|
export class WebWorkerFactory implements WorkerFactory {
|
|
constructInstance(): Worker {
|
|
return new Worker(WebWorkerFactory.asBlobURI(LMLayerWorkerCode));
|
|
}
|
|
|
|
/**
|
|
* Converts the INSIDE of a function into a blob URI that can
|
|
* be passed as a valid URI for a Worker.
|
|
* @param fn Function whose body will be referenced by a URI.
|
|
*
|
|
* This function makes the following possible:
|
|
*
|
|
* let worker = new Worker(LMLayer.asBlobURI(function myWorkerCode () {
|
|
* postMessage('inside Web Worker')
|
|
* function onmessage(event) {
|
|
* // handle message inside Web Worker.
|
|
* }
|
|
* }));
|
|
*/
|
|
static asBlobURI(fn: Function): string {
|
|
let code = LMLayer.unwrap(fn);
|
|
let blob = new Blob([code], { type: 'text/javascript' });
|
|
return URL.createObjectURL(blob);
|
|
}
|
|
}
|
|
} |