I'm using this javascript code for @tanstack/query
IndexedDB persistance as documented here.
I'm having too much put()
calls and I would like to throttle/debounce them.
How can I do this in the below code?
function createIDBPersister(store: UseStore): Persister {
return {
async persistClient(client: PersistedClient) {
// this is called a lot of times!
console.log('persistClient()');
set("custom-db", client, store);
},
async restoreClient() {
return await get<PersistedClient>("custom-db", store);
},
async removeClient() {
await del("custom-db", store);
}
};
}
I tried with:
function createIDBPersister(store: UseStore): Persister {
return {
async persistClient(client: PersistedClient) {
debounce(() => { // import { debounce } from 'lodash-es';
console.log('persistClient() inside debounce');
set("custom-db", client, store);
}, 1000)()
},
// ...rest
};
}
But this write the same amount of console.log()
messages of the first one, only a second late.