config.ts: Add config updater from version 0.0 to 1.0

This commit is contained in:
glacambre 2018-03-10 14:40:23 +01:00
parent d26e44d06b
commit 77cf277ab0
No known key found for this signature in database
GPG key ID: B9625DB1767553AC

View file

@ -284,14 +284,25 @@ export async function save(storage: "local" | "sync" = get("storageloc")){
}
/** Updates the config to the latest version. */
export function update() {
export async function update() {
let updaters = {
"0.0": async () => {
try {
// Before we had a config system, we had nmaps, and we put them in the
// root namespace because we were young and bold.
let legacy_nmaps = await browser.storage.sync.get("nmaps")
if (legacy_nmaps) {
USERCONFIG["nmaps"] = Object.assign(legacy_nmaps["nmaps"], USERCONFIG["nmaps"])
}
} finally {
set("version", "1.0")
}
},
}
if (!get("version"))
set("version", "0.0")
while (updaters[get("version")] instanceof Function) {
updaters[get("version")]()
await updaters[get("version")]()
}
}
@ -300,25 +311,15 @@ export function update() {
asynchronous calls generated by getAsync.
*/
async function init() {
try {
let syncConfig = await browser.storage.sync.get(CONFIGNAME)
schlepp(syncConfig[CONFIGNAME])
// Local storage overrides sync
let localConfig = await browser.storage.local.get(CONFIGNAME)
schlepp(localConfig[CONFIGNAME])
// Before we had a config system, we had nmaps, and we put them in the
// root namespace because we were young and bold.
let legacy_nmaps = await browser.storage.sync.get("nmaps")
if (legacy_nmaps) {
USERCONFIG["nmaps"] = Object.assign(legacy_nmaps["nmaps"], USERCONFIG["nmaps"])
}
} finally {
update()
INITIALISED = true
for (let waiter of WAITERS) {
waiter()
}
let syncConfig = await browser.storage.sync.get(CONFIGNAME)
schlepp(syncConfig[CONFIGNAME])
// Local storage overrides sync
let localConfig = await browser.storage.local.get(CONFIGNAME)
schlepp(localConfig[CONFIGNAME])
await update()
INITIALISED = true
for (let waiter of WAITERS) {
waiter()
}
}