Skip to content

Commit 0ef148f

Browse files
committed
echo notification vue
1 parent f4de8d0 commit 0ef148f

File tree

6 files changed

+387
-5
lines changed

6 files changed

+387
-5
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { type BroadcastDriver } from "laravel-echo";
22
import { useCallback, useEffect, useRef } from "react";
33
import { echo } from "../config";
44
import type {
5+
BroadcastNotification,
56
Channel,
67
ChannelData,
78
ChannelReturnType,
89
Connection,
910
ModelEvents,
1011
ModelPayload,
11-
Notification,
1212
} from "../types";
1313
import { toArray } from "../util";
1414

@@ -169,11 +169,11 @@ export const useEchoNotification = <
169169
TDriver extends BroadcastDriver = BroadcastDriver,
170170
>(
171171
channelName: string,
172-
callback: (payload: Notification<TPayload>) => void = () => {},
172+
callback: (payload: BroadcastNotification<TPayload>) => void = () => {},
173173
event: string | string[] = [],
174174
dependencies: any[] = [],
175175
) => {
176-
const result = useEcho<Notification<TPayload>, TDriver, "private">(
176+
const result = useEcho<BroadcastNotification<TPayload>, TDriver, "private">(
177177
channelName,
178178
[],
179179
callback,
@@ -186,7 +186,7 @@ export const useEchoNotification = <
186186
const initialized = useRef(false);
187187

188188
const cb = useCallback(
189-
(notification: Notification<TPayload>) => {
189+
(notification: BroadcastNotification<TPayload>) => {
190190
if (!listening.current) {
191191
return;
192192
}

packages/react/src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export type Channel = {
1616
visibility: "private" | "public" | "presence";
1717
};
1818

19-
export type Notification<TPayload> = TPayload & { id: string; type: string };
19+
export type BroadcastNotification<TPayload> = TPayload & {
20+
id: string;
21+
type: string;
22+
};
2023

2124
export type ChannelReturnType<
2225
T extends BroadcastDriver,

packages/vue/src/composables/useEcho.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type BroadcastDriver } from "laravel-echo";
22
import { onMounted, onUnmounted, ref, watch } from "vue";
33
import { echo } from "../config";
44
import type {
5+
BroadcastNotification,
56
Channel,
67
ChannelData,
78
ChannelReturnType,
@@ -178,6 +179,75 @@ export const useEcho = <
178179
};
179180
};
180181

182+
export const useEchoNotification = <
183+
TPayload,
184+
TDriver extends BroadcastDriver = BroadcastDriver,
185+
>(
186+
channelName: string,
187+
callback: (payload: BroadcastNotification<TPayload>) => void = () => {},
188+
event: string | string[] = [],
189+
dependencies: any[] = [],
190+
) => {
191+
const result = useEcho<BroadcastNotification<TPayload>, TDriver, "private">(
192+
channelName,
193+
[],
194+
callback,
195+
dependencies,
196+
"private",
197+
);
198+
199+
const events = toArray(event);
200+
let listening = false;
201+
let initialized = false;
202+
203+
const cb = (notification: BroadcastNotification<TPayload>) => {
204+
if (!listening) {
205+
return;
206+
}
207+
208+
if (events.length === 0 || events.includes(notification.type)) {
209+
callback(notification);
210+
}
211+
};
212+
213+
const listen = () => {
214+
if (listening) {
215+
return;
216+
}
217+
218+
if (!initialized) {
219+
result.channel().notification(cb);
220+
}
221+
222+
listening = true;
223+
initialized = true;
224+
};
225+
226+
const stopListening = () => {
227+
if (!listening) {
228+
return;
229+
}
230+
231+
listening = false;
232+
};
233+
234+
onMounted(() => {
235+
listen();
236+
});
237+
238+
return {
239+
...result,
240+
/**
241+
* Stop listening for notification events
242+
*/
243+
stopListening,
244+
/**
245+
* Listen for notification events
246+
*/
247+
listen,
248+
};
249+
};
250+
181251
export const useEchoPresence = <
182252
TPayload,
183253
TDriver extends BroadcastDriver = BroadcastDriver,

packages/vue/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {
22
useEcho,
33
useEchoModel,
4+
useEchoNotification,
45
useEchoPresence,
56
useEchoPublic,
67
} from "./composables/useEcho";

packages/vue/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export type Channel = {
1616
visibility: "private" | "public" | "presence";
1717
};
1818

19+
export type BroadcastNotification<TPayload> = TPayload & {
20+
id: string;
21+
type: string;
22+
};
23+
1924
export type ChannelReturnType<
2025
T extends BroadcastDriver,
2126
V extends Channel["visibility"],

0 commit comments

Comments
 (0)