Spin up a new dendrite every time we get restarted

This commit is contained in:
Kegan Dougal 2020-03-16 12:57:15 +00:00
parent 3d74d336bf
commit d80285d971
1 changed files with 38 additions and 22 deletions

View File

@ -22,15 +22,8 @@ self.importScripts(`${bundle_path}/wasm_exec.js`,
`${bundle_path}/go_http_bridge.js`,
`${bundle_path}/sqlite_bridge.js`)
self.addEventListener('install', function(event) {
console.log(`dendrite-sw.js: v${version} SW install`)
// Tell the browser to kill old sw's running in other tabs and replace them with this one
// This may cause spontaneous logouts.
self.skipWaiting();
})
self.addEventListener('activate', function(event) {
console.log(`dendrite-sw.js: v${version} SW activate`)
function initDendrite() {
console.log(`dendrite-sw.js: v${version} SW init`)
global.process = {
pid: 1,
env: {
@ -47,22 +40,45 @@ self.addEventListener('activate', function(event) {
locateFile: filename => `${bundle_path}/../../sql-wasm.wasm`
}
event.waitUntil(
sqlite_bridge.init(config).then(()=>{
const go = new Go();
return sqlite_bridge.init(config).then(()=>{
console.log(`dendrite-sw.js: v${version} starting dendrite.wasm...`)
const go = new Go()
WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject).then((result) => {
return WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject)
}).then((result) => {
go.run(result.instance)
// make fetch calls go through this sw - notably if a page registers a sw, it does NOT go through any sw by default
// unless you refresh or call this function.
console.log(`dendrite-sw.js: v${version} claiming open browser tabs`)
self.clients.claim()
}).then(async function() {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
for (let i = 0; i < 30; i++) { // 3s
if (global._go_js_server) {
return;
}
await sleep(100);
}
throw new Error("Timed out waiting for _go_js_server to be set.")
});
}
const initDendritePromise = initDendrite();
self.addEventListener('install', function(event) {
console.log(`dendrite-sw.js: v${version} SW install`)
// Tell the browser to kill old sw's running in other tabs and replace them with this one
// This may cause spontaneous logouts.
self.skipWaiting();
})
)
self.addEventListener('activate', function(event) {
console.log(`dendrite-sw.js: v${version} SW activate`)
event.waitUntil(initDendritePromise)
})
async function sendRequestToGo(event) {
await initDendritePromise; // this sets the global fetch listener
if (!global._go_js_server || !global._go_js_server.fetch) {
console.log(`dendrite-sw.js: v${version} no fetch listener present for ${event.request.url}`);
return