Skip to content

Commit

Permalink
perf(popup): (hopefully) improve reactivity performance
Browse files Browse the repository at this point in the history
  • Loading branch information
WofWca committed Apr 25, 2022
1 parent 4bfa6e9 commit 8a502ab
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/popup/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
settings = s;
settingsLoaded = true;
})
function assignNewSettings(newValues: Partial<Settings>) {
for (const [k_, v] of Object.entries(newValues)) {
const k = k_ as keyof typeof newValues;
(settings[k] as any) = v;
}
}
async function getTab() {
// TODO but what about Kiwi browser? It always opens popup on a separate page. And in general, it's not always
// guaranteed that there will be a tab, is it?
Expand Down Expand Up @@ -174,8 +180,7 @@
if (thisScriptRecentlyUpdatedStorage) {
unhandledStorageChanges = { ...unhandledStorageChanges, ...newValues };
} else {
Object.assign(settings, newValues);
settings = settings;
assignNewSettings(newValues);
}
});
Expand All @@ -199,16 +204,15 @@
() => {
thisScriptRecentlyUpdatedStorage = false;
if (unhandledStorageChanges) {
settings = { ...settings, ...unhandledStorageChanges }
assignNewSettings(unhandledStorageChanges);
unhandledStorageChanges = null;
}
},
500,
);
}, 50);
function updateSettingsLocalCopyAndStorage(newValues: Partial<Settings>) {
Object.assign(settings, newValues);
settings = settings; // Trigger Svelte's reactivity.
assignNewSettings(newValues);
Object.keys(newValues).forEach(key => settingsKeysToSaveToStorage.add(key as keyof typeof newValues));
throttledSaveUnsavedSettingsToStorageAndTriggerCallbacks();
}
Expand Down Expand Up @@ -483,7 +487,6 @@
on:click={async () => {
// TODO same issue as with "retry".
settings.applyTo = 'both';
settings = settings;
await setSettings({ applyTo: 'both', enabled: false });
setSettings({ enabled: true });
}}
Expand Down

0 comments on commit 8a502ab

Please sign in to comment.