fix: only fill in missing settings on install

This commit is contained in:
Chen Asraf
2023-04-02 11:08:54 +03:00
parent 6092929fe3
commit e162bee758
2 changed files with 31 additions and 8 deletions

View File

@@ -5,3 +5,13 @@ export function waitUntil(cond: () => boolean, cb: () => void) {
}
setTimeout(() => waitUntil(cond, cb), 100)
}
export function objectKeys<T>(obj: T): (keyof T)[] {
return Object.keys(obj as object) as (keyof T)[]
}
type Entry<T> = [keyof T, T[keyof T]]
export function objectEntries<T>(obj: T): Entry<T>[] {
return Object.entries(obj as object) as Entry<T>[]
}

View File

@@ -1,15 +1,28 @@
import { Settings } from './settings'
import { objectEntries, objectKeys } from './utils'
const defaultSettings = {
saveInterval: 5000,
lastPlayedMap: {},
completionPercentMap: {},
useTimestamps: true,
returnToLastTime: true,
completedPercent: 95,
} as Required<Settings>
chrome.runtime.onInstalled.addListener(async () => {
console.log('Extension installed!')
chrome.storage.sync.set({
saveInterval: 5000,
lastPlayedMap: {},
completionPercentMap: {},
useTimestamps: true,
returnToLastTime: true,
completedPercent: 95,
} as Required<Settings>)
chrome.storage.sync.get(objectKeys(defaultSettings), (settings) => {
const out: Partial<Settings> = {}
for (const entry of objectEntries(defaultSettings)) {
const [key, value] = entry
if (settings[key] === undefined) {
out[key] = value as never
}
}
console.log('Filling missing settings:', out)
chrome.storage.sync.set(settings)
})
// remove old keys - 31 Mar 2023
chrome.storage.sync.remove(['lastPlayed'])