Skip to content

Commit 0dd8bcd

Browse files
committed
Create usePoll.ts
1 parent edfc22c commit 0dd8bcd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

packages/vue3/src/usePoll.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ReloadOptions, VoidFunction } from '@inertiajs/core'
2+
import { router } from '@inertiajs/vue3'
3+
import { onMounted, onUnmounted } from 'vue'
4+
5+
export default function usePoll(interval: number, options: ReloadOptions, startOnMount = true): VoidFunction {
6+
let stopFunc: VoidFunction
7+
8+
let stop = () => {
9+
if (stopFunc) {
10+
stopFunc()
11+
}
12+
}
13+
14+
let start = () => {
15+
stop()
16+
stopFunc = router.poll(interval, options)
17+
}
18+
19+
onMounted(() => {
20+
if (startOnMount) {
21+
start()
22+
}
23+
})
24+
25+
onUnmounted(() => {
26+
stop()
27+
})
28+
29+
return {
30+
stop,
31+
start,
32+
}
33+
}

0 commit comments

Comments
 (0)