@@ -19,45 +19,47 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1919import Utils from "../utils/Utils" ;
2020
2121export 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+ }
0 commit comments