mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-06 20:55:23 -05:00
* 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
35 lines
828 B
TypeScript
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])
|
|
}
|