Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(useRefHistory): add shouldCommit #4471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 10, 2025
Merged
53 changes: 53 additions & 0 deletions 53 packages/core/useRefHistory/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,31 @@
expect(history.value[0].snapshot).toBe(2)
expect(last.value.snapshot).toBe(2)
})

it('sync: should respect shouldCommit option', () => {
const v = ref(0)

Check failure on line 303 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Usage of ref() is restricted. Use shallowRef() or deepRef() instead

Check failure on line 303 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

Cannot find name 'ref'.

Check failure on line 303 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Cannot find name 'ref'.

Check failure on line 303 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / autofix

Usage of ref() is restricted. Use shallowRef() or deepRef() instead
const { history } = useRefHistory(v, {
flush: 'sync',
shouldCommit: (oldValue, newValue) => newValue > 0,

Check failure on line 306 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

'newValue' is of type 'unknown'.

Check failure on line 306 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

'newValue' is of type 'unknown'.
})

expect(history.value.length).toBe(1)
expect(history.value[0].snapshot).toBe(0)

v.value = -1
expect(history.value.length).toBe(1)

v.value = 2
expect(history.value.length).toBe(2)
expect(history.value[0].snapshot).toBe(2)

v.value = -3
expect(history.value.length).toBe(2)

v.value = 4
expect(history.value.length).toBe(3)
expect(history.value[0].snapshot).toBe(4)
})
})

describe('useRefHistory - pre', () => {
Expand Down Expand Up @@ -576,4 +601,32 @@
expect(history.value[0].snapshot).toBe(2)
expect(last.value.snapshot).toBe(2)
})

it('pre: should respect shouldCommit option', async () => {
const v = ref(0)

Check failure on line 606 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Usage of ref() is restricted. Use shallowRef() or deepRef() instead

Check failure on line 606 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

Cannot find name 'ref'.

Check failure on line 606 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Cannot find name 'ref'.

Check failure on line 606 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / autofix

Usage of ref() is restricted. Use shallowRef() or deepRef() instead
const { history } = useRefHistory(v, {
shouldCommit: (oldValue, newValue) => newValue > 0,

Check failure on line 608 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

'newValue' is of type 'unknown'.

Check failure on line 608 in packages/core/useRefHistory/index.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

'newValue' is of type 'unknown'.
})

expect(history.value.length).toBe(1)
expect(history.value[0].snapshot).toBe(0)

v.value = -1
await nextTick()
expect(history.value.length).toBe(1)

v.value = 2
await nextTick()
expect(history.value.length).toBe(2)
expect(history.value[0].snapshot).toBe(2)

v.value = -3
await nextTick()
expect(history.value.length).toBe(2)

v.value = 4
await nextTick()
expect(history.value.length).toBe(3)
expect(history.value[0].snapshot).toBe(4)
})
})
15 changes: 15 additions & 0 deletions 15 packages/core/useRefHistory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export interface UseRefHistoryOptions<Raw, Serialized = Raw> extends Configurabl
* Deserialize data from the history
*/
parse?: (v: Serialized) => Raw
/**
* Function to determine if the commit should proceed
* @param oldValue Previous value
* @param newValue New value
* @returns boolean indicating if commit should proceed
*/
shouldCommit?: (oldValue: Serialized | undefined, newValue: Raw) => boolean
JonathanSchndr marked this conversation as resolved.
Show resolved Hide resolved
}

export interface UseRefHistoryReturn<Raw, Serialized> extends UseManualRefHistoryReturn<Raw, Serialized> {
Expand Down Expand Up @@ -93,6 +100,7 @@ export function useRefHistory<Raw, Serialized = Raw>(
deep = false,
flush = 'pre',
eventFilter,
shouldCommit,
JonathanSchndr marked this conversation as resolved.
Show resolved Hide resolved
} = options

const {
Expand Down Expand Up @@ -138,6 +146,13 @@ export function useRefHistory<Raw, Serialized = Raw>(
// so we do not trigger an extra commit in the async watcher
ignorePrevAsyncUpdates()

if (shouldCommit) {
const history = manualHistory.history.value
const lastValue = history.length > 0 ? history[history.length - 1].snapshot : undefined
if (!shouldCommit(lastValue, source.value))
return
}

manualCommit()
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.