Skip to content

Commit 18acfab

Browse files
JonathanSchndrantfuOrbisK
authored
feat(useRefHistory): add shouldCommit (#4471)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> Co-authored-by: Robin <robin.kehl@singular-it.de>
1 parent ae573a0 commit 18acfab

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

packages/core/useRefHistory/index.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ describe('useRefHistory - sync', () => {
298298
expect(history.value[0].snapshot).toBe(2)
299299
expect(last.value.snapshot).toBe(2)
300300
})
301+
302+
it('sync: should respect shouldCommit option', () => {
303+
const v = deepRef(0)
304+
const { history } = useRefHistory(v, {
305+
flush: 'sync',
306+
shouldCommit: (oldValue: number | undefined, newValue: number) => newValue > 0,
307+
})
308+
309+
expect(history.value.length).toBe(1)
310+
expect(history.value[0].snapshot).toBe(0)
311+
312+
v.value = -1
313+
expect(history.value.length).toBe(1)
314+
315+
v.value = 2
316+
expect(history.value.length).toBe(2)
317+
expect(history.value[0].snapshot).toBe(2)
318+
319+
v.value = -3
320+
expect(history.value.length).toBe(2)
321+
322+
v.value = 4
323+
expect(history.value.length).toBe(3)
324+
expect(history.value[0].snapshot).toBe(4)
325+
})
301326
})
302327

303328
describe('useRefHistory - pre', () => {
@@ -576,4 +601,32 @@ describe('useRefHistory - pre', () => {
576601
expect(history.value[0].snapshot).toBe(2)
577602
expect(last.value.snapshot).toBe(2)
578603
})
604+
605+
it('pre: should respect shouldCommit option', async () => {
606+
const v = deepRef(0)
607+
const { history } = useRefHistory(v, {
608+
shouldCommit: (oldValue: number | undefined, newValue: number) => newValue > 0,
609+
})
610+
611+
expect(history.value.length).toBe(1)
612+
expect(history.value[0].snapshot).toBe(0)
613+
614+
v.value = -1
615+
await nextTick()
616+
expect(history.value.length).toBe(1)
617+
618+
v.value = 2
619+
await nextTick()
620+
expect(history.value.length).toBe(2)
621+
expect(history.value[0].snapshot).toBe(2)
622+
623+
v.value = -3
624+
await nextTick()
625+
expect(history.value.length).toBe(2)
626+
627+
v.value = 4
628+
await nextTick()
629+
expect(history.value.length).toBe(3)
630+
expect(history.value[0].snapshot).toBe(4)
631+
})
579632
})

packages/core/useRefHistory/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export interface UseRefHistoryOptions<Raw, Serialized = Raw> extends Configurabl
4545
* Deserialize data from the history
4646
*/
4747
parse?: (v: Serialized) => Raw
48+
/**
49+
* Function to determine if the commit should proceed
50+
* @param oldValue Previous value
51+
* @param newValue New value
52+
* @returns boolean indicating if commit should proceed
53+
*/
54+
shouldCommit?: (oldValue: Raw | undefined, newValue: Raw) => boolean
4855
}
4956

5057
export interface UseRefHistoryReturn<Raw, Serialized> extends UseManualRefHistoryReturn<Raw, Serialized> {
@@ -93,6 +100,7 @@ export function useRefHistory<Raw, Serialized = Raw>(
93100
deep = false,
94101
flush = 'pre',
95102
eventFilter,
103+
shouldCommit = () => true,
96104
} = options
97105

98106
const {
@@ -102,6 +110,9 @@ export function useRefHistory<Raw, Serialized = Raw>(
102110
isActive: isTracking,
103111
} = pausableFilter(eventFilter)
104112

113+
// Track the last raw value for shouldCommit comparison
114+
let lastRawValue: Raw | undefined = source.value
115+
105116
const {
106117
ignoreUpdates,
107118
ignorePrevAsyncUpdates,
@@ -125,6 +136,7 @@ export function useRefHistory<Raw, Serialized = Raw>(
125136

126137
ignoreUpdates(() => {
127138
source.value = value
139+
lastRawValue = value
128140
})
129141
}
130142

@@ -138,6 +150,10 @@ export function useRefHistory<Raw, Serialized = Raw>(
138150
// so we do not trigger an extra commit in the async watcher
139151
ignorePrevAsyncUpdates()
140152

153+
if (!shouldCommit(lastRawValue, source.value))
154+
return
155+
156+
lastRawValue = source.value
141157
manualCommit()
142158
}
143159

0 commit comments

Comments
 (0)