Skip to content

Change the 'is-ready' postMessage pairs to 'ready:request' and 'ready:response' to be more explicit #5148

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

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs-developer/loading-in-profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Firefox loads the profiles directly into the front-end through a WebChannel mech

A profile can be injected via another website and the postMessage API.

First wait for the page to be ready. This can be done by posting an `{ name: 'is-ready' }` message and waiting for the response of a similar `{ name: 'is-ready' }`.
First wait for the page to be ready. This can be done by posting an `{ name: 'ready:request' }` message and waiting for the response of a similar `{ name: 'ready:response' }`.

```js
/**
Expand All @@ -133,7 +133,7 @@ function openProfile(profile) {
* @param {MessageEvent} event
*/
const listener = ({ data }) => {
if (data?.name === 'is-ready') {
if (data?.name === 'ready:response') {
console.log('The profiler is ready. Injecting the profile.');
isReady = true;
const message = {
Expand All @@ -148,7 +148,7 @@ function openProfile(profile) {
window.addEventListener('message', listener);
while (!isReady) {
await new Promise((resolve) => setTimeout(resolve, 100));
profilerWindow.postMessage({ name: 'is-ready' }, origin);
profilerWindow.postMessage({ name: 'ready:request' }, origin);
}

window.removeEventListener('message', listener);
Expand Down
6 changes: 3 additions & 3 deletions src/actions/receive-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,17 +1712,17 @@ export function retrieveProfileForRawUrl(
case 'inject-profile':
dispatch(viewProfileFromPostMessage(data.profile));
break;
case 'is-ready': {
case 'ready:request': {
// The "inject-profile" event could be coming from a variety of locations.
// It could come from a `window.open` call on another page. It could come
// from an addon. It could come from being embedded in an iframe. In order
// to generically support these cases allow the opener to poll for the
// "is-ready" message.
// "ready:response" message.
console.log(
'Responding via postMessage that the profiler is ready.'
);
const otherWindow = event.source ?? window;
otherWindow.postMessage({ name: 'is-ready' }, '*');
otherWindow.postMessage({ name: 'ready:response' }, '*');
break;
}
default:
Expand Down
8 changes: 6 additions & 2 deletions src/test/components/UrlManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,17 @@ describe('UrlManager', function () {

await new Promise((resolve) => {
function listener({ data }) {
if (data && typeof data === 'object' && data.name === 'is-ready') {
if (
data &&
typeof data === 'object' &&
data.name === 'ready:response'
) {
resolve();
window.removeEventListener('message', listener);
}
}
window.addEventListener('message', listener);
window.postMessage({ name: 'is-ready' }, '*');
window.postMessage({ name: 'ready:request' }, '*');
});

window.postMessage(
Expand Down