@@ -19,45 +19,47 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
import Utils from "../utils/Utils" ;
20
20
21
21
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
+
24
30
solve ( o , callback ) {
25
31
this . _callback = callback ;
26
32
this . _req = o ;
27
-
33
+
28
34
let regex , text = o . text , tests = o . tests , mode = o . mode ;
29
35
try {
30
36
this . _regex = regex = new RegExp ( o . pattern , o . flags ) ;
31
37
} catch ( e ) {
32
38
return this . _onRegExComplete ( { id :"regexparse" , name : e . name , message : e . message } , null , mode ) ;
33
39
}
34
-
40
+
35
41
if ( window . Worker ) {
36
- if ( ! this . _worker ) {
37
- this . _worker = new Worker ( "assets/workers/RegExWorker.js" ) ;
38
- }
42
+ const worker = new Worker ( this . _workerObjectURL ) ;
39
43
40
- this . _worker . onmessage = ( evt ) => {
44
+ worker . onmessage = ( evt ) => {
41
45
if ( evt . data === "onload" ) {
42
46
this . _startTime = Utils . now ( ) ;
43
47
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.
47
50
} , 250 ) ;
48
51
} else {
49
52
clearTimeout ( this . _timeoutId ) ;
53
+ worker . terminate ( ) ;
50
54
this . _onRegExComplete ( evt . data . error , evt . data . matches , evt . data . mode ) ;
51
55
}
52
56
} ;
53
57
54
- clearTimeout ( this . _timeoutId ) ;
55
-
56
58
// 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} ) ;
58
60
} else {
59
61
this . _startTime = Utils . now ( ) ;
60
-
62
+
61
63
// shared between BrowserSolver & RegExWorker
62
64
var matches = [ ] , match , index , error ;
63
65
if ( mode === "tests" ) {
@@ -78,19 +80,19 @@ export default class BrowserSolver {
78
80
}
79
81
}
80
82
// end share
81
-
83
+
82
84
this . _onRegExComplete ( error , matches , mode ) ;
83
85
}
84
86
}
85
-
87
+
86
88
_onRegExComplete ( error , matches , mode ) {
87
89
let result = {
88
90
time : error ? null : Utils . now ( ) - this . _startTime ,
89
91
error,
90
92
mode,
91
93
matches
92
94
} ;
93
-
95
+
94
96
let tool = this . _req . tool ;
95
97
if ( tool ) {
96
98
result . tool = { id : tool . id } ;
@@ -101,22 +103,22 @@ export default class BrowserSolver {
101
103
}
102
104
this . _callback ( result ) ;
103
105
}
104
-
106
+
105
107
_getReplace ( str ) {
106
108
return this . _req . text . replace ( this . _regex , str ) ;
107
109
}
108
-
110
+
109
111
_getList ( str ) {
110
112
// TODO: should we move this into a worker?
111
113
let source = this . _req . text , result = "" , repl , ref , trimR = 0 , regex ;
112
-
114
+
113
115
// build a RegExp without the global flag:
114
116
try {
115
117
regex = new RegExp ( this . _req . pattern , this . _req . flags . replace ( "g" , "" ) ) ;
116
118
} catch ( e ) {
117
119
return null ;
118
120
}
119
-
121
+
120
122
if ( str . search ( / \$ [ & 1 - 9 ` ' ] / ) === - 1 ) {
121
123
trimR = str . length ;
122
124
str = "$&" + str ;
@@ -132,4 +134,4 @@ export default class BrowserSolver {
132
134
if ( trimR ) { result = result . substr ( 0 , result . length - trimR ) ; }
133
135
return result ;
134
136
}
135
- }
137
+ }
0 commit comments