useWatchOnly.ts 583 B

1234567891011121314151617181920212223242526272829
  1. import { watchEffect, watch, nextTick } from 'vue'
  2. export const useWatchOnly = (...args: Parameters<typeof watch>) => {
  3. const cbRaw = args[1]
  4. let stop
  5. args[1] = (...args) => {
  6. if (stop) {
  7. stop()
  8. } else {
  9. nextTick(stop)
  10. }
  11. return cbRaw(...args)
  12. }
  13. stop = watch(...args)
  14. }
  15. export const useWatchEffectOnly = (...args: Parameters<typeof watchEffect>) => {
  16. const cbRaw = args[0]
  17. let stop
  18. args[0] = (...args) => {
  19. if (stop) {
  20. stop()
  21. } else {
  22. nextTick(stop)
  23. }
  24. return cbRaw(...args)
  25. }
  26. stop = watchEffect(...args)
  27. }