Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion __test__/useHistoryTravel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('test history travel', () => {
backLength,
forwardLength,
back,
forward,
forward
} = useHistoryTravel(1)
expect(isRef(current)).toBeTruthy()
expect(current.value).toBe(1)
Expand Down Expand Up @@ -58,4 +58,13 @@ describe('test history travel', () => {
expect(backLength.value).toBe(3)
expect(forwardLength.value).toBe(0)
})

test('fix: the first, forward or go, the next, change current but not record to history', () => {
const { current, forward, backLength } = useHistoryTravel(7)
forward()
current.value = 77
expect(backLength.value).toBe(1)
current.value = 99
expect(backLength.value).toBe(2)
})
})
10 changes: 8 additions & 2 deletions src/useHistoryTravel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,21 @@ export function useHistoryTravel<T>(initialValue?: T): ReturnValue<T> {
}

function _back(step = -1) {
if (travelState.past.length <= 0) return
if (travelState.past.length <= 0) {
updateFrom = UpdateStateFrom.USER
return
}
const { _after, _current, _before } = split(step, travelState.past)
updatePast(_before, true)
updateFuture([..._after, current.value])
current.value = _current
}

function _forward(step = 1) {
if (travelState.future.length <= 0) return
if (travelState.future.length <= 0) {
updateFrom = UpdateStateFrom.USER
return
}
const { _after, _current, _before } = split(step, travelState.future)
updatePast([current.value, ..._before])
updateFuture(_after, true)
Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -450,22 +450,14 @@
"@babel/helper-create-class-features-plugin" "^7.10.4"
"@babel/helper-plugin-utils" "^7.10.4"

"@babel/plugin-proposal-class-properties@^7.12.1":
"@babel/plugin-proposal-class-properties@^7.12.1", "@babel/plugin-proposal-class-properties@^7.8.3":
version "7.12.1"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.12.1.tgz#a082ff541f2a29a4821065b8add9346c0c16e5de"
integrity sha512-cKp3dlQsFsEs5CWKnN7BnSHOd0EOW8EKpEjkoz1pO2E5KzIDNV9Ros1b0CnmbVgAGXJubOYVBOGCT1OmJwOI7w==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.12.1"
"@babel/helper-plugin-utils" "^7.10.4"

"@babel/plugin-proposal-class-properties@^7.8.3":
version "7.12.1"
resolved "https://npm.c2cloud.cn/@babel/plugin-proposal-class-properties/download/@babel/plugin-proposal-class-properties-7.12.1.tgz#a082ff541f2a29a4821065b8add9346c0c16e5de"
integrity sha1-oIL/VB8qKaSCEGW4rdk0bAwW5d4=
dependencies:
"@babel/helper-create-class-features-plugin" "^7.12.1"
"@babel/helper-plugin-utils" "^7.10.4"

"@babel/plugin-proposal-decorators@^7.8.3":
version "7.12.12"
resolved "https://npm.c2cloud.cn/@babel/plugin-proposal-decorators/download/@babel/plugin-proposal-decorators-7.12.12.tgz#067a6d3d6ca86d54cf56bb183239199c20daeafe"
Expand Down