Throttle?
jaybo opened this issue · 2 comments
jaybo commented
If I have a large store in which a few properties are changing very frequently, but the majority of the store is not, it would improve overall app performance to be able to throttle the frequency of updates to localStorage, say to once every 5 seconds. Obviously I don't care about perfect store fidelity here.
Is this currently possible, or easy to add?
(Nice set of examples BTW!)
soc221b commented
jaybo commented
Perfect! Thank you! I'm using Quasar and changed to using debounce, but same idea:
import { defineStore } from 'pinia';
import {debounce} from "quasar";
export const useFooStore = defineStore('foo-store', {
state: () => {
return {
foo: 0,
};
},
persistedState: {
persist: true,
storage: {
setItem: debounce<(key: string, value: any) => void | Promise<void>>((...args) => {
console.log("setItem done");
localStorage.setItem(...args);
}, 1000),
getItem: (...args) => localStorage.getItem(...args),
removeItem: (...args) => localStorage.removeItem(...args),
},
},
});