@@ -47,14 +47,20 @@ declare const SRE: any;
4747 // Set up for node worker_threads versus browser webworker
4848 //
4949 if ( typeof self === 'undefined' ) {
50- self = global . self = global ; // for node, make self be the global object
50+ //
51+ // For node, make self be the global object
52+ //
53+ self = global . self = global ;
5154
52- global . DedicatedWorkerGlobalScope = global . constructor ; // so SRE knows we are a worker
55+ //
56+ // This is so SRE knows we are a worker
57+ //
58+ global . DedicatedWorkerGlobalScope = global . constructor ;
5359
5460 //
55- // Create addEventListener() and postMessage() function
61+ // Create addEventListener() and postMessage() functions
5662 //
57- const { parentPort } = await import (
63+ const { parentPort, workerData } = await import (
5864 /* webpackIgnore: true */ 'node:worker_threads'
5965 ) ;
6066 global . addEventListener = (
@@ -68,25 +74,38 @@ declare const SRE: any;
6874 } ;
6975
7076 //
71- // SRE needs require(), and we use it to load mathmaps
77+ // Get the path to the mathmaps
78+ //
79+ global . maps = workerData . maps ;
80+
81+ //
82+ // We use require() to load mathmaps in node
7283 //
7384 if ( ! global . require ) {
7485 await import ( /* webpackIgnore: true */ './require.mjs' ) ;
7586 }
76-
87+ global . getMap = ( file : string ) =>
88+ Promise . resolve ( JSON . stringify ( global . require ( file ) ) ) ;
89+ } else {
7790 //
78- // Custom loader for mathmaps
91+ // For web workers, make global be the self object
7992 //
80- global . SREfeature = {
81- custom : ( locale : string ) => {
82- const file = 'speech-rule-engine/lib/ mathmaps/' + locale + '.json' ;
83- return Promise . resolve ( JSON . stringify ( global . require ( file ) ) ) ;
84- } ,
85- } ;
86- } else {
87- global = ( self as any ) . global = self ; // for web workers make global be the self object
93+ global = ( self as any ) . global = self ;
94+ //
95+ // We use fetch() to load mathmaps in web workers
96+ //
97+ global . getMap = ( file : string ) =>
98+ fetch ( file )
99+ . then ( ( data ) => data . json ( ) )
100+ . catch ( ( err ) => console . log ( err ) ) ;
88101 }
89- global . exports = self ; // lets SRE get defined as a global variable
102+
103+ //
104+ // Custom loader for mathmaps
105+ //
106+ global . SREfeature = {
107+ custom : ( locale : string ) => global . getMap ( `${ global . maps } /${ locale } .json` ) ,
108+ } ;
90109
91110 //
92111 // Load SRE
0 commit comments