Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/react/src/hooks/use-echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const useEcho = <
>(
channelName: string,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
callback: (payload: TPayload, event: string) => void = () => {},
dependencies: any[] = [],
visibility: TVisibility = "private" as TVisibility,
) => {
Expand All @@ -103,7 +103,9 @@ export const useEcho = <
}

events.forEach((e) => {
subscription.current.stopListening(e, callbackFunc);
subscription.current.stopListening(e, (payload: TPayload) =>
callbackFunc(payload, e),
);
});

listening.current = false;
Expand All @@ -115,7 +117,9 @@ export const useEcho = <
}

events.forEach((e) => {
subscription.current.listen(e, callbackFunc);
subscription.current.listen(e, (payload: TPayload) =>
callbackFunc(payload, e),
);
});

listening.current = true;
Expand Down Expand Up @@ -255,7 +259,7 @@ export const useEchoPresence = <
>(
channelName: string,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
callback: (payload: TPayload, event: string) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<TPayload, TDriver, "presence">(
Expand All @@ -273,7 +277,7 @@ export const useEchoPublic = <
>(
channelName: string,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
callback: (payload: TPayload, event: string) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<TPayload, TDriver, "public">(
Expand All @@ -293,7 +297,10 @@ export const useEchoModel = <
model: TModel,
identifier: string | number,
event: ModelEvents<TModel> | ModelEvents<TModel>[] = [],
callback: (payload: ModelPayload<TPayload>) => void = () => {},
callback: (
payload: ModelPayload<TPayload>,
event: string,
) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<ModelPayload<TPayload>, TDriver, "private">(
Expand Down
77 changes: 55 additions & 22 deletions packages/react/tests/use-echo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,24 @@ describe("useEcho hook", async () => {

const channel = echoInstance.private(channelName);

expect(channel.listen).toHaveBeenCalledWith(events[0], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(events[1], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
events[0],
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
events[1],
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
events[0],
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
events[1],
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -191,7 +197,7 @@ describe("useEcho hook", async () => {

expect(echoInstance.private(channelName).listen).toHaveBeenCalledWith(
event,
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -252,15 +258,24 @@ describe("useEcho hook", async () => {

const channel = echoInstance.private(channelName);

expect(channel.listen).toHaveBeenCalledWith(event, mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
event,
expect.any(Function),
);

result.current.stopListening();

expect(channel.stopListening).toHaveBeenCalledWith(event, mockCallback);
expect(channel.stopListening).toHaveBeenCalledWith(
event,
expect.any(Function),
);

result.current.listen();

expect(channel.listen).toHaveBeenCalledWith(event, mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
event,
expect.any(Function),
);
});

it("can manually stop listening to events", async () => {
Expand All @@ -275,7 +290,10 @@ describe("useEcho hook", async () => {
result.current.stopListening();

const channel = echoInstance.private(channelName);
expect(channel.stopListening).toHaveBeenCalledWith(event, mockCallback);
expect(channel.stopListening).toHaveBeenCalledWith(
event,
expect.any(Function),
);
});

it("stopListening is a no-op when not listening", async () => {
Expand Down Expand Up @@ -388,22 +406,22 @@ describe("useEchoModel hook", async () => {

expect(channel.listen).toHaveBeenCalledWith(
`.${events[0]}`,
mockCallback,
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
`.${events[1]}`,
mockCallback,
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
`.${events[0]}`,
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
`.${events[1]}`,
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -532,7 +550,10 @@ describe("useEchoModel hook", async () => {
expect(echoInstance.private).toHaveBeenCalledWith(expectedChannelName);

const channel = echoInstance.private(expectedChannelName);
expect(channel.listen).toHaveBeenCalledWith(`.${event}`, mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
`.${event}`,
expect.any(Function),
);
});

it("events and listeners are optional", async () => {
Expand Down Expand Up @@ -602,18 +623,24 @@ describe("useEchoPublic hook", async () => {

const channel = echoInstance.channel(channelName);

expect(channel.listen).toHaveBeenCalledWith(events[0], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(events[1], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
events[0],
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
events[1],
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
events[0],
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
events[1],
mockCallback,
expect.any(Function),
);
});

Expand Down Expand Up @@ -756,18 +783,24 @@ describe("useEchoPresence hook", async () => {

const channel = echoInstance.join(channelName);

expect(channel.listen).toHaveBeenCalledWith(events[0], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(events[1], mockCallback);
expect(channel.listen).toHaveBeenCalledWith(
events[0],
expect.any(Function),
);
expect(channel.listen).toHaveBeenCalledWith(
events[1],
expect.any(Function),
);

expect(() => unmount()).not.toThrow();

expect(channel.stopListening).toHaveBeenCalledWith(
events[0],
mockCallback,
expect.any(Function),
);
expect(channel.stopListening).toHaveBeenCalledWith(
events[1],
mockCallback,
expect.any(Function),
);
});

Expand Down