Skip to content

Commit

Permalink
✨ improve migration from older version
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot67 committed Sep 24, 2023
1 parent 02624b7 commit ee015dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { AppDataGlobal, AppDataRule } from '~/types/app';
browser.runtime.onInstalled.addListener((event): void => {
if (event.reason === 'install') {
browser.runtime.openOptionsPage();
return;
}

if (event.reason === 'update') {
SettingsStorage.applyMigrations();
}
});

Expand Down
3 changes: 0 additions & 3 deletions src/composables/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default function useSettings() {

async function load(): Promise<void> {
const storageSettings = await SettingsStorage.getItem();
if (storageSettings.version !== defaultSettings.version) {
migrateSettings(storageSettings);
}
settings.version = storageSettings.version;
settings.favicon = storageSettings.favicon;
settings.rules = storageSettings.rules;
Expand Down
13 changes: 13 additions & 0 deletions src/logic/settings-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { storage } from 'webextension-polyfill';
import { defaultSettings } from '~/configuration/settings';
import { migrateSettings } from '~/logic/settings-migrations';
import { AppDataGlobal } from '~/types/app';
import { isDef } from '~/utils';

Expand All @@ -16,10 +17,22 @@ export const SettingsStorage = {
await SettingsStorage.resetItem();
return SettingsStorage.getItem();
}
if (settings.version !== defaultSettings.version) {
migrateSettings(settings);
await SettingsStorage.setItem(settings);
}
return settings;
},

async resetItem(): Promise<void> {
return await SettingsStorage.setItem(defaultSettings);
},

async applyMigrations(): Promise<void> {
const settings = await SettingsStorage.getItem();
if (settings.version !== defaultSettings.version) {
migrateSettings(settings);
}
await SettingsStorage.setItem(settings);
},
};

0 comments on commit ee015dc

Please sign in to comment.