forked from matrix/element-web
Spin up a new dendrite every time we get restarted
This commit is contained in:
parent
3d74d336bf
commit
d80285d971
|
@ -22,15 +22,8 @@ self.importScripts(`${bundle_path}/wasm_exec.js`,
|
||||||
`${bundle_path}/go_http_bridge.js`,
|
`${bundle_path}/go_http_bridge.js`,
|
||||||
`${bundle_path}/sqlite_bridge.js`)
|
`${bundle_path}/sqlite_bridge.js`)
|
||||||
|
|
||||||
self.addEventListener('install', function(event) {
|
function initDendrite() {
|
||||||
console.log(`dendrite-sw.js: v${version} SW install`)
|
console.log(`dendrite-sw.js: v${version} SW init`)
|
||||||
// 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`)
|
|
||||||
global.process = {
|
global.process = {
|
||||||
pid: 1,
|
pid: 1,
|
||||||
env: {
|
env: {
|
||||||
|
@ -47,22 +40,45 @@ self.addEventListener('activate', function(event) {
|
||||||
locateFile: filename => `${bundle_path}/../../sql-wasm.wasm`
|
locateFile: filename => `${bundle_path}/../../sql-wasm.wasm`
|
||||||
}
|
}
|
||||||
|
|
||||||
event.waitUntil(
|
const go = new Go();
|
||||||
sqlite_bridge.init(config).then(()=>{
|
return sqlite_bridge.init(config).then(()=>{
|
||||||
console.log(`dendrite-sw.js: v${version} starting dendrite.wasm...`)
|
console.log(`dendrite-sw.js: v${version} starting dendrite.wasm...`)
|
||||||
const go = new Go()
|
return WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject)
|
||||||
WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject).then((result) => {
|
}).then((result) => {
|
||||||
go.run(result.instance)
|
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
|
// 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.
|
// unless you refresh or call this function.
|
||||||
console.log(`dendrite-sw.js: v${version} claiming open browser tabs`)
|
console.log(`dendrite-sw.js: v${version} claiming open browser tabs`)
|
||||||
self.clients.claim()
|
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) {
|
async function sendRequestToGo(event) {
|
||||||
|
await initDendritePromise; // this sets the global fetch listener
|
||||||
if (!global._go_js_server || !global._go_js_server.fetch) {
|
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}`);
|
console.log(`dendrite-sw.js: v${version} no fetch listener present for ${event.request.url}`);
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue