Skip to content

Commit

Permalink
feat(useUrlFilterParams): append batch method update for state
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Sep 25, 2023
1 parent 0320995 commit b7b87a0
Showing 1 changed file with 47 additions and 34 deletions.
81 changes: 47 additions & 34 deletions src/hooks/useUrlFilterParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,38 @@ export const useUrlFilterParams = ({ preset }: { preset?: FilterById }) => {
const state = { ...queryState };
let queued = false;

return <T extends keyof QueryState>(key: T) =>
(value: QueryState[T]) => {
state[key] = value;

if (!queued) {
queued = true;
// we batch state changes due current call stack
// and will push it to router together in microtask queue.

// Example
// setPriorityFilter([priority]);
// setStateTypeFilter([typeA, typeB]);

// ...will produce one router push

queueMicrotask(() => {
pushStateToRouter(state);
});
}
};
const push = (nextState: QueryState) => {
if (!queued) {
queued = true;
// we batch state changes due current call stack
// and will push it to router together in microtask queue.

// Example
// setPriorityFilter([priority]);
// setStateTypeFilter([typeA, typeB]);

// ...will produce one router push
queueMicrotask(() => {
pushStateToRouter(nextState);
});
}
};

return {
key:
<T extends keyof QueryState>(key: T) =>
(value: QueryState[T]) => {
state[key] = value;

push(state);
},
batch: () => (nextState: Partial<QueryState>) => {
push({
...state,
...nextState,
});
},
};
}, [queryState, pushStateToRouter]);

const resetQueryState = useCallback(() => {
Expand Down Expand Up @@ -226,20 +238,21 @@ export const useUrlFilterParams = ({ preset }: { preset?: FilterById }) => {

const setters = useMemo(
() => ({
setPriorityFilter: pushStateProvider('priority'),
setStateFilter: pushStateProvider('state'),
setStateTypeFilter: pushStateProvider('stateType'),
setTagsFilter: pushStateProvider('tag'),
setEstimateFilter: pushStateProvider('estimate'),
setIssuerFilter: pushStateProvider('issuer'),
setOwnerFilter: pushStateProvider('owner'),
setParticipantFilter: pushStateProvider('participant'),
setProjectFilter: pushStateProvider('project'),
setStarredFilter: pushStateProvider('starred'),
setWatchingFilter: pushStateProvider('watching'),
setSortFilter: pushStateProvider('sort'),
setFulltextFilter: pushStateProvider('query'),
setLimitFilter: pushStateProvider('limit'),
setPriorityFilter: pushStateProvider.key('priority'),
setStateFilter: pushStateProvider.key('state'),
setStateTypeFilter: pushStateProvider.key('stateType'),
setTagsFilter: pushStateProvider.key('tag'),
setEstimateFilter: pushStateProvider.key('estimate'),
setIssuerFilter: pushStateProvider.key('issuer'),
setOwnerFilter: pushStateProvider.key('owner'),
setParticipantFilter: pushStateProvider.key('participant'),
setProjectFilter: pushStateProvider.key('project'),
setStarredFilter: pushStateProvider.key('starred'),
setWatchingFilter: pushStateProvider.key('watching'),
setSortFilter: pushStateProvider.key('sort'),
setFulltextFilter: pushStateProvider.key('query'),
setLimitFilter: pushStateProvider.key('limit'),
batch: pushStateProvider.batch(),
}),
[pushStateProvider],
);
Expand Down

0 comments on commit b7b87a0

Please sign in to comment.