Skip to content

Commit 40fa715

Browse files
committed
fix: await message handler via event.waitUntil
1 parent e0973aa commit 40fa715

5 files changed

Lines changed: 74 additions & 70 deletions

File tree

‎src/mockServiceWorker.js‎

Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -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

108112
addEventListener('fetch', function (event) {

‎test/browser/msw-api/setup-worker/start/handlers-reset.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('does not accumulate request handlers across restarts', async ({
2424

2525
// Stop and restart the worker.
2626
await page.evaluate(async () => {
27-
window.msw.worker.stop()
27+
await window.msw.worker.stop()
2828
await window.msw.worker.start({ quiet: true })
2929
})
3030

@@ -35,7 +35,7 @@ test('does not accumulate request handlers across restarts', async ({
3535

3636
// Stop and restart the worker again.
3737
await page.evaluate(async () => {
38-
window.msw.worker.stop()
38+
await window.msw.worker.stop()
3939
await window.msw.worker.start({ quiet: true })
4040
})
4141

‎test/browser/msw-api/setup-worker/start/start-after-stop.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ test('handles requests after starting a stopped worker', async ({
2020
const consoleSpy = spyOnConsole()
2121
await loadExample(new URL('./start-after-stop.mocks.ts', import.meta.url))
2222

23-
await page.evaluate(() => {
24-
window.msw.worker.stop()
23+
await page.evaluate(async () => {
24+
await window.msw.worker.stop()
2525
})
2626

2727
expect(consoleSpy.get('log')).toEqual(

‎test/browser/msw-api/setup-worker/stop/removes-all-listeners.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test('removes all listeners when the worker is stopped', async ({
3636
await secondPage.evaluate(async () => {
3737
const worker = window.msw.createWorker()
3838
await worker.start()
39-
worker.stop()
39+
await worker.stop()
4040
})
4141

4242
expect(firstPageConsoleSpy.get('startGroupCollapsed')).toEqual([

‎test/browser/msw-api/unregister.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ test('unregisters itself when not prompted to be activated again', async ({
3333
return typeof window.msw !== 'undefined'
3434
})
3535

36-
await page.evaluate(() => {
37-
return window.msw.worker.start()
36+
await page.evaluate(async () => {
37+
await window.msw.worker.start()
3838
})
3939
await waitForMswActivation()
4040

0 commit comments

Comments
 (0)