toggleScope.mjs 561 B

1234567891011121314151617181920212223242526272829
  1. // Utilities
  2. import { effectScope, onScopeDispose, watch } from 'vue';
  3. // Types
  4. export function useToggleScope(source, fn) {
  5. let scope;
  6. function start() {
  7. scope = effectScope();
  8. scope.run(() => fn.length ? fn(() => {
  9. scope?.stop();
  10. start();
  11. }) : fn());
  12. }
  13. watch(source, active => {
  14. if (active && !scope) {
  15. start();
  16. } else if (!active) {
  17. scope?.stop();
  18. scope = undefined;
  19. }
  20. }, {
  21. immediate: true
  22. });
  23. onScopeDispose(() => {
  24. scope?.stop();
  25. });
  26. }
  27. //# sourceMappingURL=toggleScope.mjs.map