Skip to content
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

A way to avoid using outside state #11

Open
posva opened this issue Mar 6, 2024 — with Volta.net · 2 comments
Open

A way to avoid using outside state #11

posva opened this issue Mar 6, 2024 — with Volta.net · 2 comments
Assignees
Labels
⚡️ enhancement improvement over an existing feature

Comments

Copy link
Owner

posva commented Mar 6, 2024

maybe a defineQuery(() => {}) to allow using globals and creating global state associated with the query (e.g. a page?).

Currently one can try to do this:

<script setup lang="ts">
const page = ref(1)
useQuery({
  query() {
     return fetchData(page.value)
  },
  key: () => ['key', page.value]
})
<script>

But this can fail in some scenarios, notably if the same key is used in a different component or if this gets abstracted with

export const useSomeData = () => {
const page = ref(1)
return useQuery({
  query() {
     return fetchData(page.value)
  },
  key: () => ['key', page.value]
})
}

and used in multiple components, the page will end up scoped to the first component that calls it. Both components will create a page ref but only the first one will be used within query and key. Therefore, if the first component is unmounted while the other still lives, the page ref will not be reactive anymore, breaking the usage.

Notes

  • An eslint plugin could warn agains this usage. It would be better to have some runtime warning but not sure how to do it, maybe checking for the active scope and saving it somewhere in dev only?
  • Maybe run within a computed and check its deps
  • The function version is nice to setup initial state. Each query/mutation is effectively a mini store
@posva posva added the ⚡️ enhancement improvement over an existing feature label Mar 6, 2024 — with Volta.net
Copy link
Owner Author

posva commented Mar 18, 2024

There should also be an equivalent for defineMutation()

Copy link
Owner Author

posva commented Mar 27, 2024

I think a good version would be defineQuery(() => { ...}) similar to a setup store. This looks like a composable and behaves like one except it's a singleton

@posva posva self-assigned this Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ enhancement improvement over an existing feature
Projects
Status: In progress
Development

No branches or pull requests

1 participant