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()
.then(() => {
console.log('registering service worker');
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);
}
});
return navigator.serviceWorker.ready;
})
.then((_worker) => {
.then((registration) => {
console.log('registered service worker');
const _worker = registration.active;
_worker.controller = registration.active;
const worker = new PromiseWorker(_worker);
console.log('registered service worker');
return worker;
});
} else {

View File

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

View File

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

View File

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