From 6aa1e18beba3e396578ce7d7459b7c9a56b55cdc Mon Sep 17 00:00:00 2001 From: musammilvilayil <140297924+musammilvilayil@users.noreply.github.com> Date: Sat, 15 Aug 2026 12:40:24 +0530 Subject: [PATCH 1/5] fix pubsub subscription rerenders --- src/Utils/pubsubContext.tsx | 83 +++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/src/Utils/pubsubContext.tsx b/src/Utils/pubsubContext.tsx index 82364be9a00..d56e6d48903 100644 --- a/src/Utils/pubsubContext.tsx +++ b/src/Utils/pubsubContext.tsx @@ -1,73 +1,68 @@ -import { createContext, useContext, useState } from "react"; +import { + createContext, + useCallback, + useContext, + useMemo, + useRef, +} from "react"; export type Handler = (message: unknown) => Promise; type PubSubContextType = { subscribe: (topic: string, handler: Handler) => void; unsubscribe: (topic: string, handler: Handler) => void; publish: (topic: string, message: unknown) => void; - subscribers: Record>; - setSubscribers: React.Dispatch< - React.SetStateAction>> - >; }; const PubSubContext = createContext(null); export const PubSubProvider = ({ children }: { children: React.ReactNode }) => { - const [subscribers, setSubscribers] = useState>>( - {}, - ); + const subscribersRef = useRef>>({}); - const subscribe = (topic: string, handler: Handler) => { - setSubscribers((prev) => ({ - ...prev, - [topic]: new Set([...(prev[topic] || []), handler]), - })); - }; - - const unsubscribe = (topic: string, handler: Handler) => { - setSubscribers((prev) => { - const handlers = prev[topic]; - if (!handlers) { - return prev; - } + const subscribe = useCallback((topic: string, handler: Handler) => { + const handlers = subscribersRef.current[topic]; - const newHandlers = new Set(handlers); - newHandlers.delete(handler); + if (handlers) { + handlers.add(handler); + return; + } - if (newHandlers.size === 0) { - const { [topic]: _, ...rest } = prev; - return rest; - } + subscribersRef.current[topic] = new Set([handler]); + }, []); - return { - ...prev, - [topic]: newHandlers, - }; - }); - }; + const unsubscribe = useCallback((topic: string, handler: Handler) => { + const handlers = subscribersRef.current[topic]; + if (!handlers) { + return; + } + + handlers.delete(handler); + + if (handlers.size === 0) { + delete subscribersRef.current[topic]; + } + }, []); - const publish = (topic: string, message: unknown) => { - if (!subscribers[topic]) { + const publish = useCallback((topic: string, message: unknown) => { + const handlers = subscribersRef.current[topic]; + if (!handlers) { return; } - subscribers[topic].forEach(async (handler) => { + handlers.forEach(async (handler) => { try { await handler(message); } catch (error) { console.error(`Handler failed for topic ${topic}:`, error); } }); - }; - - return ( - - {children} - + }, []); + + const value = useMemo( + () => ({ subscribe, unsubscribe, publish }), + [publish, subscribe, unsubscribe], ); + + return {children}; }; export const usePubSub = () => { From b744dc4a705bf89f1d2caf05697a931adc4f0014 Mon Sep 17 00:00:00 2001 From: musammilvilayil <140297924+musammilvilayil@users.noreply.github.com> Date: Sat, 15 Aug 2026 12:50:10 +0530 Subject: [PATCH 2/5] chore: align PubSub context type with style guide --- src/Utils/pubsubContext.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utils/pubsubContext.tsx b/src/Utils/pubsubContext.tsx index d56e6d48903..e6a61e5929f 100644 --- a/src/Utils/pubsubContext.tsx +++ b/src/Utils/pubsubContext.tsx @@ -7,11 +7,11 @@ import { } from "react"; export type Handler = (message: unknown) => Promise; -type PubSubContextType = { +interface PubSubContextType { subscribe: (topic: string, handler: Handler) => void; unsubscribe: (topic: string, handler: Handler) => void; publish: (topic: string, message: unknown) => void; -}; +} const PubSubContext = createContext(null); From 2588a18220c47c8578996e58268da1dc792b1698 Mon Sep 17 00:00:00 2001 From: musammilvilayil <140297924+musammilvilayil@users.noreply.github.com> Date: Thu, 20 Aug 2026 13:40:12 +0530 Subject: [PATCH 3/5] refactor: remove PubSub provider from app --- src/App.tsx | 57 +++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 10f0faf459f..6bf7c73505d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,7 +15,6 @@ import { QueryClientProvider } from "@tanstack/react-query"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; import { useLocationChange } from "raviger"; import { lazy, Suspense, useEffect } from "react"; -import { PubSubProvider } from "./Utils/pubsubContext"; const PatientRouter = lazy(() => import("@/Routers/PatientRouter")); const AppRouter = lazy(() => import("@/Routers/AppRouter")); @@ -39,37 +38,35 @@ const App = () => { }> - - - - - } - otpAuthorized={ - }> - - - } - > + + + + } + otpAuthorized={ }> - + - - - - - - - + } + > + }> + + + + + + + + {/* Devtools are not included in production builds by default */} From b9188f2e2baf3b67c2f92f07bc8a798f04dddcc0 Mon Sep 17 00:00:00 2001 From: musammilvilayil <140297924+musammilvilayil@users.noreply.github.com> Date: Thu, 20 Aug 2026 13:41:16 +0530 Subject: [PATCH 4/5] refactor: remove PubSub usage from patient registration --- src/pages/PublicAppointments/PatientRegistration.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/pages/PublicAppointments/PatientRegistration.tsx b/src/pages/PublicAppointments/PatientRegistration.tsx index c174dae919f..d22274bf565 100644 --- a/src/pages/PublicAppointments/PatientRegistration.tsx +++ b/src/pages/PublicAppointments/PatientRegistration.tsx @@ -28,7 +28,6 @@ import { usePatientContext } from "@/hooks/usePatientUser"; import { GENDERS, GENDER_TYPES } from "@/common/constants"; import { validateName } from "@/common/validation"; -import { usePubSub } from "@/Utils/pubsubContext"; import mutate from "@/Utils/request/mutate"; import { dateQueryString } from "@/Utils/utils"; import validators from "@/Utils/validators"; @@ -52,8 +51,6 @@ export default function PublicPatientRegistration( const queryClient = useQueryClient(); - const { publish } = usePubSub(); - const patientUserContext = usePatientContext(); const tokenData = patientUserContext?.tokenData; @@ -146,7 +143,6 @@ export default function PublicPatientRegistration( queryClient.invalidateQueries({ queryKey: ["patients"], }); - publish("patient:upsert", data); createAppointment({ patient: data.id, note: reason ?? "", From f641105668429cc5a7ea07b0f756713df5dc5afb Mon Sep 17 00:00:00 2001 From: musammilvilayil <140297924+musammilvilayil@users.noreply.github.com> Date: Thu, 20 Aug 2026 13:41:22 +0530 Subject: [PATCH 5/5] refactor: remove obsolete PubSub implementation --- src/Utils/pubsubContext.tsx | 76 ------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 src/Utils/pubsubContext.tsx diff --git a/src/Utils/pubsubContext.tsx b/src/Utils/pubsubContext.tsx deleted file mode 100644 index e6a61e5929f..00000000000 --- a/src/Utils/pubsubContext.tsx +++ /dev/null @@ -1,76 +0,0 @@ -import { - createContext, - useCallback, - useContext, - useMemo, - useRef, -} from "react"; - -export type Handler = (message: unknown) => Promise; -interface PubSubContextType { - subscribe: (topic: string, handler: Handler) => void; - unsubscribe: (topic: string, handler: Handler) => void; - publish: (topic: string, message: unknown) => void; -} - -const PubSubContext = createContext(null); - -export const PubSubProvider = ({ children }: { children: React.ReactNode }) => { - const subscribersRef = useRef>>({}); - - const subscribe = useCallback((topic: string, handler: Handler) => { - const handlers = subscribersRef.current[topic]; - - if (handlers) { - handlers.add(handler); - return; - } - - subscribersRef.current[topic] = new Set([handler]); - }, []); - - const unsubscribe = useCallback((topic: string, handler: Handler) => { - const handlers = subscribersRef.current[topic]; - if (!handlers) { - return; - } - - handlers.delete(handler); - - if (handlers.size === 0) { - delete subscribersRef.current[topic]; - } - }, []); - - const publish = useCallback((topic: string, message: unknown) => { - const handlers = subscribersRef.current[topic]; - if (!handlers) { - return; - } - - handlers.forEach(async (handler) => { - try { - await handler(message); - } catch (error) { - console.error(`Handler failed for topic ${topic}:`, error); - } - }); - }, []); - - const value = useMemo( - () => ({ subscribe, unsubscribe, publish }), - [publish, subscribe, unsubscribe], - ); - - return {children}; -}; - -export const usePubSub = () => { - const context = useContext(PubSubContext); - - if (!context) { - throw new Error("usePubSub must be used within PubSubProvider"); - } - - return context; -};