-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_wrapper.js
52 lines (42 loc) · 1.29 KB
/
worker_wrapper.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
const makeWorkerWrapperCtx = imports => {
const makePromise = (worker, args) => {
return new window.Promise((resolve, reject) => {
worker.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e.data);
worker.postMessage(args);
});
}
const parser = () => {
const worker = new window.Worker("./parser_worker.js");
return {
query(parserName, tokens) {
console.assert(typeof parserName === "string");
console.assert(Array.isArray(tokens), "parser input is string");
return makePromise(worker, [parserName, tokens]);
}
};
};
const dictionary = () => {
const worker = new window.Worker("./dictionary_worker.js");
return {
query(dictionaryName, requestType, str, numToReturn) {
console.assert(typeof dictionaryName === "string");
console.assert(typeof requestType === "string");
console.assert(typeof str === "string");
if (requestType === "lookup") {
console.assert(typeof numToReturn === "undefined");
} else if (requestType == "nearest") {
console.assert(typeof numToReturn === "number");
} else {
throw new Error("request type not recognized: " + requestType);
}
return makePromise(worker, [dictionaryName, requestType, str, numToReturn]);
}
};
};
return {
parser,
dictionary
};
};