A way to detect a composable is being used in setup store #2713
-
What problem is this solvingIn vue-concurrency I have a composable called "useTask" and most common usecase for it is to run it in components. But there's also usecase for global tasks and pinia is a good fit. Currently I issue a warning in the console when a task is being used outside of component setup. Which is annoying if someone uses it in pinia. It would be great if I, as a library author, could detect that the composable is being run inside pinia. I could then turn of this warning. Proposed solutionI can imagine I could use an equivalent to So something like Alternatively I could just look up some flag on some global object? This would also mean I don't have to have Describe alternatives you've consideredI can provide an API to explicitly turn off the warnings but it would be great if I didn't have to do this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can use You shouldn't need |
Beta Was this translation helpful? Give feedback.
You can use
getCurrentScope()
to know if you are within an effect scope.You shouldn't need
pinia
as a dependency, that's for sure. In library code, you can check for a currentInstance and conditionally setup hooks if needed but foronUnmounted()
you can also useonScopeDispose()
. This should make your library work with libraries other than Pinia that use the same pattern