8000 feat(useRefHistory): add `shouldCommit` by JonathanSchndr · Pull Request #4471 · vueuse/vueuse · GitHub
[go: up one dir, main page]

Skip to content

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
Next Next commit
feat(UseRefHistoryOptions): add beforeCommit
  • Loading branch information
JonathanSchndr authored Jan 7, 2025
commit 44d7d0222e83bb8c8ef99e9f33d31a1974c2c07f
15 changes: 15 additions & 0 deletions 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
*/
beforeCommit?: (oldValue: Raw, newValue: Raw) => boolean
}

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,
beforeCommit,
} = 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 (beforeCommit) {
const history = manualHistory.history.value
const lastValue = history.length > 0 ? history[history.length - 1] : undefined
if (!beforeCommit(lastValue, source.value))
return
}

manualCommit()
}

Expand Down
0