From 9e19d910c1d361ae46c0699922c267d7773f4bb0 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 3 Jul 2020 11:21:04 +0100 Subject: [PATCH] Bump to 0.2.0 and implement nuke db logic --- src/vector/dendrite-sw.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/vector/dendrite-sw.js b/src/vector/dendrite-sw.js index 1c844989..a10e4a33 100644 --- a/src/vector/dendrite-sw.js +++ b/src/vector/dendrite-sw.js @@ -16,7 +16,17 @@ // 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.1.1"; +const version = "0.2.0"; + +const isVersionBump = function(oldVer, newVer) { + oldVer = oldVer || ""; + const newSegments = newVer.split("."); + const oldSegments = oldVer.split("."); + if (oldSegments.length != 3) { + return true; // brand new + } + return newSegments[1] > oldSegments[1]; +} const bundle_path = self.location.href.replace("/dendrite_sw.js", "") const id = Math.random(); @@ -69,13 +79,30 @@ function syncfs(isStartup) { }); } +function deleteDatabase(name) { + return new Promise(function(resolve, reject) { + const req = indexedDB.deleteDatabase(name); + req.onsuccess = function () { + console.log("Deleted database successfully: ", name); + resolve(); + }; + req.onerror = function (event) { + console.log("Couldn't delete database: ", name, event); + reject(new Error("failed to delete database")); + }; + }); +} + async function initDendrite() { 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}`) + const nukeDatabase = isVersionBump(prevVer, version); + console.log(`dendrite-sw.js: previous ver ${prevVer} current ${version} nuke databases: ${nukeDatabase}`); + if (nukeDatabase) { + await deleteDatabase("/idb"); + } } global.process = {