18
18
// If we didn't take that into the account, we could send much simpler signals
19
19
// like just `0` or whatever, but the code would be less resilient.
20
20
21
- function waitForMsgType ( target , type ) {
22
- return new Promise ( resolve => {
23
- target . addEventListener ( 'message' , function onMsg ( { data } ) {
24
- if ( data == null || data . type !== type ) return ;
25
- target . removeEventListener ( 'message' , onMsg ) ;
26
- resolve ( data ) ;
27
- } ) ;
28
- } ) ;
29
- }
30
-
31
- waitForMsgType ( self , 'wasm_bindgen_worker_init' ) . then ( async data => {
32
- // # Note 1
33
- // Our JS should have been generated in
34
- // `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.js`,
35
- // resolve the main module via `../../..`.
36
- //
37
- // This might need updating if the generated structure changes on wasm-bindgen
38
- // side ever in the future, but works well with bundlers today. The whole
39
- // point of this crate, after all, is to abstract away unstable features
40
- // and temporary bugs so that you don't need to deal with them in your code.
41
- //
42
- // # Note 2
43
- // This could be a regular import, but then some bundlers complain about
44
- // circular deps.
45
- //
46
- // Dynamic import could be cheap if this file was inlined into the parent,
47
- // which would require us just using `../../..` in `new Worker` below,
48
- // but that doesn't work because wasm-pack unconditionally adds
49
- // "sideEffects":false (see below).
50
- //
51
- // OTOH, even though it can't be inlined, it should be still reasonably
52
- // cheap since the requested file is already in cache (it was loaded by
53
- // the main thread).
54
- const pkg = await import ( '../../..' ) ;
55
- await pkg . default ( data . module , data . memory ) ;
56
- postMessage ( { type : 'wasm_bindgen_worker_ready' } ) ;
57
- pkg . wbg_rayon_start_worker ( data . receiver ) ;
58
- } ) ;
59
-
60
21
// Note: this is never used, but necessary to prevent a bug in Firefox
61
22
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1702191) where it collects
62
23
// Web Workers that have a shared WebAssembly memory with the main thread,
@@ -72,7 +33,6 @@ export async function startWorkers(module, memory, builder) {
72
33
}
73
34
74
35
const workerInit = {
75
- type : 'wasm_bindgen_worker_init' ,
76
36
module,
77
37
memory,
78
38
receiver : builder . receiver ( )
@@ -86,21 +46,16 @@ export async function startWorkers(module, memory, builder) {
86
46
// way to get asset URLs relative to the module across various bundlers
87
47
// and browser, ideally we should switch to `import.meta.resolve`
88
48
// once it becomes a standard.
89
- //
90
- // Note: we could use `../../..` as the URL here to inline workerHelpers.js
91
- // into the parent entry instead of creating another split point -
92
- // this would be preferable from optimization perspective -
93
- // however, Webpack then eliminates all message handler code
94
- // because wasm-pack produces "sideEffects":false in package.json
95
- // unconditionally.
96
- //
97
- // The only way to work around that is to have side effect code
98
- // in an entry point such as Worker file itself.
99
- const worker = new Worker ( new URL ( './workerHelpers.js' , import . meta. url ) , {
100
- type : 'module'
101
- } ) ;
49
+ const worker = new Worker (
50
+ new URL ( './workerHelpers.worker.js' , import . meta. url ) ,
51
+ {
52
+ type : 'module'
53
+ }
54
+ ) ;
102
55
worker . postMessage ( workerInit ) ;
103
- await waitForMsgType ( worker , 'wasm_bindgen_worker_ready' ) ;
56
+ await new Promise ( resolve =>
57
+ worker . addEventListener ( 'message' , resolve , { once : true } )
58
+ ) ;
104
59
return worker ;
105
60
} )
106
61
) ;
0 commit comments