-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapiDemoInteractiveWorker.js
68 lines (54 loc) · 1.91 KB
/
apiDemoInteractiveWorker.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
importScripts('Util.js');
importScripts('Validator.js');
var abort = false, running = false;
var fixedMap = null, validator = null;
onmessage = function (msg) {
switch (msg.data.request) {
case "create":
fixedMap = msg.data.fixedMap;
validator = Validator.create(msg.data.fixedMap);
postMessage({ response: "created" });
break;
case "validate":
var currentEntry = 0, options = msg.data.options || {};
var results = Util.createResults();
running = true;
setTimeout(validateSingleEntry, 0);
function validateSingleEntry () {
if (abort) {
// Emergency Stop button was pressed.
console.log(`aborted at entry ${currentEntry}`);
abort = running = false;
postMessage({ response: "aborted", stoppedAt: currentEntry });
} else if (currentEntry === fixedMap.length) {
// Done -- show results and restore interface.
running = false;
if (options.includeDoneResults)
postMessage({ response: "done", results: results.getShapeMap() });
else
postMessage({ response: "done" });
} else {
var queryMap = [fixedMap[currentEntry++]]; // ShapeMap with single entry.
var newResults = validator.validate(queryMap, results.getShapeMap());
// Merge into results.
results.merge(newResults);
// Notify caller.
postMessage({ response: "update", results: newResults });
// Skip entries that were already processed.
while (currentEntry < fixedMap.length &&
results.has(fixedMap[currentEntry]))
++currentEntry;
// Call this function again after yielding.
setTimeout(validateSingleEntry, 0);
}
}
break;
case "abort": // kills validator
validator = null;
abort = true;
// postMessage({ response: "aborted" });
break;
default:
throw "unknown request: " + JSON.stringify(msg.data);
}
}