@@ -7,9 +7,13 @@ import {
77
88declare const self : ServiceWorkerGlobalScope ;
99
10- let workerCommands : EmulatorFallbackCommand [ ] = [ ] ;
11- let isPaused = false ;
12- let onUnpauseCallbacks : ( ( response : Response ) => void ) [ ] = [ ] ;
10+ const workerInfoById = new Map < string , WorkerInfo > ( ) ;
11+
12+ class WorkerInfo {
13+ public commands : EmulatorFallbackCommand [ ] = [ ] ;
14+ public isPaused = false ;
15+ public onUnpauseCallbacks : ( ( response : Response ) => void ) [ ] = [ ] ;
16+ }
1317
1418function constructJsonResponse ( json : any ) {
1519 return new Response ( JSON . stringify ( json ) , {
@@ -28,23 +32,29 @@ self.addEventListener("message", event => {
2832 const { data} = event ;
2933 if ( data . type === "worker-command" ) {
3034 const command = data . command as EmulatorFallbackCommand ;
35+ const workerId = data . workerId as string ;
36+ let workerInfo = workerInfoById . get ( workerId ) ;
37+ if ( ! workerInfo ) {
38+ workerInfo = new WorkerInfo ( ) ;
39+ workerInfoById . set ( workerId , workerInfo ) ;
40+ }
3141 let shouldPush = true ;
3242 if ( command . type === "input" ) {
3343 if ( command . event . type === "pause" ) {
34- isPaused = true ;
44+ workerInfo . isPaused = true ;
3545 shouldPush = false ;
3646 }
3747 if ( command . event . type === "unpause" ) {
38- isPaused = false ;
48+ workerInfo . isPaused = false ;
3949 shouldPush = false ;
40- for ( const callback of onUnpauseCallbacks ) {
41- callback ( prepareCommandsFetchResponse ( ) ) ;
50+ for ( const callback of workerInfo . onUnpauseCallbacks ) {
51+ callback ( prepareCommandsFetchResponse ( workerId ) ) ;
4252 }
43- onUnpauseCallbacks = [ ] ;
53+ workerInfo . onUnpauseCallbacks = [ ] ;
4454 }
4555 }
4656 if ( shouldPush ) {
47- workerCommands . push ( data . command ) ;
57+ workerInfo . commands . push ( data . command ) ;
4858 }
4959 } else if ( data . type === "init-disk-cache" ) {
5060 const diskFileSpec = data . spec as EmulatorChunkedFileSpec ;
@@ -78,9 +88,21 @@ self.addEventListener("message", event => {
7888self . addEventListener ( "fetch" , ( event : FetchEvent ) => {
7989 const requestUrl = new URL ( event . request . url ) ;
8090 if ( requestUrl . pathname . endsWith ( "/worker-commands" ) ) {
81- handleWorkerCommands ( event ) ;
91+ const workerId = requestUrl . searchParams . get ( "worker-id" ) ;
92+ if ( ! workerId ) {
93+ console . error ( "No worker-id provided" ) ;
94+ event . respondWith ( constructJsonResponse ( [ ] ) ) ;
95+ return ;
96+ }
97+ handleWorkerCommands ( event , workerId ) ;
8298 } else if ( requestUrl . pathname . endsWith ( "/worker-idlewait" ) ) {
83- handleIdleWait ( event ) ;
99+ const workerId = requestUrl . searchParams . get ( "worker-id" ) ;
100+ if ( ! workerId ) {
101+ console . error ( "No worker-id provided" ) ;
102+ event . respondWith ( constructJsonResponse ( { } ) ) ;
103+ return ;
104+ }
105+ handleIdleWait ( event , workerId ) ;
84106 } else if (
85107 diskCacheSpecs . some ( spec =>
86108 decodeURIComponent ( requestUrl . pathname ) . startsWith ( spec . baseUrl )
@@ -90,12 +112,18 @@ self.addEventListener("fetch", (event: FetchEvent) => {
90112 }
91113} ) ;
92114
93- function handleWorkerCommands ( event : FetchEvent ) {
94- if ( isPaused ) {
115+ function handleWorkerCommands ( event : FetchEvent , workerId : string ) {
116+ const workerInfo = workerInfoById . get ( workerId ) ;
117+ if ( ! workerInfo ) {
118+ console . error ( "No worker info found for worker ID" , workerId ) ;
119+ event . respondWith ( constructJsonResponse ( [ ] ) ) ;
120+ return ;
121+ }
122+ if ( workerInfo . isPaused ) {
95123 const unpausePromise = new Promise < Response > ( resolve => {
96124 console . log ( "Emulator paused, waiting for input" ) ;
97125 const startTime = performance . now ( ) ;
98- onUnpauseCallbacks . push ( response => {
126+ workerInfo . onUnpauseCallbacks . push ( response => {
99127 console . log (
100128 "Emulator unpaused after" ,
101129 ( ( performance . now ( ) - startTime ) / 1000 ) . toFixed ( 1 ) ,
@@ -107,16 +135,33 @@ function handleWorkerCommands(event: FetchEvent) {
107135 event . respondWith ( unpausePromise ) ;
108136 return ;
109137 }
110- event . respondWith ( prepareCommandsFetchResponse ( ) ) ;
138+ event . respondWith ( prepareCommandsFetchResponse ( workerId ) ) ;
111139}
112140
113- function prepareCommandsFetchResponse ( ) : Response {
114- const fetchResponse = constructJsonResponse ( workerCommands ) ;
115- workerCommands = [ ] ;
141+ function prepareCommandsFetchResponse ( workerId : string ) : Response {
142+ const workerInfo = workerInfoById . get ( workerId ) ;
143+ if ( ! workerInfo ) {
144+ console . error (
145+ "No worker info found for worker ID, returning no commands" ,
146+ workerId
147+ ) ;
148+ return constructJsonResponse ( [ ] ) ;
149+ }
150+ const fetchResponse = constructJsonResponse ( workerInfo . commands ) ;
151+ workerInfo . commands = [ ] ;
116152 return fetchResponse ;
117153}
118154
119- function handleIdleWait ( event : FetchEvent ) {
155+ function handleIdleWait ( event : FetchEvent , workerId : string ) {
156+ const workerInfo = workerInfoById . get ( workerId ) ;
157+ if ( ! workerInfo ) {
158+ console . error (
159+ "No worker info found for worker ID, skipping idlewait" ,
160+ workerId
161+ ) ;
162+ event . respondWith ( constructJsonResponse ( { } ) ) ;
163+ return ;
164+ }
120165 const requestUrl = new URL ( event . request . url ) ;
121166 const timeout = parseInt ( requestUrl . searchParams . get ( "timeout" ) ! , 10 ) ;
122167 // Subtract 1ms since that's the overhead of a roundtrip with the
@@ -127,7 +172,10 @@ function handleIdleWait(event: FetchEvent) {
127172 const interval = self . setInterval ( ( ) => {
128173 // Interrupt idlewait if new commands (presumably input) has
129174 // come in.
130- if ( workerCommands . length || performance . now ( ) >= endTime ) {
175+ if (
176+ workerInfo . commands . length ||
177+ performance . now ( ) >= endTime
178+ ) {
131179 self . clearInterval ( interval ) ;
132180 resolve ( constructJsonResponse ( { } ) ) ;
133181 }
0 commit comments