[web-console] Make status polling resilient to hiding the page by Karakatiza666 · Pull Request #6079 · feldera/feldera · GitHub
Skip to content
Merged
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
18 changes: 10 additions & 8 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js-packages/web-console/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { closedIntervalAction } from '$lib/functions/common/promise'

/**
* Runs the function on initial call
* @param f
* @param durationMs
* @param offsetMs
* @returns
* Runs `f` immediately, then repeatedly on an interval, exposing the latest return value
* as reactive `.current`.
*
* Built on {@link closedIntervalAction}, so polling is paused while the document is
* hidden and async callbacks are awaited before the next tick is scheduled.
*/
export const useInterval = <T>(f: () => T, durationMs: number, offsetMs?: number) => {
let state = $state(f())
let interval: Timer
$effect.pre(() => {
if (offsetMs) {
setTimeout(() => (interval = setInterval(() => (state = f()), durationMs)), offsetMs)

const action = async () => {
const result = f()
if (result && typeof (result as { then?: unknown }).then === 'function') {
state = await (result as unknown as Promise<T>)
} else {
interval = setInterval(() => (state = f()), durationMs)
state = result
}
return () => {
clearInterval(interval)
}
})
}

$effect.pre(() => closedIntervalAction(action, durationMs, { initialDelayMs: offsetMs }))

return {
get current() {
return state
Expand Down
Loading
Loading