dcs-retribution/client/src/hooks/useEventSteam.ts
Dan Albert 8126635bd9
Add missing type annotation.
Caught by newer versions of typescript.
2022-10-02 19:56:50 +02:00

26 lines
642 B
TypeScript

import { handleStreamedEvents } from "../api/eventstream";
import { useAppDispatch } from "../app/hooks";
import { useSocket } from "./useSocket";
import { useCallback, useEffect } from "react";
export const useEventStream = () => {
const ws = useSocket();
const dispatch = useAppDispatch();
const onMessage = useCallback(
(message: MessageEvent) => {
handleStreamedEvents(dispatch, JSON.parse(message.data));
},
[dispatch]
);
useEffect(() => {
ws.addEventListener("message", onMessage);
return () => {
ws.removeEventListener("message", onMessage);
};
});
};
export default useEventStream;