Write dendrite version to the database

This commit is contained in:
Kegan Dougal 2020-05-19 15:33:18 +01:00
parent b0fb043c2b
commit ec45bb7976
1 changed files with 48 additions and 33 deletions

View File

@ -13,10 +13,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Dendrite Service Worker version
// Bumping the patch version of this has no side-effects.
// Bumping the minor version of this will delete databases.
const version = "0.0.7"
const bundle_path = self.location.href.replace("/dendrite_sw.js", "") const bundle_path = self.location.href.replace("/dendrite_sw.js", "")
const id = Math.random(); const id = Math.random();
console.log("swjs: ", id," dendrite-sw.js file running...") console.log("swjs: ", id," dendrite-sw.js file running...")
const version = "0.0.7"
self.registration.addEventListener('updatefound', () => { self.registration.addEventListener('updatefound', () => {
console.log("swjs: ", id," updatefound registration event fired") console.log("swjs: ", id," updatefound registration event fired")
const newWorker = self.registration.installing; const newWorker = self.registration.installing;
@ -34,8 +38,15 @@ self.importScripts(`${bundle_path}/wasm_exec.js`,
`${bundle_path}/sqlitejs.js`, `${bundle_path}/sqlitejs.js`,
`${bundle_path}/localforage.js`) `${bundle_path}/localforage.js`)
function initDendrite() { async function initDendrite() {
console.log(`dendrite-sw.js: v${version} SW init`) console.log(`dendrite-sw.js: v${version} SW init`)
// check if we need to purge databases (minor version bump)
const prevVer = await global.localforage.getItem("dendrite_version")
if (prevVer != version) {
const nukeDatabase = false; // TODO
console.log(`dendrite-sw.js: previous ver ${prevVer} current ${version} nuke databases: ${nukeDatabase}`)
}
global.process = { global.process = {
pid: 1, pid: 1,
env: { env: {
@ -53,10 +64,9 @@ function initDendrite() {
} }
const go = new Go(); const go = new Go();
return sqlitejs.init(config).then(()=>{ await sqlitejs.init(config)
console.log(`dendrite-sw.js: v${version} starting dendrite.wasm...`) console.log(`dendrite-sw.js: v${version} starting dendrite.wasm...`)
return WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject) const result = await WebAssembly.instantiateStreaming(fetch(`${bundle_path}/../../dendrite.wasm`), go.importObject)
}).then((result) => {
go.run(result.instance).then(() => { go.run(result.instance).then(() => {
console.log(`dendrite-sw.js: v${version} dendrite.wasm terminated, restarting...`); console.log(`dendrite-sw.js: v${version} dendrite.wasm terminated, restarting...`);
// purge databases and p2p nodes. // purge databases and p2p nodes.
@ -72,20 +82,25 @@ function initDendrite() {
// 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`)
console.log("swjs: ", id," invoke self.clients.claim()") console.log("swjs: ", id," invoke self.clients.claim()")
self.clients.claim() self.clients.claim();
}).then(async function() {
function sleep(ms) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
let serverIsUp = false;
for (let i = 0; i < 30; i++) { // 3s for (let i = 0; i < 30; i++) { // 3s
if (global._go_js_server) { if (global._go_js_server) {
console.log("swjs: ", id," init dendrite promise resolving") console.log("swjs: ", id," init dendrite promise resolving");
return; serverIsUp = true;
break;
} }
await sleep(100); await sleep(100);
} }
throw new Error("Timed out waiting for _go_js_server to be set.") if (!serverIsUp) {
}); throw new Error("Timed out waiting for _go_js_server to be set.");
}
// persist the new version
await global.localforage.setItem("dendrite_version", version);
} }
let initDendritePromise = initDendrite(); let initDendritePromise = initDendrite();