Skip to content

Commit

Permalink
fix(history): fix history cache
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Mar 20, 2024
1 parent e08765d commit 2d670fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo } from 'react';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import {
type Options as MutativeOptions,
type Patches,
Expand Down Expand Up @@ -80,6 +80,7 @@ export const useTravel = <S, A extends boolean, F extends boolean>(
initialState: S,
{ maxHistory = 10, initialPatches, ...options }: Options<A, F> = {}
) => {
const resetRef = useRef(false);
const [position, setPosition] = useMutative(-1);
const [allPatches, setAllPatches] = useMutative(
() =>
Expand All @@ -94,6 +95,10 @@ export const useTravel = <S, A extends boolean, F extends boolean>(
});
useEffect(() => {
if (position === -1 && patches.length > 0) {
if (resetRef.current) {
resetRef.current = false;
return;
}
setAllPatches((_allPatches) => {
_allPatches.patches.push(patches);
_allPatches.inversePatches.push(inversePatches);
Expand Down Expand Up @@ -134,30 +139,31 @@ export const useTravel = <S, A extends boolean, F extends boolean>(
)
);
};
let cachedHistory: (F extends true
? Immutable<InitialValue<S>>
: InitialValue<S>)[];
return {
position: cachedPosition,
getHistory: () => {
const history = [state];
if (cachedHistory) return cachedHistory;
cachedHistory = [state];
let currentState = state as any;
for (let i = cachedPosition; i < allPatches.patches.length; i++) {
currentState = apply(
currentState as object,
allPatches.patches[i]
) as S;
console.log('i', i, JSON.stringify(currentState));
history.push(currentState);
cachedHistory.push(currentState);
}
currentState = state as any;
for (let i = cachedPosition - 1; i > -1; i--) {
currentState = apply(
currentState as object,
allPatches.inversePatches[i]
) as S;
console.log('j', i, JSON.stringify(currentState));
history.unshift(currentState);
cachedHistory.unshift(currentState);
}
console.log('history', JSON.stringify(history));
return history;
return cachedHistory;
},
patches: allPatches,
back: (amount = 1) => {
Expand All @@ -172,6 +178,7 @@ export const useTravel = <S, A extends boolean, F extends boolean>(
() => initialPatches ?? { patches: [], inversePatches: [] }
);
setState(() => initialState);
resetRef.current = true;
},
go,
canBack: () => cachedPosition > 0,
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe('useTravel', () => {
);
let [nextState, setState, controls] = result.current;
expect(nextState).toEqual({ todos: [] });
expect(controls.getHistory()).toEqual([{ todos: [] }]);

act(() =>
setState((draft) => {
draft.todos.push({
Expand All @@ -29,6 +31,19 @@ describe('useTravel', () => {
},
],
});
expect(controls.getHistory()).toEqual([
{ todos: [] },
{
todos: [
{
name: 'todo 1',
},
{
name: 'todo 2',
},
],
},
]);

act(() =>
setState((draft) => {
Expand Down Expand Up @@ -251,6 +266,7 @@ describe('useTravel', () => {
act(() => controls.reset());
[nextState, setState, controls] = result.current;
expect(nextState).toEqual({ todos: [] });
expect(controls.getHistory()).toEqual([{ todos: [] }]);
});

it('[useTravel] with normal init state', () => {
Expand Down

0 comments on commit 2d670fa

Please sign in to comment.