| 13 | const SAVE_TIMEOUT = 1000; |
| 14 | |
| 15 | export default class UserStorage { |
| 16 | private static loadBarrier: PromiseBarrier<UserSettings, void>; |
| 17 | private static saveStorageBarrier: PromiseBarrier<void, void> | null; |
| 18 | static settings: Readonly<UserSettings>; |
| 19 | |
| 20 | static async loadSettings(): Promise<void> { |
| 21 | if (!UserStorage.settings) { |
| 22 | UserStorage.settings = await UserStorage.loadSettingsFromStorage(); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | private static fillDefaults(settings: UserSettings) { |
| 27 | settings.theme = {...DEFAULT_THEME, ...settings.theme}; |
| 28 | settings.time = {...DEFAULT_SETTINGS.time, ...settings.time}; |
| 29 | settings.presets.forEach((preset) => { |
| 30 | preset.theme = {...DEFAULT_THEME, ...preset.theme}; |
| 31 | }); |
| 32 | settings.customThemes.forEach((site) => { |
| 33 | site.theme = {...DEFAULT_THEME, ...site.theme}; |
| 34 | }); |
| 35 | if (settings.customThemes.length === 0) { |
| 36 | settings.customThemes = DEFAULT_SETTINGS.customThemes; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // migrateAutomationSettings migrates old automation settings to the new interface. |
| 41 | // It will move settings.automation & settings.automationBehavior into, |
| 42 | // settings.automation = { enabled, mode, behavior }. |
| 43 | // Remove this over two years(mid-2024). |
| 44 | // This won't always work, because browsers can decide to instead use the default settings |
| 45 | // when they notice a different type being requested for automation, in that case it's a data-loss |
| 46 | // and not something we can encounter for, except for doing always two extra requests to explicitly |
| 47 | // check for this case which is inefficient usage of requesting storage. |
| 48 | private static migrateAutomationSettings(settings: UserSettings): void { |
| 49 | if (typeof settings.automation === 'string') { |
| 50 | const automationMode = settings.automation; |
| 51 | const automationBehavior: UserSettings['automation']['behavior'] = (settings as any).automationBehaviour; |
| 52 | if (settings.automation === '') { |
| 53 | settings.automation = { |
| 54 | enabled: false, |
| 55 | mode: automationMode, |
| 56 | behavior: automationBehavior, |
| 57 | }; |
| 58 | } else { |
| 59 | settings.automation = { |
| 60 | enabled: true, |
| 61 | mode: automationMode, |
| 62 | behavior: automationBehavior, |
| 63 | }; |
| 64 | } |
| 65 | delete (settings as any).automationBehaviour; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private static migrateSiteListsV2(deprecated: any): Partial<UserSettings> { |
| 70 | const settings: Partial<UserSettings> = {}; |
| 71 | settings.enabledByDefault = !deprecated.applyToListedOnly; |
| 72 | if (settings.enabledByDefault) { |
nothing calls this directly
no test coverage detected