Frontend: Composables useConfirm/useChat/usePolling; Guide-Chat abbrechbar
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
44
frontend/src/composables/usePolling.js
Normal file
44
frontend/src/composables/usePolling.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { onUnmounted } from 'vue'
|
||||
|
||||
// Polling mit Visibility-Pause: Tab unsichtbar → stoppen; wieder sichtbar →
|
||||
// sofortiger Tick, dann weiter, falls isActive(). Stoppt selbst, sobald
|
||||
// isActive() nach einem Tick false liefert.
|
||||
export function usePolling(tick, isActive, interval = 3000) {
|
||||
let timer = null
|
||||
|
||||
function stop() {
|
||||
if (timer) {
|
||||
clearInterval(timer)
|
||||
timer = null
|
||||
}
|
||||
}
|
||||
|
||||
function start() {
|
||||
stop()
|
||||
timer = setInterval(async () => {
|
||||
await tick()
|
||||
if (!isActive()) stop()
|
||||
}, interval)
|
||||
}
|
||||
|
||||
function running() {
|
||||
return !!timer
|
||||
}
|
||||
|
||||
async function onVisibility() {
|
||||
if (document.hidden) {
|
||||
stop()
|
||||
} else {
|
||||
await tick()
|
||||
if (isActive()) start()
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('visibilitychange', onVisibility)
|
||||
onUnmounted(() => {
|
||||
stop()
|
||||
document.removeEventListener('visibilitychange', onVisibility)
|
||||
})
|
||||
|
||||
return { start, stop, running }
|
||||
}
|
||||
Reference in New Issue
Block a user