Skip to content

Commit 05f7b81

Browse files
committed
Inline the RegExWorker in index.html
1 parent 192ab15 commit 05f7b81

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

assets/workers/RegExWorker.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
/**
2+
* This file is now added to the index.html file
3+
* in .regexWorker
4+
*
5+
* Its currently manually minified. But that should move to the build process.
6+
*/
17

28
// in plain JS for now:
39
onmessage = function (evt) {
410
postMessage("onload");
511
var data = evt.data, text = data.text, tests = data.tests, mode = data.mode;
612
var regex = new RegExp(data.pattern, data.flags);
7-
13+
814
// shared between BrowserSolver & RegExWorker
915
var matches = [], match, index, error;
1016
if (mode === "tests") {

dev/src/helpers/BrowserSolver.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,47 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
import Utils from "../utils/Utils";
2020

2121
export default class BrowserSolver {
22-
23-
22+
23+
constructor() {
24+
const workerBlob = new Blob([
25+
document.querySelector('#regexWorker').textContent
26+
], { type: "text/javascript" });
27+
this._workerObjectURL = URL.createObjectURL(workerBlob);
28+
}
29+
2430
solve(o, callback) {
2531
this._callback = callback;
2632
this._req = o;
27-
33+
2834
let regex, text=o.text, tests=o.tests, mode = o.mode;
2935
try {
3036
this._regex = regex = new RegExp(o.pattern, o.flags);
3137
} catch(e) {
3238
return this._onRegExComplete({id:"regexparse", name: e.name, message: e.message}, null, mode);
3339
}
34-
40+
3541
if (window.Worker) {
36-
if (!this._worker) {
37-
this._worker = new Worker("assets/workers/RegExWorker.js");
38-
}
42+
const worker = new Worker(this._workerObjectURL);
3943

40-
this._worker.onmessage = (evt) => {
44+
worker.onmessage = (evt) => {
4145
if (evt.data === "onload") {
4246
this._startTime = Utils.now();
4347
this._timeoutId = setTimeout(() => {
44-
this._worker.terminate();
45-
this._worker = null;
46-
this._onRegExComplete({id: "timeout"}); // TODO: make this a warning, and return all results so far.
48+
worker.terminate();
49+
this._onRegExComplete({id: "timeout"}, null, mode); // TODO: make this a warning, and return all results so far.
4750
}, 250);
4851
} else {
4952
clearTimeout(this._timeoutId);
53+
worker.terminate();
5054
this._onRegExComplete(evt.data.error, evt.data.matches, evt.data.mode);
5155
}
5256
};
5357

54-
clearTimeout(this._timeoutId);
55-
5658
// we need to pass the pattern and flags as text, because Safari strips the unicode flag when passing a RegExp to a Worker
57-
this._worker.postMessage({pattern:o.pattern, flags:o.flags, text, tests, mode});
59+
worker.postMessage({pattern:o.pattern, flags:o.flags, text, tests, mode});
5860
} else {
5961
this._startTime = Utils.now();
60-
62+
6163
// shared between BrowserSolver & RegExWorker
6264
var matches = [], match, index, error;
6365
if (mode === "tests") {
@@ -78,19 +80,19 @@ export default class BrowserSolver {
7880
}
7981
}
8082
// end share
81-
83+
8284
this._onRegExComplete(error, matches, mode);
8385
}
8486
}
85-
87+
8688
_onRegExComplete(error, matches, mode) {
8789
let result = {
8890
time: error ? null : Utils.now()-this._startTime,
8991
error,
9092
mode,
9193
matches
9294
};
93-
95+
9496
let tool = this._req.tool;
9597
if (tool) {
9698
result.tool = { id: tool.id };
@@ -101,22 +103,22 @@ export default class BrowserSolver {
101103
}
102104
this._callback(result);
103105
}
104-
106+
105107
_getReplace(str) {
106108
return this._req.text.replace(this._regex, str);
107109
}
108-
110+
109111
_getList(str) {
110112
// TODO: should we move this into a worker?
111113
let source = this._req.text, result = "", repl, ref, trimR = 0, regex;
112-
114+
113115
// build a RegExp without the global flag:
114116
try {
115117
regex = new RegExp(this._req.pattern, this._req.flags.replace("g", ""));
116118
} catch(e) {
117119
return null;
118120
}
119-
121+
120122
if (str.search(/\$[&1-9`']/) === -1) {
121123
trimR = str.length;
122124
str = "$&"+str;
@@ -132,4 +134,4 @@ export default class BrowserSolver {
132134
if (trimR) { result = result.substr(0,result.length-trimR); }
133135
return result;
134136
}
135-
}
137+
}

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
gtag('config', 'UA-3579542-6');
1414
</script>
1515

16+
<!-- source for this is in ./assets/workers/RegExWorker.js -->
17+
<script id="regexWorker" type="javascript/worker">
18+
onmessage=function(e){postMessage("onload");var t,s,n,a=e.data,i=a.text,d=a.tests,l=a.mode,r=new RegExp(a.pattern,a.flags),o=[];if("tests"===l)for(var g=0,x=d.length;g<x;g++){let e=d[g];i=e.text,r.lastIndex=0,t=r.exec(i),o[g]=t?{i:t.index,l:t[0].length,id:e.id}:{id:e.id}}else for(;t=r.exec(i);){s===r.lastIndex&&(n={id:"infinite",warning:!0},++r.lastIndex),s=r.lastIndex;var f=t.reduce(function(e,t,s){return(0===s||e.push({s:t}))&&e},[]);if(o.push({i:t.index,l:t[0].length,groups:f}),!r.global)break}postMessage({error:n,matches:o,mode:l})};
19+
</script>
20+
1621
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@400;700&family=Source+Code+Pro:wght@200;400;700&display=swap" rel="stylesheet">
1722
<link rel="stylesheet" type="text/css" href="<%= css_file %>">
1823

0 commit comments

Comments
 (0)