-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: ensure the service worker is awake before every port message (#1433
) * fix: ensure there is an open port connection before sending a port message * chore: remove unnecessary console.log * fix: prevent Error: Invalid url messages when setting a non http active tab * fix test
- Loading branch information
Showing
6 changed files
with
134 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2019-2024 @polkadot/extension-base authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { Message } from '@polkadot/extension-base/types'; | ||
|
||
import { chrome } from '@polkadot/extension-inject/chrome'; | ||
|
||
export function setupPort (portName: string, onMessageHandler: (data: Message['data']) => void, onDisconnectHandler: () => void): chrome.runtime.Port { | ||
const port = chrome.runtime.connect({ name: portName }); | ||
|
||
port.onMessage.addListener(onMessageHandler); | ||
|
||
port.onDisconnect.addListener(() => { | ||
console.log(`Disconnected from ${portName}`); | ||
onDisconnectHandler(); | ||
}); | ||
|
||
return port; | ||
} | ||
|
||
export async function wakeUpServiceWorker (): Promise<{ status: string }> { | ||
return new Promise((resolve, reject) => { | ||
chrome.runtime.sendMessage({ type: 'wakeup' }, (response: { status: string }) => { | ||
if (chrome.runtime.lastError) { | ||
reject(new Error(chrome.runtime.lastError.message)); | ||
} else { | ||
resolve(response); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// This object is required to allow jest.spyOn to be used to create a mock Implementation for testing | ||
export const wakeUpServiceWorkerWrapper = { wakeUpServiceWorker }; | ||
|
||
export async function ensurePortConnection ( | ||
portRef: chrome.runtime.Port | undefined, | ||
portConfig: { | ||
portName: string, | ||
onPortMessageHandler: (data: Message['data']) => void, | ||
onPortDisconnectHandler: () => void | ||
} | ||
): Promise<chrome.runtime.Port> { | ||
const maxAttempts = 5; | ||
const delayMs = 1000; | ||
|
||
for (let attempt = 0; attempt < maxAttempts; attempt++) { | ||
try { | ||
const response = await wakeUpServiceWorkerWrapper.wakeUpServiceWorker(); | ||
|
||
if (response?.status === 'awake') { | ||
if (!portRef) { | ||
return setupPort(portConfig.portName, portConfig.onPortMessageHandler, portConfig.onPortDisconnectHandler); | ||
} | ||
|
||
return portRef; | ||
} | ||
} catch (error) { | ||
console.error(`Attempt ${attempt + 1} failed: ${(error as Error).message}`); | ||
await new Promise((resolve) => setTimeout(resolve, delayMs)); | ||
} | ||
} | ||
|
||
throw new Error('Failed to wake up the service worker and setup the port after multiple attempts'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters