Skip to content

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

Merged
merged 6 commits into from
Mar 28, 2023
Merged

feat(useSupported): support tracking reactivity for the callback function #2904

merged 6 commits into from
Mar 28, 2023

Conversation

ferferga
Copy link
Member

@ferferga ferferga commented Mar 24, 2023

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

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 use computed and useMounted for better readability and performance. This change was based on a code review suggestion.

🤖 Generated by Copilot at 96a4dbb

  • Refactor useSupported function to use computed and useMounted (link)

const isSupported = ref() as Ref<boolean>
export function useSupported(callback: () => unknown) {
const isMounted = useMounted()
const callbackResult = computed(callback)
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

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 :)

@ferferga ferferga requested a review from antfu March 24, 2023 11:05
@antfu antfu changed the title feat(useSupported): ensure reactivity feat(useSupported): support tracking reactivity for the callback function Mar 28, 2023
@antfu antfu enabled auto-merge (squash) March 28, 2023 09:18
@antfu antfu merged commit 55a32d9 into vueuse:main Mar 28, 2023
@ferferga ferferga deleted the reactive-useSupported branch March 28, 2023 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants