perf: settingsstate storage performance improvements

This commit is contained in:
SethBurkart123
2025-08-15 10:49:41 +10:00
parent a396aa8a9d
commit 0d6aa1e5fd
+1 -23
View File
@@ -12,7 +12,6 @@ class StorageManager {
private globalListeners: Set<GlobalChangeListener>; private globalListeners: Set<GlobalChangeListener>;
private subscribers: Set<Subscriber<SettingsState>> = new Set(); private subscribers: Set<Subscriber<SettingsState>> = new Set();
private saveTimeout: NodeJS.Timeout | null = null; private saveTimeout: NodeJS.Timeout | null = null;
private pendingSave = false;
private initialized = false; private initialized = false;
private constructor() { private constructor() {
@@ -109,27 +108,7 @@ class StorageManager {
}); });
} }
private async saveToStorage(): Promise<void> { public async saveToStorage(): Promise<void> {
// Clear any existing timeout
if (this.saveTimeout) {
clearTimeout(this.saveTimeout);
}
// Set a new timeout to batch changes
this.saveTimeout = setTimeout(async () => {
if (this.pendingSave) {
// @ts-expect-error
await browser.storage.local.set(this.data);
this.notifySubscribers();
this.pendingSave = false;
}
}, 100); // Adjust delay as needed
this.pendingSave = true;
}
// Add immediate save method for critical updates
public async saveImmediately(): Promise<void> {
if (this.saveTimeout) { if (this.saveTimeout) {
clearTimeout(this.saveTimeout); clearTimeout(this.saveTimeout);
this.saveTimeout = null; this.saveTimeout = null;
@@ -137,7 +116,6 @@ class StorageManager {
// @ts-expect-error // @ts-expect-error
await browser.storage.local.set(this.data); await browser.storage.local.set(this.data);
this.notifySubscribers(); this.notifySubscribers();
this.pendingSave = false;
} }
private async removeFromStorage(key: string): Promise<void> { private async removeFromStorage(key: string): Promise<void> {