Skip to content

Commit

Permalink
fix: fix infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Feb 15, 2020
1 parent 0ecd0ee commit 3f5eadc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
11 changes: 1 addition & 10 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ if ('serviceWorker' in navigator) {

static get observedAttributes() {
return [
'auto-reload',
'channel-name',
'path',
'scope',
Expand All @@ -36,12 +35,10 @@ if ('serviceWorker' in navigator) {
* @attr auto-reload
*/
get autoReload() {
return !!this.__autoReload;
return this.hasAttribute('auto-reload');
}

set autoReload(value) {
if (this.__autoReload === value) return;
this.__autoReload = !!value;
if (value) this.setAttribute('auto-reload', '');
else this.removeAttribute('auto-reload');
}
Expand Down Expand Up @@ -154,8 +151,6 @@ if ('serviceWorker' in navigator) {

this.interacted = false;

this.autoReload = false;

this.updateAction = this.getAttribute('update-action') || 'skipWaiting';

this.error = null;
Expand Down Expand Up @@ -194,10 +189,6 @@ if ('serviceWorker' in navigator) {
case 'scope': this.scope = newVal; break;
case 'channel-name': this.channelName = newVal; break;
case 'update-action': this.updateAction = newVal; break;
case 'auto-reload': {
if (!!oldVal === !!newVal) return;
this.autoReload = !!(newVal || newVal === ''); break;
}
}
}

Expand Down
18 changes: 9 additions & 9 deletions test/service-worker.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-env mocha */
import {expect, fixture, oneEvent} from '@open-wc/testing';
import { expect, fixture, oneEvent, html } from '@open-wc/testing';
import '../service-worker.js';
import sinon from 'sinon';

async function unregisterAllServiceWorkers() {
const registrations = await navigator.serviceWorker.getRegistrations();
return await Promise.all(registrations.map((r) => r.unregister()));
return await Promise.all(registrations.map(r => r.unregister()));
}

if ('serviceWorker' in navigator) {
Expand Down Expand Up @@ -87,7 +87,7 @@ if ('serviceWorker' in navigator) {
describe('scope attribute', function() {
const scope = '/party';
it('sets scope property', async function() {
const element = await fixture(`<service-worker scope="${ scope }"></service-worker>`);
const element = await fixture(`<service-worker scope="${scope}"></service-worker>`);
expect(element.scope).to.equal(scope);
});

Expand All @@ -104,7 +104,7 @@ if ('serviceWorker' in navigator) {
const stub = sinon.stub(navigator.serviceWorker, 'register');
const element = await fixture(`<service-worker></service-worker>`);
element.scope = '/scope';
expect(stub).to.have.been.calledWith(sinon.match.string, sinon.match({scope: '/scope'}));
expect(stub).to.have.been.calledWith(sinon.match.string, sinon.match({ scope: '/scope' }));
stub.restore();
});
});
Expand All @@ -115,15 +115,15 @@ if ('serviceWorker' in navigator) {
const element = await fixture(`<service-worker></service-worker>`);
element.remove();
element.scope = '/scope';
expect(spy).to.not.have.been.calledWith(sinon.match.string, sinon.match({scope: '/scope'}));
expect(spy).to.not.have.been.calledWith(sinon.match.string, sinon.match({ scope: '/scope' }));
spy.restore();
});
});
});

describe('auto-reload attribute', function() {
it('sets autoReload property', async function() {
const element = await fixture(`<service-worker auto-reload></service-worker>`);
const element = await fixture(html`<service-worker auto-reload></service-worker>`);
expect(element.autoReload).to.be.true;
});

Expand Down Expand Up @@ -158,7 +158,7 @@ if ('serviceWorker' in navigator) {
describe('update-action attribute', function() {
const updateAction = 'party';
it('sets updateAction property', async function() {
const element = await fixture(`<service-worker update-action="${ updateAction }"></service-worker>`);
const element = await fixture(`<service-worker update-action="${updateAction}"></service-worker>`);
expect(element.updateAction).to.equal(updateAction);
});

Expand All @@ -174,7 +174,7 @@ if ('serviceWorker' in navigator) {
describe('channel-name attribute', function() {
const channelName = 'party';
it('sets channelName property', async function() {
const element = await fixture(`<service-worker channel-name="${ channelName }"></service-worker>`);
const element = await fixture(`<service-worker channel-name="${channelName}"></service-worker>`);
expect(element.channelName).to.equal(channelName);
});

Expand Down Expand Up @@ -216,7 +216,7 @@ if ('serviceWorker' in navigator) {

it('receives messages on the broadcast channel', async function() {
const element = await fixture('<service-worker path="broadcast-sw.js"></service-worker>');
const {detail} = await oneEvent(element, 'message');
const { detail } = await oneEvent(element, 'message');
expect(detail.data.action).to.equal('install');
});
});
Expand Down

0 comments on commit 3f5eadc

Please sign in to comment.