Skip to content
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

Expose WorkboxMessageEvent.ports #2874

Merged
merged 1 commit into from
Jun 23, 2021
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: 4 additions & 2 deletions packages/workbox-window/src/Workbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ class Workbox extends WorkboxEventTarget {
* @param {Event} originalEvent
*/
private readonly _onMessage = async (originalEvent: MessageEvent) => {
const {data, source} = originalEvent;
const {data, ports, source} = originalEvent;

// Wait until there's an "own" service worker. This is used to buffer
// `message` events that may be received prior to calling `register()`.
Expand All @@ -543,8 +543,9 @@ class Workbox extends WorkboxEventTarget {
if (this._ownSWs.has(source as ServiceWorker)) {
this.dispatchEvent(new WorkboxEvent('message', {
data,
sw: source as ServiceWorker,
originalEvent,
ports,
sw: source as ServiceWorker,
}));
}
}
Expand All @@ -564,6 +565,7 @@ export {Workbox};
* @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
* event.
* @property {string} type `message`.
* @property {MessagePort[]} ports The `ports` value from `originalEvent`.
* @property {Workbox} target The `Workbox` instance.
*/

Expand Down
3 changes: 2 additions & 1 deletion packages/workbox-window/src/utils/WorkboxEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export class WorkboxEvent<K extends keyof WorkboxEventMap> {
}

export interface WorkboxMessageEvent extends WorkboxEvent<'message'> {
originalEvent: Event;
data: any;
originalEvent: Event;
ports: readonly MessagePort[];
}

export interface WorkboxLifecycleEvent extends WorkboxEvent<keyof WorkboxLifecycleEventMap> {
Expand Down
3 changes: 2 additions & 1 deletion test/workbox-window/window/sw-message-reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ addEventListener('message', async (event) => {
includeUncontrolled: true,
});
for (const win of windows) {
win.postMessage('postMessage from SW!');
const channel = new MessageChannel();
win.postMessage('postMessage from SW!', [channel.port1]);
}
} else if (event.data.type === 'BROADCAST_BACK') {
const bc = new BroadcastChannel('workbox');
Expand Down
8 changes: 5 additions & 3 deletions test/workbox-window/window/test-Workbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,13 @@ describe(`[workbox-window] Workbox`, function() {
wb.messageSW({type: 'POST_MESSAGE_BACK'});
await nextEvent(wb, 'message');

assertMatchesWorkboxEvent(messageSpy.args[0][0], {
type: 'message',
target: wb,
const wbEvent = messageSpy.args[0][0];
assertMatchesWorkboxEvent(wbEvent, {
data: 'postMessage from SW!',
originalEvent: {type: 'message'},
ports: wbEvent.originalEvent.ports,
target: wb,
type: 'message',
});
});

Expand Down