Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Add some additional poll helpers #2074

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/core/src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Poll {
if (this.id) {
// console.log('clearing interval...')
clearInterval(this.id)
this.id = null
}
}

Expand Down Expand Up @@ -52,4 +53,16 @@ export class Poll {
this.cbCount = 0
}
}

public toggle(): void {
if (this.polling()) {
return this.stop()
}

this.start()
}

public polling(): boolean {
return !!this.id
}
}
10 changes: 2 additions & 8 deletions packages/core/src/polls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ class Polls {
interval: number,
cb: VoidFunction,
options: PollOptions,
): {
stop: VoidFunction
start: VoidFunction
} {
): Poll {
const poll = new Poll(interval, cb, options)

this.polls.push(poll)

return {
stop: () => poll.stop(),
start: () => poll.start(),
}
return poll
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure why the reference gets broken here, but it's needed to keep the reactivity later down the line

}

public clear() {
Expand Down
9 changes: 8 additions & 1 deletion packages/react/src/usePoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export default function usePoll(
keepAlive: false,
autoStart: true,
},
) {
): {
stop: VoidFunction
start: VoidFunction
toggle: VoidFunction
polling: () => boolean
} {
const pollRef = useRef(
router.poll(interval, requestOptions, {
...options,
Expand All @@ -27,5 +32,7 @@ export default function usePoll(
return {
stop: pollRef.current.stop,
start: pollRef.current.start,
toggle: pollRef.current.toggle,
polling: pollRef.current.polling,
}
}
20 changes: 15 additions & 5 deletions packages/svelte/src/usePoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@ export default function usePoll(
keepAlive: false,
autoStart: true,
},
) {
const { stop, start } = router.poll(interval, requestOptions, {
): {
stop: VoidFunction
start: VoidFunction
toggle: VoidFunction
polling: () => boolean
} {
const poll = router.poll(interval, requestOptions, {
...options,
autoStart: false,
})

onMount(() => {
if (options.autoStart ?? true) {
start()
poll.start()
}
})

onDestroy(() => {
stop()
poll.stop()
})

return { stop, start }
return {
stop: () => poll.stop(),
start: () => poll.start(),
toggle: () => poll.toggle(),
polling: () => poll.polling(),
}
}
18 changes: 11 additions & 7 deletions packages/vue3/src/usePoll.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PollOptions, ReloadOptions, router } from '@inertiajs/core'
import { onMounted, onUnmounted } from 'vue'
import { computed, ComputedRef, onMounted, onUnmounted, ref } from 'vue'

export default function usePoll(
interval: number,
Expand All @@ -11,24 +11,28 @@ export default function usePoll(
): {
stop: VoidFunction
start: VoidFunction
toggle: VoidFunction
polling: ComputedRef<boolean>
} {
const { stop, start } = router.poll(interval, requestOptions, {
const poll = ref(router.poll(interval, requestOptions, {
...options,
autoStart: false,
})
}))

onMounted(() => {
if (options.autoStart ?? true) {
start()
poll.value.start()
}
})

onUnmounted(() => {
stop()
poll.value.stop()
})

return {
stop,
start,
stop: () => poll.value.stop(),
start: () => poll.value.start(),
toggle: () => poll.value.toggle(),
polling: computed(() => poll.value.polling()),
}
}
Loading