-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat(reactivity): add 'equals' option to watch for custom equality checks #14256
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
e08c83b
1dc011c
521ed12
f55a814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ export interface WatchOptions<Immediate = boolean> extends DebuggerOptions { | |
| immediate?: Immediate | ||
| deep?: boolean | number | ||
| once?: boolean | ||
| equals?: (a: any, b: any) => boolean | ||
| scheduler?: WatchScheduler | ||
| onWarn?: (msg: string, ...args: any[]) => void | ||
| /** | ||
|
|
@@ -240,14 +241,31 @@ export function watch( | |
| if (cb) { | ||
| // watch(source, cb) | ||
| const newValue = effect.run() | ||
| if ( | ||
| deep || | ||
| forceTrigger || | ||
| (isMultiSource | ||
| ? (newValue as any[]).some((v, i) => hasChanged(v, oldValue[i])) | ||
| : hasChanged(newValue, oldValue)) | ||
| ) { | ||
| // cleanup before running cb again | ||
| const areEqual = (a: any, b: any): boolean => { | ||
| if (options.equals) { | ||
| try { | ||
| return !!options.equals(a, b) | ||
| } catch (e) { | ||
| if (call) { | ||
| call(() => { | ||
| throw e | ||
| }, WatchErrorCodes.WATCH_CALLBACK) | ||
| } | ||
| return false | ||
| } | ||
| } | ||
| return !hasChanged(a, b) | ||
| } | ||
| const isChanged = isMultiSource | ||
| ? (newValue as any[]).some((v, i) => !areEqual(v, oldValue[i])) | ||
| : !areEqual(newValue, oldValue) | ||
| // If equals is provided, it fully controls the trigger decision, | ||
| // bypassing deep and forceTrigger logic. | ||
| const shouldTrigger = options.equals | ||
| ? isChanged | ||
| : deep || forceTrigger || isChanged | ||
|
Comment on lines
+261
to
+268
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling around user-provided If the user-provided Wrap the 🔎 Example approach for error handlingConsider wrapping equals calls: const isChanged = isMultiSource
? (newValue as any[]).some((v, i) =>
options.equals
- ? !options.equals(v, oldValue[i])
+ ? !safeEquals(options.equals, v, oldValue[i])
: hasChanged(v, oldValue[i]),
)
: options.equals
- ? !options.equals(newValue, oldValue)
+ ? !safeEquals(options.equals, newValue, oldValue)
: hasChanged(newValue, oldValue)Where
🤖 Prompt for AI Agents |
||
|
|
||
| if (shouldTrigger) { | ||
| if (cleanup) { | ||
| cleanup() | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.