Skip to content

Commit

Permalink
fix(react): websocket message event handler (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVidra authored Jan 23, 2025
1 parent 01c8e34 commit b6ad4f6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
30 changes: 15 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion workspaces/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@flows/shared": "workspace:*"
},
"devDependencies": {
"@playwright/test": "^1.49.1",
"@playwright/test": "^1.50.0",
"@types/node": "^20",
"@types/react": "19.0.7",
"@types/react-dom": "19.0.3",
Expand Down
1 change: 1 addition & 0 deletions workspaces/e2e/tests/websocket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const run = (packageName: string) => {
route.fulfill({ json: { blocks: [] } });
});
await page.goto(`/${packageName}.html`);
await expect(page.locator("h1")).toBeVisible();
await expect(page.getByText("Hello world")).toBeHidden();
const payload: BlockUpdatesPayload = {
exitedBlockIds: [],
Expand Down
21 changes: 11 additions & 10 deletions workspaces/react/src/hooks/use-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export const useWebsocket = ({ url, onMessage, onOpen }: Props): void => {
onOpenRef.current = onOpen;
}, [onOpen]);

const onMessageRef = useRef(onMessage);
useEffect(() => {
onMessageRef.current = onMessage;
}, [onMessage]);
const handleMessage = useCallback((event: MessageEvent<unknown>) => {
onMessageRef.current(event);
}, []);

const connect = useCallback(() => {
if (cleanupRef.current) {
cleanupRef.current();
Expand All @@ -35,10 +43,12 @@ export const useWebsocket = ({ url, onMessage, onOpen }: Props): void => {
};
socket.addEventListener("open", handleOpen);
socket.addEventListener("close", handleClose);
socket.addEventListener("message", handleMessage);

const cleanup = (): void => {
socket.removeEventListener("open", handleOpen);
socket.removeEventListener("close", handleClose);
socket.removeEventListener("message", handleMessage);

if (socket.readyState === WebSocket.CONNECTING) {
socket.addEventListener("open", () => {
Expand All @@ -51,7 +61,7 @@ export const useWebsocket = ({ url, onMessage, onOpen }: Props): void => {

cleanupRef.current = cleanup;
return cleanup;
}, [url]);
}, [handleMessage, url]);

useEffect(() => {
const cleanup = connect();
Expand All @@ -75,15 +85,6 @@ export const useWebsocket = ({ url, onMessage, onOpen }: Props): void => {
};
}, [reconnectAttempts, ws, connect]);

useEffect(() => {
if (!ws) return;

ws.addEventListener("message", onMessage);
return () => {
ws.removeEventListener("message", onMessage);
};
}, [onMessage, ws]);

useEffect(() => {
return () => {
if (cleanupRef.current) {
Expand Down

0 comments on commit b6ad4f6

Please # to comment.