initial commit
This commit is contained in:
69
app/composables/useLiveSessions.js
Normal file
69
app/composables/useLiveSessions.js
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Fetches active live sessions (camera + location sharing) and refreshes on an interval.
|
||||
* Only runs when the app is focused so we don't poll in the background.
|
||||
*/
|
||||
|
||||
const POLL_MS = 1500
|
||||
|
||||
export function useLiveSessions() {
|
||||
const { data: sessions, refresh } = useAsyncData(
|
||||
'live-sessions',
|
||||
async () => {
|
||||
try {
|
||||
const result = await $fetch('/api/live')
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('[useLiveSessions] Fetched sessions:', result.map(s => ({
|
||||
id: s.id,
|
||||
label: s.label,
|
||||
hasStream: s.hasStream,
|
||||
})))
|
||||
}
|
||||
return result
|
||||
}
|
||||
catch (err) {
|
||||
const msg = err?.message ?? String(err)
|
||||
const status = err?.statusCode ?? err?.status
|
||||
console.error('[useLiveSessions] Failed to fetch sessions:', status ? `${status}: ${msg}` : msg)
|
||||
return []
|
||||
}
|
||||
},
|
||||
{ default: () => [] },
|
||||
)
|
||||
|
||||
const pollInterval = ref(null)
|
||||
|
||||
function startPolling() {
|
||||
if (pollInterval.value) return
|
||||
refresh() // Fetch immediately so new sessions show without waiting for first interval
|
||||
pollInterval.value = setInterval(() => {
|
||||
refresh()
|
||||
}, POLL_MS)
|
||||
}
|
||||
|
||||
function stopPolling() {
|
||||
if (pollInterval.value) {
|
||||
clearInterval(pollInterval.value)
|
||||
pollInterval.value = null
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (typeof document === 'undefined') return
|
||||
const onFocus = () => startPolling()
|
||||
const onBlur = () => stopPolling()
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
onFocus()
|
||||
refresh() // Fresh data when returning to tab
|
||||
}
|
||||
else onBlur()
|
||||
})
|
||||
if (document.visibilityState === 'visible') startPolling()
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
stopPolling()
|
||||
})
|
||||
|
||||
return { sessions, refresh, startPolling, stopPolling }
|
||||
}
|
||||
Reference in New Issue
Block a user