-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Popup ready signal to service worker #174
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fddafca
Create popup ready message
JasonMHasperhoven 831ea52
Add scripts/private to gitignore
JasonMHasperhoven 5564da1
Abstract popupReady
JasonMHasperhoven 8e03a5d
Refactor popup ready
JasonMHasperhoven 3f788eb
[pairing] update types
grod220 ccd44ae
Refactor isPopupReadyResponse
JasonMHasperhoven 7e7355b
Fix typo
JasonMHasperhoven 6829bf6
Refactor popup ready msging
JasonMHasperhoven 3cba6f5
Add comment
JasonMHasperhoven a9d0478
Remove usePopupReady arg
JasonMHasperhoven 861180e
Use pathname for throwIfAlreadyOpen
JasonMHasperhoven 20472ac
Refactor throwIfAlreadyOpen to use hashPath
JasonMHasperhoven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -62,3 +62,4 @@ packages/*/package | |
|
||
apps/extension/chromium-profile | ||
|
||
scripts/private | ||
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,17 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
|
||
// signals that react is ready (mounted) to service worker | ||
export const usePopupReady = () => { | ||
const sentMessagesRef = useRef(new Set()); | ||
const [searchParams] = useSearchParams(); | ||
const popupId = searchParams.get('popupId'); | ||
|
||
useEffect(() => { | ||
if (popupId && !sentMessagesRef.current.has(popupId)) { | ||
sentMessagesRef.current.add(popupId); | ||
|
||
void chrome.runtime.sendMessage(popupId); | ||
} | ||
}, [popupId]); | ||
}; |
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 |
---|---|---|
|
@@ -18,9 +18,13 @@ const isChromeResponderDroppedError = ( | |
export const popup = async <M extends PopupMessage>( | ||
req: PopupRequest<M>, | ||
): Promise<M['response']> => { | ||
await spawnPopup(req.type); | ||
// We have to wait for React to bootup, navigate to the page, and render the components | ||
await new Promise(resolve => setTimeout(resolve, 800)); | ||
const popupId = crypto.randomUUID(); | ||
await spawnPopup(req.type, popupId); | ||
|
||
// this is necessary given it takes a bit of time for the popup | ||
// to be ready to accept messages from the service worker. | ||
await popupReady(popupId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: we should add a code comment here that informs devs that this is necessary given it takes a bit of time for the popup to be ready to accept messages. |
||
|
||
const response = await chrome.runtime | ||
.sendMessage<InternalRequest<M>, InternalResponse<M>>(req) | ||
.catch((e: unknown) => { | ||
|
@@ -30,20 +34,22 @@ export const popup = async <M extends PopupMessage>( | |
throw e; | ||
} | ||
}); | ||
|
||
if (response && 'error' in response) { | ||
throw errorFromJson(response.error, undefined, ConnectError.from(response)); | ||
} else { | ||
return response && response.data; | ||
} | ||
}; | ||
|
||
const spawnDetachedPopup = async (path: string) => { | ||
await throwIfAlreadyOpen(path); | ||
const spawnDetachedPopup = async (url: URL) => { | ||
const [hashPath] = url.hash.split('?'); | ||
await throwIfAlreadyOpen(hashPath!); | ||
|
||
const { top, left, width } = await chrome.windows.getLastFocused(); | ||
|
||
await chrome.windows.create({ | ||
url: path, | ||
url: url.href, | ||
type: 'popup', | ||
width: 400, | ||
height: 628, | ||
|
@@ -73,19 +79,36 @@ const throwIfNeedsLogin = async () => { | |
} | ||
}; | ||
|
||
const spawnPopup = async (pop: PopupType) => { | ||
const spawnPopup = async (pop: PopupType, popupId: string) => { | ||
const popUrl = new URL(chrome.runtime.getURL('popup.html')); | ||
|
||
await throwIfNeedsLogin(); | ||
|
||
switch (pop) { | ||
// set path as hash since we use a hash router within the popup | ||
case PopupType.OriginApproval: | ||
popUrl.hash = PopupPath.ORIGIN_APPROVAL; | ||
return spawnDetachedPopup(popUrl.href); | ||
popUrl.hash = `${PopupPath.ORIGIN_APPROVAL}?popupId=${popupId}`; | ||
return spawnDetachedPopup(popUrl); | ||
case PopupType.TxApproval: | ||
popUrl.hash = PopupPath.TRANSACTION_APPROVAL; | ||
return spawnDetachedPopup(popUrl.href); | ||
popUrl.hash = `${PopupPath.TRANSACTION_APPROVAL}?popupId=${popupId}`; | ||
return spawnDetachedPopup(popUrl); | ||
default: | ||
throw Error('Unknown popup type'); | ||
} | ||
}; | ||
|
||
const POPUP_READY_TIMEOUT = 60 * 1000; | ||
|
||
const popupReady = (popupId: string): Promise<void> => | ||
new Promise((resolve, reject) => { | ||
AbortSignal.timeout(POPUP_READY_TIMEOUT).onabort = reject; | ||
|
||
const idListen = (msg: unknown, _: chrome.runtime.MessageSender, respond: () => void) => { | ||
if (msg === popupId) { | ||
resolve(); | ||
chrome.runtime.onMessage.removeListener(idListen); | ||
respond(); | ||
} | ||
}; | ||
|
||
chrome.runtime.onMessage.addListener(idListen); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: revert gitignore diff