Skip to content

Commit b1554b8

Browse files
authored
shave 9 bytes (#94)
1 parent f89ef5c commit b1554b8

3 files changed

Lines changed: 27 additions & 33 deletions

File tree

packages/zundo/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ declare module 'zustand/vanilla' {
3232
}
3333
}
3434

35-
const zundoImpl = <TState>(
35+
export const temporal = (<TState>(
3636
config: StateCreator<TState, [], []>,
3737
{
38-
partialize = (state: TState) => state,
38+
partialize = (state) => state,
3939
handleSet = (handleSetCb) => handleSetCb,
4040
...restOptions
4141
} = {} as ZundoOptions<TState>,
@@ -86,7 +86,6 @@ const zundoImpl = <TState>(
8686
);
8787
};
8888
return configWithTemporal as StateCreator<TState, [], []>;
89-
};
89+
}) as unknown as Zundo;
9090

91-
export const temporal = zundoImpl as unknown as Zundo;
9291
export type { ZundoOptions, Zundo, TemporalState };

packages/zundo/src/temporal.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,34 @@ export const createVanillaTemporal = <TState>(
2828
// Fastest way to clone an array on Chromium. Needed to create a new array reference
2929
const pastStates = get().pastStates.slice();
3030
const futureStates = get().futureStates.slice();
31-
if (pastStates.length === 0) {
32-
return;
33-
}
34-
35-
// Based on the steps, get values from the pastStates array and push them to the futureStates array
36-
for (let i = 0; i < steps; i++) {
37-
const pastState = pastStates.pop();
38-
if (pastState) {
39-
futureStates.push(partialize(userGet()));
40-
userSet(pastState);
31+
if (pastStates.length) {
32+
// Based on the steps, get values from the pastStates array and push them to the futureStates array
33+
for (let i = 0; i < steps; i++) {
34+
const pastState = pastStates.pop();
35+
if (pastState) {
36+
futureStates.push(partialize(userGet()));
37+
userSet(pastState);
38+
}
4139
}
42-
}
4340

44-
set({ pastStates, futureStates });
41+
set({ pastStates, futureStates });
42+
}
4543
},
4644
redo: (steps = 1) => {
4745
// Fastest way to clone an array on Chromium. Needed to create a new array reference
4846
const pastStates = get().pastStates.slice();
4947
const futureStates = get().futureStates.slice();
50-
if (futureStates.length === 0) {
51-
return;
52-
}
53-
54-
// Based on the steps, get values from the futureStates array and push them to the pastStates array
55-
for (let i = 0; i < steps; i++) {
56-
const futureState = futureStates.pop();
57-
if (futureState) {
58-
pastStates.push(partialize(userGet()));
59-
userSet(futureState);
48+
if (futureStates.length) {
49+
// Based on the steps, get values from the futureStates array and push them to the pastStates array
50+
for (let i = 0; i < steps; i++) {
51+
const futureState = futureStates.pop();
52+
if (futureState) {
53+
pastStates.push(partialize(userGet()));
54+
userSet(futureState);
55+
}
6056
}
57+
set({ pastStates, futureStates });
6158
}
62-
63-
set({ pastStates, futureStates });
6459
},
6560
clear: () => {
6661
set({ pastStates: [], futureStates: [] });
@@ -78,10 +73,10 @@ export const createVanillaTemporal = <TState>(
7873
// Internal properties
7974
__onSave: onSave,
8075
__handleSet: (pastState) => {
81-
const trackingStatus = get().trackingStatus,
82-
onSave = get().__onSave,
83-
pastStates = get().pastStates.slice(),
84-
currentState = partialize(userGet());
76+
const trackingStatus = get().trackingStatus;
77+
const onSave = get().__onSave;
78+
const pastStates = get().pastStates.slice();
79+
const currentState = partialize(userGet());
8580
if (
8681
trackingStatus === 'tracking' &&
8782
!equality?.(currentState, pastState)

packages/zundo/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ export type Write<T, U> = Omit<T, keyof U> & U;
6565

6666
export type TemporalState<TState> = Omit<
6767
TemporalStateWithInternals<TState>,
68-
'__onSave' | '__handleUserSet'
68+
'__onSave' | '__handleSet'
6969
>;

0 commit comments

Comments
 (0)