-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: support ref cleanup functions #4436
Changes from all commits
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 |
---|---|---|
|
@@ -554,14 +554,26 @@ function diffElementNodes( | |
|
||
/** | ||
* Invoke or update a ref, depending on whether it is a function or object ref. | ||
* @param {Ref<any>} ref | ||
* @param {Ref<any> & { _unmount?: unknown }} ref | ||
* @param {any} value | ||
* @param {VNode} vnode | ||
*/ | ||
export function applyRef(ref, value, vnode) { | ||
try { | ||
if (typeof ref == 'function') ref(value); | ||
else ref.current = value; | ||
if (typeof ref == 'function') { | ||
let hasRefUnmount = typeof ref._unmount == 'function'; | ||
if (hasRefUnmount) { | ||
// @ts-ignore TS doesn't like moving narrowing checks into variables | ||
ref._unmount(); | ||
} | ||
|
||
if (!hasRefUnmount || value != null) { | ||
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. Why do we need EDIT: seems needed when I tested it out 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. Maybe inverting the check makes it more clear. When a cleanup function is returned, then the |
||
// Store the cleanup function on the function | ||
// instance object itself to avoid shape | ||
// transitioning vnode | ||
ref._unmount = ref(value); | ||
} | ||
} else ref.current = value; | ||
} catch (e) { | ||
options._catchError(e, vnode); | ||
} | ||
|
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.
We'll need to add
_unmount
to mangle to be safe I reckonThere 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.
It's already there as we use that for
options._unmount
too.