Skip to content

Commit

Permalink
Fire controlling event more than once (GoogleChrome#2817)
Browse files Browse the repository at this point in the history
Dispatches the `controlling` event every time the documentation 
specifies that it should.
  • Loading branch information
rockwalrus committed May 12, 2021
1 parent 79a415c commit c22b871
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/workbox-window/src/Workbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Workbox extends WorkboxEventTarget {

this._registration.addEventListener('updatefound', this._onUpdateFound);
navigator.serviceWorker.addEventListener(
'controllerchange', this._onControllerChange, {once: true});
'controllerchange', this._onControllerChange);

return this._registration;
}
Expand Down
55 changes: 54 additions & 1 deletion test/workbox-window/window/test-Workbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ describe(`[workbox-window] Workbox`, function() {
// the first time a page registers a SW). This case is tested in the
// integration tests.

expect(controlling1Spy.callCount).to.equal(1);
expect(controlling1Spy.callCount).to.equal(2);
assertMatchesWorkboxEvent(controlling1Spy.args[0][0], {
isExternal: false,
isUpdate: true,
Expand All @@ -853,6 +853,14 @@ describe(`[workbox-window] Workbox`, function() {
target: wb1,
type: 'controlling',
});
assertMatchesWorkboxEvent(controlling1Spy.args[1][0], {
isExternal: true,
isUpdate: true,
originalEvent: {type: 'controllerchange'},
sw: await wb1.getSW(),
target: wb1,
type: 'controlling',
});

// This will be an "external" event, due to wb3's SW taking control.
// wb2's SW never controls, because it's stuck in waiting.
Expand All @@ -872,6 +880,51 @@ describe(`[workbox-window] Workbox`, function() {
type: 'controlling',
});
});

it(`runs every time the registered SW is updated`, async function() {
const scriptURL = uniq('sw-skip-waiting.js.njk');
const wb1 = new Workbox(scriptURL);
const controlling1Spy = sandbox.spy();
wb1.addEventListener('controlling', controlling1Spy);
await wb1.register();
await nextEvent(wb1, 'controlling');

await updateVersion('2.0.0', scriptURL);

wb1.update();
await nextEvent(wb1, 'controlling');

await updateVersion('3.0.0', scriptURL);

wb1.update();
await nextEvent(wb1, 'controlling');

expect(controlling1Spy.callCount).to.equal(3);
assertMatchesWorkboxEvent(controlling1Spy.args[0][0], {
isExternal: false,
isUpdate: true,
originalEvent: {type: 'controllerchange'},
sw: await wb1.getSW(),
target: wb1,
type: 'controlling',
});
assertMatchesWorkboxEvent(controlling1Spy.args[1][0], {
isExternal: true,
isUpdate: true,
originalEvent: {type: 'controllerchange'},
sw: await wb1.getSW(),
target: wb1,
type: 'controlling',
});
assertMatchesWorkboxEvent(controlling1Spy.args[2][0], {
isExternal: true,
isUpdate: true,
originalEvent: {type: 'controllerchange'},
sw: await wb1.getSW(),
target: wb1,
type: 'controlling',
});
});
});

describe(`redundant`, function() {
Expand Down

0 comments on commit c22b871

Please sign in to comment.