Working ServiceWorker

This commit is contained in:
Nicolas Gotchac 2016-12-19 14:39:10 +01:00
parent 1d04f25a0a
commit e377ec3194
4 changed files with 36 additions and 37 deletions

View File

@ -25,29 +25,15 @@ if ('serviceWorker' in navigator) {
.register() .register()
.then(() => { .then(() => {
console.log('registering service worker'); console.log('registering service worker');
return navigator.serviceWorker.ready;
if (navigator.serviceWorker.controller) {
// already active and controlling this page
return navigator.serviceWorker;
}
// wait for a new service worker to control this page
return new Promise((resolve, reject) => {
try {
const onControllerChange = () => {
navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange);
resolve(navigator.serviceWorker);
};
navigator.serviceWorker.addEventListener('controllerchange', onControllerChange);
} catch (error) {
reject(error);
}
});
}) })
.then((_worker) => { .then((registration) => {
console.log('registered service worker');
const _worker = registration.active;
_worker.controller = registration.active;
const worker = new PromiseWorker(_worker); const worker = new PromiseWorker(_worker);
console.log('registered service worker');
return worker; return worker;
}); });
} else { } else {

View File

@ -25,10 +25,12 @@ registerPromiseWorker((msg) => {
}); });
self.addEventListener('install', (event) => { self.addEventListener('install', (event) => {
console.warn('installing sw');
event.waitUntil(self.skipWaiting()); event.waitUntil(self.skipWaiting());
}); });
self.addEventListener('activate', (event) => { self.addEventListener('activate', (event) => {
console.warn('activating sw');
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim());
}); });
@ -141,18 +143,19 @@ function fetchSolc (build) {
return fetch(URL) return fetch(URL)
.then((response) => { .then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') { if (!response || response.status !== 200) {
return response; return response;
} }
const responseToCache = response.clone(); const responseToCache = response.clone();
caches.open(CACHE_NAME) return caches.open(CACHE_NAME)
.then((cache) => { .then((cache) => {
cache.put(URL, responseToCache); return cache.put(URL, responseToCache);
})
.then(() => {
return response;
}); });
return response;
}); });
}); });
} }

View File

@ -74,6 +74,7 @@ export default class WriteContractStore {
@observable workerError = null; @observable workerError = null;
loadingSolidity = false;
lastCompilation = {}; lastCompilation = {};
snippets = SNIPPETS; snippets = SNIPPETS;
worker = null; worker = null;
@ -147,7 +148,11 @@ export default class WriteContractStore {
return; return;
} }
return this.worker if (this.loadingSolidity) {
return this.loadingSolidity;
}
this.loadingSolidity = this.worker
.postMessage({ .postMessage({
action: 'load', action: 'load',
data: build data: build
@ -161,8 +166,11 @@ export default class WriteContractStore {
this.setWorkerError(error); this.setWorkerError(error);
}) })
.then(() => { .then(() => {
this.loadingSolidity = false;
this.loading = false; this.loading = false;
}); });
return this.loadingSolidity;
} }
@action handleOpenDeployModal = () => { @action handleOpenDeployModal = () => {
@ -243,18 +251,20 @@ export default class WriteContractStore {
}); });
} }
return promise.then((data = {}) => { return promise.then((data = null) => {
const { if (data) {
contract, contractIndex, const {
annotations, contracts, errors contract, contractIndex,
} = data.result; annotations, contracts, errors
} = data.result;
this.contract = contract; this.contract = contract;
this.contractIndex = contractIndex; this.contractIndex = contractIndex;
this.annotations = annotations; this.annotations = annotations;
this.contracts = contracts; this.contracts = contracts;
this.errors = errors; this.errors = errors;
}
this.compiled = true; this.compiled = true;
this.compiling = false; this.compiling = false;

View File

@ -166,7 +166,7 @@ module.exports = {
}), }),
new ServiceWorkerWebpackPlugin({ new ServiceWorkerWebpackPlugin({
entry: path.join(__dirname, '../src/serviceWorker.js'), entry: path.join(__dirname, '../src/serviceWorker.js')
}), }),
DappsHTMLInjection DappsHTMLInjection