-
-
Notifications
You must be signed in to change notification settings - Fork 9k
fix(types): watchEffect typing does not support async callback (#14249) #14251
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?
Conversation
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/reactivity/src/watch.ts (1)
37-37: Critical: TheWatchEffecttype was not updated to support async callbacks.The PR objectives and issue #14249 request changing
WatchEffectto allow async callbacks, but this line remains unchanged. The fix should be:-export type WatchEffect = (onCleanup: OnCleanup) => void +export type WatchEffect = (onCleanup: OnCleanup) => Promise<void> | voidThis is the actual type that governs whether
watchEffectcan accept an async callback. Without this change, TypeScript will still reject async watchEffect callbacks.🔎 Proposed fix
-export type WatchEffect = (onCleanup: OnCleanup) => void +export type WatchEffect = (onCleanup: OnCleanup) => Promise<void> | void
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
packages/reactivity/src/watch.ts (1)
47-47: Critical: Revert OnCleanup type change - promises would be ignored.The change to
OnCleanupallowingPromise<void>is incorrect and creates a type/implementation mismatch:
OnCleanupis the type of the cleanup registration function (theonCleanupparameter passed TO your watchEffect callback)- The implementation at line 292 (
boundCleanup = fn => onWatcherCleanup(fn, false, effect)) callsonWatcherCleanupwhich returnsvoid(line 107)- Any promise returned by
OnCleanupwould be completely ignored by the implementationOnly
WatchEffect(line 37) should allow async return - the cleanup registration itself must remain synchronous.🔎 Proposed fix
-export type OnCleanup = (cleanupFn: () => void) => Promise<void> | void +export type OnCleanup = (cleanupFn: () => void) => voidBased on past review comments.
🧹 Nitpick comments (1)
packages/runtime-core/__tests__/apiWatch.spec.ts (1)
1107-1108: Clarify: Why change spy() to spy(arr)?The removal of
return arrat line 1108 makes sense for type compliance (watchEffect callbacks should returnvoid | Promise<void>, not a Ref). However, the change fromspy()tospy(arr)at line 1107 is unclear:
- The test assertions only check call counts (
toHaveBeenCalledTimes), not spy arguments- This change doesn't affect test behavior
- It doesn't appear directly related to the type fix
Is this an intentional improvement to make the test track the watched value, or an accidental change?
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/reactivity/src/watch.tspackages/runtime-core/__tests__/apiWatch.spec.ts
🧰 Additional context used
🧬 Code graph analysis (1)
packages/reactivity/src/watch.ts (3)
packages/runtime-core/src/index.ts (1)
WatchEffect(230-230)packages/runtime-core/src/apiWatch.ts (2)
WatchEffect(29-29)OnCleanup(32-32)packages/reactivity/src/index.ts (2)
WatchEffect(93-93)OnCleanup(96-96)
close #14249
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.