Skip to content

Commit

Permalink
initial pass at simplifying handleSet
Browse files Browse the repository at this point in the history
  • Loading branch information
charkour committed Jan 21, 2024
1 parent cfe9bdc commit bbbfbae
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ const useStoreWithUndo = create<StoreState>()(
}),
{
handleSet: (handleSet) =>
throttle<typeof handleSet>((state) => {
throttle<typeof handleSet>((...args) => {
console.info('handleSet called');
handleSet(state);
handleSet(...args);
}, 1000),
},
),
Expand Down
4 changes: 2 additions & 2 deletions examples/web/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const withZundo = temporal<MyState>(
}),
{
handleSet: (handleSet) =>
throttle<typeof handleSet>((state) => {
throttle<typeof handleSet>((...args) => {
console.info('handleSet called');
handleSet(state);
handleSet(...args);
}, 1000),
},
);
Expand Down
4 changes: 2 additions & 2 deletions examples/web/pages/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const useMyStore = create<MyState>()(
{
equality: deepEqual,
handleSet: (handleSet) =>
throttle<typeof handleSet>((state) => {
handleSet(state);
throttle<typeof handleSet>((...args) => {
handleSet(...args);
}, 500),
partialize: (state): HistoryTrackedState => {
const { untrackedValue, ...trackedValues } = state;
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export const temporal = (<TState>(

const curriedHandleSet =
options?.handleSet?.(
(store.temporal.getState() as _TemporalState<TState>)
._handleSet as StoreApi<TState>['setState'],
(store.temporal.getState() as _TemporalState<TState>)._handleSet,
) || (store.temporal.getState() as _TemporalState<TState>)._handleSet;

const temporalHandleSet = (pastState: TState) => {
Expand All @@ -72,7 +71,7 @@ export const temporal = (<TState>(
)
)
) {
curriedHandleSet(pastState, undefined, currentState, deltaState);
curriedHandleSet(pastState, currentState, deltaState);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/temporal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const temporalStateCreator = <TState>(
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();
Expand Down
28 changes: 14 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ type onSave<TState> =
| ((pastState: TState, currentState: TState) => void)
| undefined;

type HandleSet<TState> = (
pastState: Partial<TState>,
currentState: Partial<TState>,
deltaState?: Partial<TState> | null,
) => void;

export interface _TemporalState<TState> {
pastStates: Partial<TState>[];
futureStates: Partial<TState>[];
Expand All @@ -16,15 +22,9 @@ export interface _TemporalState<TState> {
pause: () => void;
resume: () => void;

setOnSave: (onSave: onSave<TState>) => void;
_onSave: onSave<TState>;
_handleSet: (
pastState: TState,
// `replace` will likely be deprecated and removed in the future
replace: Parameters<StoreApi<TState>['setState']>[1],
currentState: TState,
deltaState?: Partial<TState> | null,
) => void;
setOnSave: (onSave: onSave<Partial<TState>>) => void;
_onSave: onSave<Partial<TState>>;
_handleSet: HandleSet<Partial<TState>>;
}

export interface ZundoOptions<TState, PartialTState = TState> {
Expand All @@ -35,11 +35,11 @@ export interface ZundoOptions<TState, PartialTState = TState> {
pastState: Partial<PartialTState>,
currentState: Partial<PartialTState>,
) => Partial<PartialTState> | null;
onSave?: onSave<TState>;
handleSet?: (handleSet: StoreApi<TState>['setState']) => (
pastState: Parameters<StoreApi<TState>['setState']>[0],
// `replace` will likely be deprecated and removed in the future
replace: Parameters<StoreApi<TState>['setState']>[1],
onSave?: onSave<Partial<TState>>;
handleSet?: (
handleSet: HandleSet<TState>,
) => (
pastState: TState,
currentState: PartialTState,
deltaState?: Partial<PartialTState> | null,
) => void;
Expand Down
22 changes: 11 additions & 11 deletions tests/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
},
});
Expand Down Expand Up @@ -644,9 +644,9 @@ describe('Middleware options', () => {
vi.useFakeTimers();
const storeWithHandleSet = createVanillaStore({
handleSet: (handleSet) => {
return throttle<typeof handleSet>((state) => {
return throttle<typeof handleSet>((...args) => {
console.error('handleSet called');
handleSet(state);
handleSet(...args);
}, 1000);
},
});
Expand Down Expand Up @@ -735,10 +735,10 @@ describe('Middleware options', () => {
const storeWithHandleSetAndPartializeAndEquality = createVanillaStore({
handleSet: (handleSet) => {
return throttle<typeof handleSet>(
(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
Expand Down Expand Up @@ -787,10 +787,10 @@ describe('Middleware options', () => {
const storeWithHandleSetAndPartializeAndDiff = createVanillaStore({
handleSet: (handleSet) => {
return throttle<typeof handleSet>(
(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
Expand Down Expand Up @@ -854,10 +854,10 @@ describe('Middleware options', () => {
const storeWithHandleSetAndPartializeAndDiff = createVanillaStore({
handleSet: (handleSet) => {
return throttle<typeof handleSet>(
(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
Expand Down Expand Up @@ -1019,7 +1019,7 @@ describe('Middleware options', () => {
const { _handleSet } =
store.temporal.getState() as _TemporalState<MyState>;
act(() => {
_handleSet(store.getState(), undefined, store.getState(), null);
_handleSet(store.getState(), store.getState(), null);
});
expect(store.temporal.getState().pastStates.length).toBe(1);
});
Expand Down

0 comments on commit bbbfbae

Please sign in to comment.