Skip to content

Commit

Permalink
Improve SettingsManager initial load logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewoook committed Aug 14, 2022
1 parent 69de80d commit 602e32a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/SettingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ export class SettingsManager<T> {
settings: T | null = null;

constructor(defaultSettings: T) {
(async () => {
try {
this.settings = await this.loadSettings();
} catch (err) {
console.error(err);
this.settings = defaultSettings;
await chrome.storage.local.set(defaultSettings);
}
})();
this.loadSettings().then((settings) => {
this.settings = settings;
}).catch((err) => {
console.error(err);
this.settings = defaultSettings;
chrome.storage.local.set(defaultSettings);
});
}

get(key: keyof T) {
return this.settings?.[key];
}

async loadSettings() {
if (!isExtension()) {
return null;
}

const settings = await chrome.storage.local.get(null);
if (settings) {
return settings as T;
Expand Down

0 comments on commit 602e32a

Please sign in to comment.