| 153 | // that registered the worker. It's with the latter the worker should |
| 154 | // communicate with during the response resolving phase. |
| 155 | async function resolveMainClient(event) { |
| 156 | const client = await self.clients.get(event.clientId) |
| 157 | |
| 158 | if (client?.frameType === 'top-level') { |
| 159 | return client |
| 160 | } |
| 161 | |
| 162 | const allClients = await self.clients.matchAll({ |
| 163 | type: 'window', |
| 164 | }) |
| 165 | |
| 166 | return allClients |
| 167 | .filter((client) => { |
| 168 | // Get only those clients that are currently visible. |
| 169 | return client.visibilityState === 'visible' |
| 170 | }) |
| 171 | .find((client) => { |
| 172 | // Find the client ID that's recorded in the |
| 173 | // set of clients that have registered the worker. |
| 174 | return activeClientIds.has(client.id) |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | async function getResponse(event, client, requestId) { |
| 179 | const { request } = event |