@@ -25,84 +25,88 @@ addEventListener('activate', function (event) {
2525 event . waitUntil ( self . clients . claim ( ) )
2626} )
2727
28- addEventListener ( 'message' , async function ( event ) {
28+ addEventListener ( 'message' , function ( event ) {
2929 const clientId = Reflect . get ( event . source || { } , 'id' )
3030
3131 if ( ! clientId || ! self . clients ) {
3232 return
3333 }
3434
35- const client = await self . clients . get ( clientId )
35+ event . waitUntil (
36+ ( async ( ) => {
37+ const client = await self . clients . get ( clientId )
3638
37- if ( ! client ) {
38- return
39- }
40-
41- if ( event . data === 'CLIENT_CLOSE' ) {
42- const allClients = await self . clients . matchAll ( {
43- type : 'window' ,
44- } )
45-
46- activeClientIds . delete ( clientId )
47-
48- // Await any pending requests from the closing client.
49- // This makes sure that those requests are handled and not passthrough.
50- const pending = pendingRequests . get ( clientId )
51- if ( pending != null && pending . size > 0 ) {
52- await Promise . allSettled ( pending )
53- }
54- pendingRequests . delete ( clientId )
39+ if ( ! client ) {
40+ return
41+ }
5542
56- const remainingClients = allClients . filter ( ( client ) => {
57- return client . id !== clientId
58- } )
43+ if ( event . data === 'CLIENT_CLOSE' ) {
44+ const allClients = await self . clients . matchAll ( {
45+ type : 'window' ,
46+ } )
5947
60- // Unregister itself when there are no more clients
61- if ( remainingClients . length === 0 ) {
62- self . registration . unregister ( )
63- }
48+ activeClientIds . delete ( clientId )
6449
65- sendToClient ( client , {
66- type : 'CLIENT_CLOSED' ,
67- } )
50+ // Await any pending requests from the closing client.
51+ // This makes sure that those requests are handled and not passthrough.
52+ const pending = pendingRequests . get ( clientId )
53+ if ( pending != null && pending . size > 0 ) {
54+ await Promise . allSettled ( pending )
55+ }
56+ pendingRequests . delete ( clientId )
6857
69- return
70- }
58+ const remainingClients = allClients . filter ( ( client ) => {
59+ return client . id !== clientId
60+ } )
7161
72- switch ( event . data ) {
73- case 'KEEPALIVE_REQUEST' : {
74- sendToClient ( client , {
75- type : 'KEEPALIVE_RESPONSE' ,
76- } )
77- break
78- }
62+ // Unregister itself when there are no more clients
63+ if ( remainingClients . length === 0 ) {
64+ await self . registration . unregister ( )
65+ }
7966
80- case 'INTEGRITY_CHECK_REQUEST' : {
81- sendToClient ( client , {
82- type : 'INTEGRITY_CHECK_RESPONSE' ,
83- payload : {
84- packageVersion : PACKAGE_VERSION ,
85- checksum : INTEGRITY_CHECKSUM ,
86- } ,
87- } )
88- break
89- }
67+ await sendToClient ( client , {
68+ type : 'CLIENT_CLOSED' ,
69+ } )
9070
91- case 'MOCK_ACTIVATE' : {
92- activeClientIds . add ( clientId )
71+ return
72+ }
9373
94- sendToClient ( client , {
95- type : 'MOCKING_ENABLED' ,
96- payload : {
97- client : {
98- id : client . id ,
99- frameType : client . frameType ,
100- } ,
101- } ,
102- } )
103- break
104- }
105- }
74+ switch ( event . data ) {
75+ case 'KEEPALIVE_REQUEST' : {
76+ await sendToClient ( client , {
77+ type : 'KEEPALIVE_RESPONSE' ,
78+ } )
79+ break
80+ }
81+
82+ case 'INTEGRITY_CHECK_REQUEST' : {
83+ await sendToClient ( client , {
84+ type : 'INTEGRITY_CHECK_RESPONSE' ,
85+ payload : {
86+ packageVersion : PACKAGE_VERSION ,
87+ checksum : INTEGRITY_CHECKSUM ,
88+ } ,
89+ } )
90+ break
91+ }
92+
93+ case 'MOCK_ACTIVATE' : {
94+ activeClientIds . add ( clientId )
95+
96+ await sendToClient ( client , {
97+ type : 'MOCKING_ENABLED' ,
98+ payload : {
99+ client : {
100+ id : client . id ,
101+ frameType : client . frameType ,
102+ } ,
103+ } ,
104+ } )
105+ break
106+ }
107+ }
108+ } ) ( ) ,
109+ )
106110} )
107111
108112addEventListener ( 'fetch' , function ( event ) {
0 commit comments