Skip to content

Make events and listeners optional #430

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

Merged
merged 1 commit into from
May 13, 2025
Merged
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
16 changes: 8 additions & 8 deletions packages/react/src/hooks/use-echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export const useEcho = <
TVisibility extends Channel["visibility"] = "private",
>(
channelName: string,
event: string | string[],
callback: (payload: TPayload) => void,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
dependencies: any[] = [],
visibility: TVisibility = "private" as TVisibility,
) => {
Expand Down Expand Up @@ -173,8 +173,8 @@ export const useEchoPresence = <
TDriver extends BroadcastDriver = BroadcastDriver,
>(
channelName: string,
event: string | string[],
callback: (payload: TPayload) => void,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<TPayload, TDriver, "presence">(
Expand All @@ -191,8 +191,8 @@ export const useEchoPublic = <
TDriver extends BroadcastDriver = BroadcastDriver,
>(
channelName: string,
event: string | string[],
callback: (payload: TPayload) => void,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<TPayload, TDriver, "public">(
Expand All @@ -211,8 +211,8 @@ export const useEchoModel = <
>(
model: TModel,
identifier: string | number,
event: ModelEvents<TModel> | ModelEvents<TModel>[],
callback: (payload: ModelPayload<TPayload>) => void,
event: ModelEvents<TModel> | ModelEvents<TModel>[] = [],
callback: (payload: ModelPayload<TPayload>) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<ModelPayload<TPayload>, TDriver, "private">(
Expand Down
43 changes: 43 additions & 0 deletions packages/react/tests/use-echo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ describe("useEcho hook", async () => {
const channel = echoInstance.private(channelName);
expect(channel.listen).toHaveBeenCalledTimes(1);
});

it("events and listeners are optional", async () => {
const channelName = "test-channel";

const { result } = renderHook(() => echoModule.useEcho(channelName));

expect(result.current).toHaveProperty("channel");
expect(result.current.channel).not.toBeNull();
});
});

describe("useEchoModel hook", async () => {
Expand Down Expand Up @@ -524,6 +533,18 @@ describe("useEchoModel hook", async () => {
const channel = echoInstance.private(expectedChannelName);
expect(channel.listen).toHaveBeenCalledWith(`.${event}`, mockCallback);
});

it("events and listeners are optional", async () => {
const model = "App.Models.User.Profile";
const identifier = "123";

const { result } = renderHook(() =>
echoModule.useEchoModel(model, identifier),
);

expect(result.current).toHaveProperty("channel");
expect(result.current.channel).not.toBeNull();
});
});

describe("useEchoPublic hook", async () => {
Expand Down Expand Up @@ -661,6 +682,17 @@ describe("useEchoPublic hook", async () => {

expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
});

it("events and listeners are optional", async () => {
const channelName = "test-channel";

const { result } = renderHook(() =>
echoModule.useEchoPublic(channelName),
);

expect(result.current).toHaveProperty("channel");
expect(result.current.channel).not.toBeNull();
});
});

describe("useEchoPresence hook", async () => {
Expand Down Expand Up @@ -810,4 +842,15 @@ describe("useEchoPresence hook", async () => {

expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
});

it("events and listeners are optional", async () => {
const channelName = "test-channel";

const { result } = renderHook(() =>
echoModule.useEchoPresence(channelName),
);

expect(result.current).toHaveProperty("channel");
expect(result.current.channel).not.toBeNull();
});
});
12 changes: 6 additions & 6 deletions packages/vue/src/composables/useEcho.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export const useEcho = <
TVisibility extends Channel["visibility"] = "private",
>(
channelName: string,
event: string | string[],
callback: (payload: TPayload) => void,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
dependencies: any[] = [],
visibility: TVisibility = "private" as TVisibility,
) => {
Expand Down Expand Up @@ -193,8 +193,8 @@ export const useEchoPresence = <
TDriver extends BroadcastDriver = BroadcastDriver,
>(
channelName: string,
event: string | string[],
callback: (payload: TPayload) => void,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<TPayload, TDriver, "presence">(
Expand All @@ -211,8 +211,8 @@ export const useEchoPublic = <
TDriver extends BroadcastDriver = BroadcastDriver,
>(
channelName: string,
event: string | string[],
callback: (payload: TPayload) => void,
event: string | string[] = [],
callback: (payload: TPayload) => void = () => {},
dependencies: any[] = [],
) => {
return useEcho<TPayload, TDriver, "public">(
Expand Down
36 changes: 30 additions & 6 deletions packages/vue/tests/useEcho.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const getUnConfiguredTestComponent = (

const getTestComponent = (
channelName: string,
event: string | string[],
callback: (data: any) => void,
event: string | string[] | undefined,
callback: ((data: any) => void) | undefined,
dependencies: any[] = [],
visibility: "private" | "public" = "private",
) => {
Expand Down Expand Up @@ -58,8 +58,8 @@ const getTestComponent = (

const getPublicTestComponent = (
channelName: string,
event: string | string[],
callback: (data: any) => void,
event: string | string[] | undefined,
callback: ((data: any) => void) | undefined,
dependencies: any[] = [],
) => {
const TestComponent = defineComponent({
Expand All @@ -80,8 +80,8 @@ const getPublicTestComponent = (

const getPresenceTestComponent = (
channelName: string,
event: string | string[],
callback: (data: any) => void,
event: string | string[] | undefined,
callback: ((data: any) => void) | undefined,
dependencies: any[] = [],
) => {
const TestComponent = defineComponent({
Expand Down Expand Up @@ -416,6 +416,14 @@ describe("useEcho hook", async () => {
);
});
});

it("events and listeners are optional", async () => {
const channelName = "test-channel";

wrapper = getTestComponent(channelName, undefined, undefined);

expect(wrapper.vm.channel).not.toBeNull();
});
});

describe("useEchoPublic hook", async () => {
Expand Down Expand Up @@ -538,6 +546,14 @@ describe("useEchoPublic hook", async () => {

expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
});

it("events and listeners are optional", async () => {
const channelName = "test-channel";

wrapper = getPublicTestComponent(channelName, undefined, undefined);

expect(wrapper.vm.channel).not.toBeNull();
});
});

describe("useEchoPresence hook", async () => {
Expand Down Expand Up @@ -673,4 +689,12 @@ describe("useEchoPresence hook", async () => {

expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
});

it("events and listeners are optional", async () => {
const channelName = "test-channel";

wrapper = getPresenceTestComponent(channelName, undefined, undefined);

expect(wrapper.vm.channel).not.toBeNull();
});
});