Skip to content

[Feature Request] React: Provide event name in useEcho callback #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
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 @@ -75,7 +75,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 @@ -102,7 +102,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 @@ -114,7 +116,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 @@ -169,7 +173,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 @@ -187,7 +191,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 @@ -207,7 +211,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 @@ -122,18 +122,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 @@ -190,7 +196,7 @@ describe("useEcho hook", async () => {

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

Expand Down Expand Up @@ -251,15 +257,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 @@ -274,7 +289,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 @@ -387,22 +405,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 @@ -531,7 +549,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 @@ -601,18 +622,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 @@ -755,18 +782,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