From bbbfbaec74050ece219daee44b4f7000e3515245 Mon Sep 17 00:00:00 2001 From: Charles Kornoelje <33156025+charkour@users.noreply.github.com> Date: Sun, 21 Jan 2024 10:58:05 -0800 Subject: [PATCH] initial pass at simplifying handleSet --- README.md | 4 ++-- examples/web/pages/index.tsx | 4 ++-- examples/web/pages/options.tsx | 4 ++-- src/index.ts | 5 ++--- src/temporal.ts | 2 +- src/types.ts | 28 ++++++++++++++-------------- tests/__tests__/options.test.ts | 22 +++++++++++----------- 7 files changed, 34 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index fe49eaf..57ed213 100644 --- a/README.md +++ b/README.md @@ -398,9 +398,9 @@ const useStoreWithUndo = create()( }), { handleSet: (handleSet) => - throttle((state) => { + throttle((...args) => { console.info('handleSet called'); - handleSet(state); + handleSet(...args); }, 1000), }, ), diff --git a/examples/web/pages/index.tsx b/examples/web/pages/index.tsx index 866bad8..e95fe27 100644 --- a/examples/web/pages/index.tsx +++ b/examples/web/pages/index.tsx @@ -17,9 +17,9 @@ const withZundo = temporal( }), { handleSet: (handleSet) => - throttle((state) => { + throttle((...args) => { console.info('handleSet called'); - handleSet(state); + handleSet(...args); }, 1000), }, ); diff --git a/examples/web/pages/options.tsx b/examples/web/pages/options.tsx index f22bf2e..f01c65f 100644 --- a/examples/web/pages/options.tsx +++ b/examples/web/pages/options.tsx @@ -28,8 +28,8 @@ const useMyStore = create()( { equality: deepEqual, handleSet: (handleSet) => - throttle((state) => { - handleSet(state); + throttle((...args) => { + handleSet(...args); }, 500), partialize: (state): HistoryTrackedState => { const { untrackedValue, ...trackedValues } = state; diff --git a/src/index.ts b/src/index.ts index 5fe86d2..e79fe7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,8 +52,7 @@ export const temporal = (( const curriedHandleSet = options?.handleSet?.( - (store.temporal.getState() as _TemporalState) - ._handleSet as StoreApi['setState'], + (store.temporal.getState() as _TemporalState)._handleSet, ) || (store.temporal.getState() as _TemporalState)._handleSet; const temporalHandleSet = (pastState: TState) => { @@ -72,7 +71,7 @@ export const temporal = (( ) ) ) { - curriedHandleSet(pastState, undefined, currentState, deltaState); + curriedHandleSet(pastState, currentState, deltaState); } }; diff --git a/src/temporal.ts b/src/temporal.ts index 2043fa8..c2c3046 100644 --- a/src/temporal.ts +++ b/src/temporal.ts @@ -58,7 +58,7 @@ export const temporalStateCreator = ( setOnSave: (_onSave) => set({ _onSave }), // Internal properties _onSave: options?.onSave, - _handleSet: (pastState, replace, currentState, deltaState) => { + _handleSet: (pastState, currentState, deltaState) => { // This naively assumes that only one new state can be added at a time if (options?.limit && get().pastStates.length >= options?.limit) { get().pastStates.shift(); diff --git a/src/types.ts b/src/types.ts index e15162e..c9e972d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,12 @@ type onSave = | ((pastState: TState, currentState: TState) => void) | undefined; +type HandleSet = ( + pastState: Partial, + currentState: Partial, + deltaState?: Partial | null, +) => void; + export interface _TemporalState { pastStates: Partial[]; futureStates: Partial[]; @@ -16,15 +22,9 @@ export interface _TemporalState { pause: () => void; resume: () => void; - setOnSave: (onSave: onSave) => void; - _onSave: onSave; - _handleSet: ( - pastState: TState, - // `replace` will likely be deprecated and removed in the future - replace: Parameters['setState']>[1], - currentState: TState, - deltaState?: Partial | null, - ) => void; + setOnSave: (onSave: onSave>) => void; + _onSave: onSave>; + _handleSet: HandleSet>; } export interface ZundoOptions { @@ -35,11 +35,11 @@ export interface ZundoOptions { pastState: Partial, currentState: Partial, ) => Partial | null; - onSave?: onSave; - handleSet?: (handleSet: StoreApi['setState']) => ( - pastState: Parameters['setState']>[0], - // `replace` will likely be deprecated and removed in the future - replace: Parameters['setState']>[1], + onSave?: onSave>; + handleSet?: ( + handleSet: HandleSet, + ) => ( + pastState: TState, currentState: PartialTState, deltaState?: Partial | null, ) => void; diff --git a/tests/__tests__/options.test.ts b/tests/__tests__/options.test.ts index 666415e..0fd4132 100644 --- a/tests/__tests__/options.test.ts +++ b/tests/__tests__/options.test.ts @@ -565,9 +565,9 @@ describe('Middleware options', () => { global.console.info = vi.fn(); const storeWithHandleSet = createVanillaStore({ handleSet: (handleSet) => { - return (state) => { + return (...args) => { console.info('handleSet called'); - handleSet(state); + handleSet(...args); }; }, }); @@ -644,9 +644,9 @@ describe('Middleware options', () => { vi.useFakeTimers(); const storeWithHandleSet = createVanillaStore({ handleSet: (handleSet) => { - return throttle((state) => { + return throttle((...args) => { console.error('handleSet called'); - handleSet(state); + handleSet(...args); }, 1000); }, }); @@ -735,10 +735,10 @@ describe('Middleware options', () => { const storeWithHandleSetAndPartializeAndEquality = createVanillaStore({ handleSet: (handleSet) => { return throttle( - (state) => { + (...args) => { // used for determining how many times `handleSet` is called console.error('handleSet called'); - handleSet(state); + handleSet(...args); }, throttleIntervalInMs, // Call throttle only on leading edge of timeout @@ -787,10 +787,10 @@ describe('Middleware options', () => { const storeWithHandleSetAndPartializeAndDiff = createVanillaStore({ handleSet: (handleSet) => { return throttle( - (state) => { + (...args) => { // used for determining how many times `handleSet` is called console.error('handleSet called'); - handleSet(state); + handleSet(...args); }, throttleIntervalInMs, // Call throttle only on leading edge of timeout @@ -854,10 +854,10 @@ describe('Middleware options', () => { const storeWithHandleSetAndPartializeAndDiff = createVanillaStore({ handleSet: (handleSet) => { return throttle( - (state) => { + (...args) => { // used for determining how many times `handleSet` is called console.error('handleSet called'); - handleSet(state); + handleSet(...args); }, throttleIntervalInMs, // Call throttle only on leading edge of timeout @@ -1019,7 +1019,7 @@ describe('Middleware options', () => { const { _handleSet } = store.temporal.getState() as _TemporalState; act(() => { - _handleSet(store.getState(), undefined, store.getState(), null); + _handleSet(store.getState(), store.getState(), null); }); expect(store.temporal.getState().pastStates.length).toBe(1); });