-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(useSupported): support tracking reactivity for the callback function #2904
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
Conversation
packages/core/useSupported/index.ts
Outdated
const isSupported = ref() as Ref<boolean> | ||
export function useSupported(callback: () => unknown) { | ||
const isMounted = useMounted() | ||
const callbackResult = computed(callback) |
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.
This will make callback
be called in on the server side. We should use only one computed instead
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.
@antfu Perhaps I'm misunderstanding a bit of Vue's reactivity system, so correct me if I'm wrong, but I believe what you suggest won't work (although, to be honest, I haven't tested it).
I initially thought about doing it like this:
computed(() => isMounted.value ? fn() : false);
However, as far as I know, computed tracks direct dependencies of the callback passed to it. The "single computed" has two dependencies:
- isMounted
- fn
fn
is not reactive. But the contents of fn
might be. Hence why the double computed, which has:
- isMounted
- Return of fn
Correct me if I'm wrong please.
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.
Vue collects reactive deps automatically on the go. This means at the first run if isMounted
is false, only isMounted
is collected. Later once it flipped to true
and fn
get called, any reactive usage inside fn
will be collected as well. So you don't have such concern
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.
@antfu Done, squash commits as you deem necessary
I added a comment explaining why the useless check is added, if you prefer me to remove it I can do it no problem :)
Before submitting the PR, please make sure you do the following
fixes #123
).Description
Some functions, like
useFullscreen
, accept refs as parameters. Whether the composable is supported or not might depend on those parameters, so this function must be reactive to take that into account.🤖 Generated by Copilot at 96a4dbb
Refactored
useSupported
function to usecomputed
anduseMounted
for better readability and performance. This change was based on a code review suggestion.🤖 Generated by Copilot at 96a4dbb
useSupported
function to usecomputed
anduseMounted
(link)