diff --git a/src/composables/index.ts b/src/composables/index.ts new file mode 100644 index 0000000..e91ed36 --- /dev/null +++ b/src/composables/index.ts @@ -0,0 +1 @@ +export { useAutoRefresh } from './useAutoRefresh'; \ No newline at end of file diff --git a/src/composables/useAutoRefresh.ts b/src/composables/useAutoRefresh.ts new file mode 100644 index 0000000..c07db82 --- /dev/null +++ b/src/composables/useAutoRefresh.ts @@ -0,0 +1,30 @@ +import {onUnmounted, ref} from "vue"; + +const DEFAULT_INTERVAL = 1_000; + +export const useAutoRefresh = (callback: () => void | Promise, interval: number = DEFAULT_INTERVAL) => { + const active = ref(false); + let timer: ReturnType | undefined; + + const stop = () => { + if (timer) { + clearInterval(timer); + timer = undefined; + } + active.value = false; + }; + + const start = () => { + if (active.value) { + return; + } + active.value = true; + timer = setInterval(callback, interval); + }; + + const toggle = () => active.value ? stop() : start(); + + onUnmounted(stop); + + return {active, start, stop, toggle}; +}; \ No newline at end of file diff --git a/src/market/acquisition/AcquisitionQuantilsTooltip.vue b/src/market/acquisition/AcquisitionQuantilsTooltip.vue index e4c238c..3beec75 100644 --- a/src/market/acquisition/AcquisitionQuantilsTooltip.vue +++ b/src/market/acquisition/AcquisitionQuantilsTooltip.vue @@ -47,9 +47,9 @@ watchEffect(async () => {