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