Files
sim/apps/sim/hooks/use-stream-cleanup.ts
Waleed 4787909851 improvement(kb): removed zustand cache syncing in kb, added chunk text tokenizer (#2647)
* improvement(kb): removed zustand cache syncing in kb, added chunk text tokenizer

* removed dead code

* removed redundant hook

* remove unused hook

* remove alert notification and use simple error

* added more popover actions

* removed debug instrumentation

* remove extraneous comments

* removed unused handler
2025-12-30 20:15:08 -08:00

35 lines
828 B
TypeScript

'use client'
import { useCallback, useEffect } from 'react'
/**
* Generic hook to handle stream cleanup on page unload and component unmount
* This ensures that ongoing streams are properly terminated when:
* - Page is refreshed
* - User navigates away
* - Component unmounts
* - Tab is closed
*/
export function useStreamCleanup(cleanup: () => void) {
const stableCleanup = useCallback(() => {
try {
cleanup()
} catch (error) {
console.warn('Error during stream cleanup:', error)
}
}, [cleanup])
useEffect(() => {
const handleBeforeUnload = () => {
stableCleanup()
}
window.addEventListener('beforeunload', handleBeforeUnload)
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload)
stableCleanup()
}
}, [stableCleanup])
}