Skip to content

Commit

Permalink
🐛 Wallet Extension ensureExtensionPopupOpened does not wait for RPC…
Browse files Browse the repository at this point in the history
… to be listening.
  • Loading branch information
Chralu committed May 28, 2024
1 parent 216c412 commit a4ec9f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 38 deletions.
9 changes: 1 addition & 8 deletions lib/infrastructure/rpc/browser_extension_aws.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import 'package:stream_channel/stream_channel.dart';
class BrowserExtensionAWS {
BrowserExtensionAWS() {
print('[AWCBrowserExtension] Init');
onMessage.addListener(
allowInterop((message, sender, sendResponse) {
if (message == 'isExtensionPopupReady') {
sendResponse(_isRunning);
}
}),
);
}
static const logName = 'Browser RPC Server';

Expand Down Expand Up @@ -51,7 +44,7 @@ class BrowserExtensionAWS {

onConnectExternal.addListener(allowInterop(_connectionReceived));
_isRunning = true;
sendMessage('extensionPopupReady');
// sendMessage('extensionPopupReady');
},
(error, stack) {
print(
Expand Down
39 changes: 9 additions & 30 deletions web_chrome_extension/src/background.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
chrome.runtime.onMessageExternal.addListener((message, sender, sendResponse) => {
if (message === 'waitForExtensionPopup') {
waitForExtensionPopup().then(() => {
sendResponse()
})
chrome.runtime.onMessageExternal.addListener(async (message, sender, sendResponse) => {
if (message === 'ensureExtensionPopupOpened') {
await ensureExtensionPopupOpened()
sendResponse()
}
})

Expand Down Expand Up @@ -33,33 +32,13 @@ async function openExtensionPopup(): Promise<void> {
})
}

async function extensionPopupExists(): Promise<boolean> {
async function isExtensionPopupOpened(): Promise<boolean> {
return (await findExtensionWindowId()) !== null
}

async function isExtensionPopupReady(): Promise<boolean> {
if (await extensionPopupExists()) {
return await chrome.runtime.sendMessage('isExtensionPopupReady');
async function ensureExtensionPopupOpened() {
if (await isExtensionPopupOpened()) {
return
}
return false
}

async function waitForExtensionPopup() {
return new Promise<void>(async (resolve) => {
if (await isExtensionPopupReady()) {
resolve()
return
}

const _extensionPopupReadyListener = (message: string) => {
if (message === 'extensionPopupReady') {
console.log('Extension popup ready')
chrome.runtime.onMessage.removeListener(_extensionPopupReadyListener)
resolve()
}
}

chrome.runtime.onMessage.addListener(_extensionPopupReadyListener)
openExtensionPopup()
})
await openExtensionPopup()
}

0 comments on commit a4ec9f6

Please sign in to comment.