Skip to content

Commit 50fa1d0

Browse files
committed
refactor: enhance listenChannel function and update notification handling
- Refactored the listenChannel function to accept an object of event handlers instead of separate parameters, improving flexibility and readability. - Updated the RealtimeProvider to use the new handler structure for invalidating notification queries upon receiving new notifications.
1 parent 329bc8a commit 50fa1d0

2 files changed

Lines changed: 4 additions & 16 deletions

File tree

src/components/providers/RealtimeProvider.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ export function RealtimeProvider({ children }: PropsWithChildren) {
2828
if (!userId) return;
2929

3030
const channelName = `private-user.${userId}`;
31-
return listenChannel(channelName, "notification.new", () => {
31+
const invalidate = () => {
3232
queryClient.invalidateQueries({ queryKey: ["my-notifications"] });
3333
queryClient.invalidateQueries({
3434
queryKey: ["unread-notification-count"],
3535
});
36+
};
37+
return listenChannel(channelName, {
38+
"notification.new": invalidate,
3639
});
3740
}, [userId, queryClient]);
3841

src/lib/pusher/pusher.client.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,7 @@ type EventHandlers = Record<string, () => void>;
2828
export function listenChannel(
2929
channel: string,
3030
handlers: EventHandlers,
31-
): () => void;
32-
export function listenChannel(
33-
channel: string,
34-
event: string,
35-
handler: () => void,
36-
): () => void;
37-
export function listenChannel(
38-
channel: string,
39-
eventOrHandlers: string | EventHandlers,
40-
handler?: () => void,
4131
): () => void {
42-
const handlers: EventHandlers =
43-
typeof eventOrHandlers === "string" && handler !== undefined
44-
? { [eventOrHandlers]: handler }
45-
: (eventOrHandlers as EventHandlers);
46-
4732
const pusher = getPusherClient();
4833
if (!pusher) {
4934
return () => {};

0 commit comments

Comments
 (0)