Skip to content

Commit

Permalink
lint and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
charkour committed Oct 24, 2021
1 parent a0f7075 commit 218bede
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const createUndoStore = () => {
clear: () => {
set({ prevStates: [], futureStates: [] });
},
setIsUndoHistoryEnabled: (isEnabled) => {
setIsUndoHistoryEnabled: isEnabled => {
const { prevStates, getStore, options } = get();
const currState = filterState(getStore(), options?.omit);

Expand Down
103 changes: 52 additions & 51 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,65 @@ export interface Options {
}

// custom zustand middleware to get previous state
export const undoMiddleware =
<TState extends UndoState>(config: StateCreator<TState>, options?: Options) =>
(set: SetState<TState>, get: GetState<TState>, api: StoreApi<TState>) => {
const undoStore = createUndoStore();
const { getState, setState } = undoStore;
return config(
(args) => {
/* TODO: const, should call this function and inject the values once, but it does
export const undoMiddleware = <TState extends UndoState>(
config: StateCreator<TState>,
options?: Options
) => (set: SetState<TState>, get: GetState<TState>, api: StoreApi<TState>) => {
const undoStore = createUndoStore();
const { getState, setState } = undoStore;
return config(
args => {
/* TODO: const, should call this function and inject the values once, but it does
it on every action call currently. */
const {
undo,
clear,
redo,
setIsUndoHistoryEnabled,
isUndoHistoryEnabled,
} = getState();
// inject helper functions to user defined store.
set({
undo,
clear,
redo,
getState,
setIsUndoHistoryEnabled,
});
const {
undo,
clear,
redo,
setIsUndoHistoryEnabled,
isUndoHistoryEnabled,
} = getState();
// inject helper functions to user defined store.
set({
undo,
clear,
redo,
getState,
setIsUndoHistoryEnabled,
});

// Get the last state before updating state
const lastState = filterState({ ...get() }, options?.omit);
// Get the last state before updating state
const lastState = filterState({ ...get() }, options?.omit);

set(args);
set(args);

// Get the current state after updating state
const currState = filterState({ ...get() }, options?.omit);
// Get the current state after updating state
const currState = filterState({ ...get() }, options?.omit);

// Only store changes if state isn't equal (or option has been set)
const shouldStoreChange =
isUndoHistoryEnabled &&
(!isEqual(lastState, currState) || options?.allowUnchanged);
// Only store changes if state isn't equal (or option has been set)
const shouldStoreChange =
isUndoHistoryEnabled &&
(!isEqual(lastState, currState) || options?.allowUnchanged);

const limit = options?.historyDepthLimit;
const limit = options?.historyDepthLimit;

if (shouldStoreChange) {
const prevStates = getState().prevStates;
if (limit && prevStates.length >= limit) {
// pop front
prevStates.shift();
}
setState({
prevStates: [...prevStates, lastState],
setStore: set,
futureStates: [],
getStore: get,
options,
});
if (shouldStoreChange) {
const prevStates = getState().prevStates;
if (limit && prevStates.length >= limit) {
// pop front
prevStates.shift();
}
},
get,
api
);
};
setState({
prevStates: [...prevStates, lastState],
setStore: set,
futureStates: [],
getStore: get,
options,
});
}
},
get,
api
);
};

export default undoMiddleware;

0 comments on commit 218bede

Please sign in to comment.