Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
86 changes: 86 additions & 0 deletions packages/react/src/hooks/use-echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type BroadcastDriver } from "laravel-echo";
import { useCallback, useEffect, useRef } from "react";
import { echo } from "../config";
import type {
BroadcastNotification,
Channel,
ChannelData,
ChannelReturnType,
Expand Down Expand Up @@ -163,6 +164,91 @@ export const useEcho = <
};
};

export const useEchoNotification = <
TPayload,
TDriver extends BroadcastDriver = BroadcastDriver,
>(
channelName: string,
callback: (payload: BroadcastNotification<TPayload>) => void = () => {},
event: string | string[] = [],
dependencies: any[] = [],
) => {
const result = useEcho<BroadcastNotification<TPayload>, TDriver, "private">(
channelName,
[],
callback,
dependencies,
"private",
);

const events = useRef(
toArray(event)
.map((e) => {
if (e.includes(".")) {
return [e, e.replace(/\./g, "\\")];
}

return [e, e.replace(/\\/g, ".")];
})
.flat(),
);
const listening = useRef(false);
const initialized = useRef(false);

const cb = useCallback(
(notification: BroadcastNotification<TPayload>) => {
if (!listening.current) {
return;
}

if (
events.current.length === 0 ||
events.current.includes(notification.type)
) {
callback(notification);
}
},
dependencies.concat(events.current).concat([callback]),
);

const listen = useCallback(() => {
if (listening.current) {
return;
}

if (!initialized.current) {
result.channel().notification(cb);
}

listening.current = true;
initialized.current = true;
}, [cb]);

const stopListening = useCallback(() => {
if (!listening.current) {
return;
}

listening.current = false;
}, [cb]);

useEffect(() => {
listen();
}, dependencies.concat(events.current));

return {
...result,
/**
* Stop listening for notification events
*/
stopListening,
/**
* Listen for notification events
*/
listen,
};
};

export const useEchoPresence = <
TPayload,
TDriver extends BroadcastDriver = BroadcastDriver,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { configureEcho, echo } from "./config/index";
export {
useEcho,
useEchoModel,
useEchoNotification,
useEchoPresence,
useEchoPublic,
} from "./hooks/use-echo";
5 changes: 5 additions & 0 deletions packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export type Channel = {
visibility: "private" | "public" | "presence";
};

export type BroadcastNotification<TPayload> = TPayload & {
id: string;
type: string;
};

export type ChannelReturnType<
T extends BroadcastDriver,
V extends Channel["visibility"],
Expand Down
Loading