Skip to content

Commit 70af7a1

Browse files
committed
Only load the RegExWorker once.
1 parent 4a8876d commit 70af7a1

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

assets/workers/RegExWorker.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,5 @@ onmessage = function (evt) {
2525
}
2626
}
2727
// end share
28-
2928
postMessage({error: error, matches: matches, mode: mode});
30-
self.close();
3129
};

dev/src/helpers/BrowserSolver.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ export default class BrowserSolver {
3333
}
3434

3535
if (window.Worker) {
36-
if (this._worker) {
37-
clearTimeout(this._timeoutId);
38-
this._worker.terminate();
36+
if (!this._worker) {
37+
this._worker = new Worker("assets/workers/RegExWorker.js");
3938
}
40-
41-
let worker = this._worker = new Worker("assets/workers/RegExWorker.js");
42-
43-
worker.onmessage = (evt) => {
39+
40+
this._worker.onmessage = (evt) => {
4441
if (evt.data === "onload") {
4542
this._startTime = Utils.now();
46-
this._timeoutId = setTimeout(()=> {
43+
this._timeoutId = setTimeout(() => {
44+
this._worker.terminate();
45+
this._worker = null;
4746
this._onRegExComplete({id: "timeout"}); // TODO: make this a warning, and return all results so far.
48-
worker.terminate();
4947
}, 250);
5048
} else {
5149
clearTimeout(this._timeoutId);
5250
this._onRegExComplete(evt.data.error, evt.data.matches, evt.data.mode);
5351
}
5452
};
55-
53+
54+
clearTimeout(this._timeoutId);
55+
5656
// we need to pass the pattern and flags as text, because Safari strips the unicode flag when passing a RegExp to a Worker
57-
worker.postMessage({pattern:o.pattern, flags:o.flags, text, tests, mode});
57+
this._worker.postMessage({pattern:o.pattern, flags:o.flags, text, tests, mode});
5858
} else {
5959
this._startTime = Utils.now();
6060

0 commit comments

Comments
 (0)