Skip to content

Commit

Permalink
Mirror the ports property (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored Jun 23, 2021
1 parent 064df86 commit 86ada01
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
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

0 comments on commit 86ada01

Please sign in to comment.