import { useContext, useEffect } from 'react' import { WebSocketContext, WebSocketContextValue } from './WebSocketProvider' import type { ClientToServerEvents, ServerToClientEvents, } from 'wasp/server/webSocket' // PUBLIC API export type ServerToClientPayload = Parameters[0] // PUBLIC API export type ClientToServerPayload = Parameters[0] // PUBLIC API export function useSocket(): WebSocketContextValue { return useContext(WebSocketContext) } // PUBLIC API export function useSocketListener( event: Event, handler: ServerToClientEvents[Event] ): void { const { socket } = useContext(WebSocketContext) useEffect(() => { // Casting to `keyof ServerToClientEvents` is necessary because TypeScript // reports the handler function as incompatible with the event type. // See https://github.com/wasp-lang/wasp/pull/1203#discussion_r1232068898 // We are wrapping it in `Extract<...>` due to Typescript infering string | number // in the case of default events being used. type AllowedEvents = Extract; socket.on(event as AllowedEvents, handler) return () => { socket.off(event as AllowedEvents, handler) } }, [event, handler]) }