Skip to content

feat(queries): add setEnabled function to queries and infinite queries #33

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 2 commits into from
Apr 19, 2021

Conversation

SomaticIT
Copy link
Collaborator

In some situations, we need to enable or disable queries using svelte reactive statements.

We can do this using the setOptions method but it forces us to pass all options.
It could be very useful to have a setEnabled shortcut function which reuse existing options and only update the enabled option.

It allows this code:

<script>
  import { useQuery } from '@sveltestack/svelte-query'

  let isQueryEnabled = false;

  const queryResult = useQuery('projects', getProjects, { enabled: isQueryEnabled })
  $: queryResult.setOptions('projects', getProjects, { enabled: isQueryEnabled })
</script>

to become

<script>
  import { useQuery } from '@sveltestack/svelte-query'

  let isQueryEnabled = false;

  const queryResult = useQuery('projects', getProjects)
  $: queryResult.setEnabled(isQueryEnabled)
</script>

It makes the code more readable and understandable.

Do you agree?

@amen-souissi
Copy link
Collaborator

amen-souissi commented Apr 7, 2021

Thanks :)
Hummm, the others options will be jealous 😁 . Can we generate dynamically a function for each option ? and next we can expose these functions from an "updater" object. what do you think ?

@amen-souissi amen-souissi added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 7, 2021
@SomaticIT
Copy link
Collaborator Author

SomaticIT commented Apr 7, 2021

Hmmm... I'm not sure that all options need this reactivity.

Maybe a good way to handle all other cases would be to add a function named updateOptions or mergeOptions that will merge given options with existing ones.

eg:

function mergeOptions(options: UseQueryOptions<TQueryFnData, TError, TData>): void {
    observer.setOptions({ ...observer.options, ...options })
}

What do you think?

@amen-souissi
Copy link
Collaborator

Yes, cool... it seems ok for me 👍

@SomaticIT
Copy link
Collaborator Author

Do you have a preference on naming?

@amen-souissi
Copy link
Collaborator

maybe updateOptions :)

@SomaticIT
Copy link
Collaborator Author

Hi @amen-souissi,

I added the updateOptions features about 10 days ago.
Did you have time to review?

Is there something missing before merging?

@amen-souissi amen-souissi merged commit 4417065 into SvelteStack:main Apr 19, 2021
@amen-souissi
Copy link
Collaborator

I'm so sorry I haven't seen the changes :( ... merged and released 👍

@SomaticIT SomaticIT deleted the feat-set-enabled branch April 19, 2021 15:51
@SomaticIT
Copy link
Collaborator Author

There is absolutely no problem 😁 !

Thank you for merging and releasing this, I will update my stack.

@saturnonearth
Copy link

Can this apply to, useQueries, as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants