overleaf/services/web/frontend/js/shared/hooks/user-channel/use-user-channel.ts

16 lines
399 B
TypeScript
Raw Normal View History

import { useEffect, useRef } from 'react'
export const useUserChannel = (): BroadcastChannel | null => {
const channelRef = useRef<BroadcastChannel | null>(null)
if (channelRef.current === null && 'BroadcastChannel' in window) {
channelRef.current = new BroadcastChannel('user')
}
useEffect(() => {
return () => channelRef.current?.close()
}, [])
return channelRef.current
}