mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
perf(mod queue): reduce loading rerenders
This commit is contained in:
@@ -38,7 +38,7 @@ describe('interaction stores', () => {
|
||||
useCreateBoardModalStore.getState().closeCreateBoardModal();
|
||||
useDirectoryModalStore.getState().closeDirectoryModal();
|
||||
useDisclaimerModalStore.getState().closeDisclaimerModal();
|
||||
useFeedResetStore.setState({ reset: null });
|
||||
useFeedResetStore.setState({ currentResetFunction: null, reset: null });
|
||||
usePostNumberStore.setState({ numberToCid: {}, cidToNumber: {} });
|
||||
useSelectedTextStore.getState().resetSelectedText();
|
||||
useSortingStore.getState().setSortType('active');
|
||||
@@ -80,9 +80,17 @@ describe('interaction stores', () => {
|
||||
|
||||
const resetMock = vi.fn();
|
||||
useFeedResetStore.getState().setResetFunction(resetMock);
|
||||
const firstResetInvoker = useFeedResetStore.getState().reset;
|
||||
useFeedResetStore.getState().reset?.();
|
||||
expect(resetMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
const nextResetMock = vi.fn();
|
||||
useFeedResetStore.getState().setResetFunction(nextResetMock);
|
||||
expect(useFeedResetStore.getState().reset).toBe(firstResetInvoker);
|
||||
useFeedResetStore.getState().reset?.();
|
||||
expect(resetMock).toHaveBeenCalledTimes(1);
|
||||
expect(nextResetMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(useSortingStore.getState().sortType).toBe('active');
|
||||
useSortingStore.getState().setSortType('replyCount');
|
||||
expect(useSortingStore.getState().sortType).toBe('replyCount');
|
||||
|
||||
@@ -2,12 +2,26 @@ import { create } from 'zustand';
|
||||
|
||||
interface FeedResetState {
|
||||
reset: (() => void) | null;
|
||||
currentResetFunction: (() => void) | null;
|
||||
setResetFunction: (resetFunction: () => void) => void;
|
||||
}
|
||||
|
||||
const invokeCurrentResetFunction = () => {
|
||||
useFeedResetStore.getState().currentResetFunction?.();
|
||||
};
|
||||
|
||||
const useFeedResetStore = create<FeedResetState>((set) => ({
|
||||
reset: null,
|
||||
setResetFunction: (resetFunction) => set({ reset: resetFunction }),
|
||||
currentResetFunction: null,
|
||||
setResetFunction: (resetFunction) =>
|
||||
set((state) =>
|
||||
state.currentResetFunction === resetFunction && state.reset
|
||||
? state
|
||||
: {
|
||||
currentResetFunction: resetFunction,
|
||||
reset: state.reset ?? invokeCurrentResetFunction,
|
||||
},
|
||||
),
|
||||
}));
|
||||
|
||||
export default useFeedResetStore;
|
||||
|
||||
Reference in New Issue
Block a user