Skip to content

Commit b42e98d

Browse files
Merge pull request #430 from laravel/optional-listener
Make events and listeners optional
2 parents c054de6 + 6954453 commit b42e98d

File tree

4 files changed

+87
-20
lines changed

4 files changed

+87
-20
lines changed

packages/react/src/hooks/use-echo.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export const useEcho = <
8080
TVisibility extends Channel["visibility"] = "private",
8181
>(
8282
channelName: string,
83-
event: string | string[],
84-
callback: (payload: TPayload) => void,
83+
event: string | string[] = [],
84+
callback: (payload: TPayload) => void = () => {},
8585
dependencies: any[] = [],
8686
visibility: TVisibility = "private" as TVisibility,
8787
) => {
@@ -173,8 +173,8 @@ export const useEchoPresence = <
173173
TDriver extends BroadcastDriver = BroadcastDriver,
174174
>(
175175
channelName: string,
176-
event: string | string[],
177-
callback: (payload: TPayload) => void,
176+
event: string | string[] = [],
177+
callback: (payload: TPayload) => void = () => {},
178178
dependencies: any[] = [],
179179
) => {
180180
return useEcho<TPayload, TDriver, "presence">(
@@ -191,8 +191,8 @@ export const useEchoPublic = <
191191
TDriver extends BroadcastDriver = BroadcastDriver,
192192
>(
193193
channelName: string,
194-
event: string | string[],
195-
callback: (payload: TPayload) => void,
194+
event: string | string[] = [],
195+
callback: (payload: TPayload) => void = () => {},
196196
dependencies: any[] = [],
197197
) => {
198198
return useEcho<TPayload, TDriver, "public">(
@@ -211,8 +211,8 @@ export const useEchoModel = <
211211
>(
212212
model: TModel,
213213
identifier: string | number,
214-
event: ModelEvents<TModel> | ModelEvents<TModel>[],
215-
callback: (payload: ModelPayload<TPayload>) => void,
214+
event: ModelEvents<TModel> | ModelEvents<TModel>[] = [],
215+
callback: (payload: ModelPayload<TPayload>) => void = () => {},
216216
dependencies: any[] = [],
217217
) => {
218218
return useEcho<ModelPayload<TPayload>, TDriver, "private">(

packages/react/tests/use-echo.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ describe("useEcho hook", async () => {
307307
const channel = echoInstance.private(channelName);
308308
expect(channel.listen).toHaveBeenCalledTimes(1);
309309
});
310+
311+
it("events and listeners are optional", async () => {
312+
const channelName = "test-channel";
313+
314+
const { result } = renderHook(() => echoModule.useEcho(channelName));
315+
316+
expect(result.current).toHaveProperty("channel");
317+
expect(result.current.channel).not.toBeNull();
318+
});
310319
});
311320

312321
describe("useEchoModel hook", async () => {
@@ -524,6 +533,18 @@ describe("useEchoModel hook", async () => {
524533
const channel = echoInstance.private(expectedChannelName);
525534
expect(channel.listen).toHaveBeenCalledWith(`.${event}`, mockCallback);
526535
});
536+
537+
it("events and listeners are optional", async () => {
538+
const model = "App.Models.User.Profile";
539+
const identifier = "123";
540+
541+
const { result } = renderHook(() =>
542+
echoModule.useEchoModel(model, identifier),
543+
);
544+
545+
expect(result.current).toHaveProperty("channel");
546+
expect(result.current.channel).not.toBeNull();
547+
});
527548
});
528549

529550
describe("useEchoPublic hook", async () => {
@@ -661,6 +682,17 @@ describe("useEchoPublic hook", async () => {
661682

662683
expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
663684
});
685+
686+
it("events and listeners are optional", async () => {
687+
const channelName = "test-channel";
688+
689+
const { result } = renderHook(() =>
690+
echoModule.useEchoPublic(channelName),
691+
);
692+
693+
expect(result.current).toHaveProperty("channel");
694+
expect(result.current.channel).not.toBeNull();
695+
});
664696
});
665697

666698
describe("useEchoPresence hook", async () => {
@@ -810,4 +842,15 @@ describe("useEchoPresence hook", async () => {
810842

811843
expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
812844
});
845+
846+
it("events and listeners are optional", async () => {
847+
const channelName = "test-channel";
848+
849+
const { result } = renderHook(() =>
850+
echoModule.useEchoPresence(channelName),
851+
);
852+
853+
expect(result.current).toHaveProperty("channel");
854+
expect(result.current.channel).not.toBeNull();
855+
});
813856
});

packages/vue/src/composables/useEcho.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export const useEcho = <
8080
TVisibility extends Channel["visibility"] = "private",
8181
>(
8282
channelName: string,
83-
event: string | string[],
84-
callback: (payload: TPayload) => void,
83+
event: string | string[] = [],
84+
callback: (payload: TPayload) => void = () => {},
8585
dependencies: any[] = [],
8686
visibility: TVisibility = "private" as TVisibility,
8787
) => {
@@ -193,8 +193,8 @@ export const useEchoPresence = <
193193
TDriver extends BroadcastDriver = BroadcastDriver,
194194
>(
195195
channelName: string,
196-
event: string | string[],
197-
callback: (payload: TPayload) => void,
196+
event: string | string[] = [],
197+
callback: (payload: TPayload) => void = () => {},
198198
dependencies: any[] = [],
199199
) => {
200200
return useEcho<TPayload, TDriver, "presence">(
@@ -211,8 +211,8 @@ export const useEchoPublic = <
211211
TDriver extends BroadcastDriver = BroadcastDriver,
212212
>(
213213
channelName: string,
214-
event: string | string[],
215-
callback: (payload: TPayload) => void,
214+
event: string | string[] = [],
215+
callback: (payload: TPayload) => void = () => {},
216216
dependencies: any[] = [],
217217
) => {
218218
return useEcho<TPayload, TDriver, "public">(

packages/vue/tests/useEcho.test.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const getUnConfiguredTestComponent = (
2929

3030
const getTestComponent = (
3131
channelName: string,
32-
event: string | string[],
33-
callback: (data: any) => void,
32+
event: string | string[] | undefined,
33+
callback: ((data: any) => void) | undefined,
3434
dependencies: any[] = [],
3535
visibility: "private" | "public" = "private",
3636
) => {
@@ -58,8 +58,8 @@ const getTestComponent = (
5858

5959
const getPublicTestComponent = (
6060
channelName: string,
61-
event: string | string[],
62-
callback: (data: any) => void,
61+
event: string | string[] | undefined,
62+
callback: ((data: any) => void) | undefined,
6363
dependencies: any[] = [],
6464
) => {
6565
const TestComponent = defineComponent({
@@ -80,8 +80,8 @@ const getPublicTestComponent = (
8080

8181
const getPresenceTestComponent = (
8282
channelName: string,
83-
event: string | string[],
84-
callback: (data: any) => void,
83+
event: string | string[] | undefined,
84+
callback: ((data: any) => void) | undefined,
8585
dependencies: any[] = [],
8686
) => {
8787
const TestComponent = defineComponent({
@@ -416,6 +416,14 @@ describe("useEcho hook", async () => {
416416
);
417417
});
418418
});
419+
420+
it("events and listeners are optional", async () => {
421+
const channelName = "test-channel";
422+
423+
wrapper = getTestComponent(channelName, undefined, undefined);
424+
425+
expect(wrapper.vm.channel).not.toBeNull();
426+
});
419427
});
420428

421429
describe("useEchoPublic hook", async () => {
@@ -538,6 +546,14 @@ describe("useEchoPublic hook", async () => {
538546

539547
expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
540548
});
549+
550+
it("events and listeners are optional", async () => {
551+
const channelName = "test-channel";
552+
553+
wrapper = getPublicTestComponent(channelName, undefined, undefined);
554+
555+
expect(wrapper.vm.channel).not.toBeNull();
556+
});
541557
});
542558

543559
describe("useEchoPresence hook", async () => {
@@ -673,4 +689,12 @@ describe("useEchoPresence hook", async () => {
673689

674690
expect(echoInstance.leave).toHaveBeenCalledWith(channelName);
675691
});
692+
693+
it("events and listeners are optional", async () => {
694+
const channelName = "test-channel";
695+
696+
wrapper = getPresenceTestComponent(channelName, undefined, undefined);
697+
698+
expect(wrapper.vm.channel).not.toBeNull();
699+
});
676700
});

0 commit comments

Comments
 (0)