From bfc997ddaada01a004d8c066d746ff7f2935b577 Mon Sep 17 00:00:00 2001 From: dominictb Date: Tue, 11 Jun 2024 00:45:36 +0700 Subject: [PATCH 1/4] fix: unsub userIsTyping event on report screen unmount Signed-off-by: dominictb --- src/pages/home/ReportScreen.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 8e9cb1f4396e..aa28aa170beb 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -462,6 +462,7 @@ function ReportScreen({ }); return () => { interactionTask.cancel(); + Report.unsubscribeFromReportChannel(report.reportID); if (!didSubscribeToReportLeavingEvents.current) { return; } From 8ff0effbdc2d6a7a04f2802a4425754b4065acf3 Mon Sep 17 00:00:00 2001 From: dominictb Date: Wed, 12 Jun 2024 01:32:16 +0700 Subject: [PATCH 2/4] chore: optimize the logic of sub/unsub report pusher events Signed-off-by: dominictb --- src/pages/home/ReportScreen.tsx | 20 ++++---- src/pages/home/report/ReportActionsView.tsx | 9 +++- .../home/report/UserTypingEventListener.tsx | 21 ++++---- .../useReportPusherEventSubscription/index.ts | 49 +++++++++++++++++++ .../useReportPusherEventSubscription/types.ts | 9 ++++ 5 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 src/pages/home/report/useReportPusherEventSubscription/index.ts create mode 100644 src/pages/home/report/useReportPusherEventSubscription/types.ts diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index aa28aa170beb..3ccbf6e78952 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -35,6 +35,7 @@ import Navigation from '@libs/Navigation/Navigation'; import clearReportNotifications from '@libs/Notification/clearReportNotifications'; import Performance from '@libs/Performance'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; +import * as Pusher from '@libs/Pusher/pusher'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import shouldFetchReport from '@libs/shouldFetchReport'; @@ -52,6 +53,7 @@ import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import HeaderView from './HeaderView'; import ReportActionsView from './report/ReportActionsView'; import ReportFooter from './report/ReportFooter'; +import useReportPusherEventSubscription from './report/useReportPusherEventSubscription'; import type {ActionListContextType, ReactionListRef, ScrollPosition} from './ReportScreenContext'; import {ActionListContext, ReactionListContext} from './ReportScreenContext'; @@ -304,7 +306,6 @@ function ReportScreen({ const isSingleTransactionView = ReportUtils.isMoneyRequest(report) || ReportUtils.isTrackExpenseReport(report); const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`] ?? null; const isTopMostReportId = currentReportID === reportIDFromRoute; - const didSubscribeToReportLeavingEvents = useRef(false); useEffect(() => { if (!report.reportID || shouldHideReport) { @@ -453,6 +454,8 @@ function ReportScreen({ useEffect(clearNotifications, [clearNotifications]); useAppFocusEvent(clearNotifications); + const subscriptionManagerMap = useReportPusherEventSubscription(); + useEffect(() => { Timing.end(CONST.TIMING.CHAT_RENDER); Performance.markEnd(CONST.TIMING.CHAT_RENDER); @@ -462,12 +465,7 @@ function ReportScreen({ }); return () => { interactionTask.cancel(); - Report.unsubscribeFromReportChannel(report.reportID); - if (!didSubscribeToReportLeavingEvents.current) { - return; - } - - Report.unsubscribeFromLeavingRoomReportChannel(report.reportID); + subscriptionManagerMap.forEach((manager) => manager.unsubscribe(report.reportID)); }; // I'm disabling the warning, as it expects to use exhaustive deps, even though we want this useEffect to run only on the first render. @@ -583,10 +581,9 @@ function ReportScreen({ // Existing reports created will have empty fields for `pendingFields`. const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat); let interactionTask: ReturnType | null = null; - if (!didSubscribeToReportLeavingEvents.current && didCreateReportSuccessfully) { + if (didCreateReportSuccessfully) { interactionTask = InteractionManager.runAfterInteractions(() => { - Report.subscribeToReportLeavingEvents(reportIDFromRoute); - didSubscribeToReportLeavingEvents.current = true; + subscriptionManagerMap.get(Pusher.TYPE.USER_IS_LEAVING_ROOM)?.subscribe(reportIDFromRoute); }); } return () => { @@ -595,7 +592,7 @@ function ReportScreen({ } interactionTask.cancel(); }; - }, [report, didSubscribeToReportLeavingEvents, reportIDFromRoute]); + }, [report, reportIDFromRoute, subscriptionManagerMap]); const onListLayout = useCallback((event: LayoutChangeEvent) => { setListHeight((prev) => event.nativeEvent?.layout?.height ?? prev); @@ -718,6 +715,7 @@ function ReportScreen({ hasLoadingOlderReportActionsError={reportMetadata?.hasLoadingOlderReportActionsError} isReadyForCommentLinking={!shouldShowSkeleton} transactionThreadReportID={transactionThreadReportID} + userTypingEventSubscriptionManager={subscriptionManagerMap.get(Pusher.TYPE.USER_IS_TYPING)} /> )} diff --git a/src/pages/home/report/ReportActionsView.tsx b/src/pages/home/report/ReportActionsView.tsx index 7084434733aa..c1d8530f7d42 100755 --- a/src/pages/home/report/ReportActionsView.tsx +++ b/src/pages/home/report/ReportActionsView.tsx @@ -32,6 +32,7 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import getInitialPaginationSize from './getInitialPaginationSize'; import PopoverReactionList from './ReactionList/PopoverReactionList'; import ReportActionsList from './ReportActionsList'; +import type {ReportPusherSubscriptionManager} from './useReportPusherEventSubscription/types'; import UserTypingEventListener from './UserTypingEventListener'; type ReportActionsViewOnyxProps = { @@ -76,6 +77,8 @@ type ReportActionsViewProps = ReportActionsViewOnyxProps & { /** The reportID of the transaction thread report associated with this current report, if any */ // eslint-disable-next-line react/no-unused-prop-types transactionThreadReportID?: string | null; + + userTypingEventSubscriptionManager?: ReportPusherSubscriptionManager; }; const DIFF_BETWEEN_SCREEN_HEIGHT_AND_LIST = 120; @@ -96,6 +99,7 @@ function ReportActionsView({ isLoadingNewerReportActions = false, hasLoadingNewerReportActionsError = false, isReadyForCommentLinking = false, + userTypingEventSubscriptionManager, }: ReportActionsViewProps) { useCopySelectionHelper(); const reactionListRef = useContext(ReactionListContext); @@ -547,7 +551,10 @@ function ReportActionsView({ onContentSizeChange={onContentSizeChange} shouldEnableAutoScrollToTopThreshold={shouldEnableAutoScroll} /> - + ); diff --git a/src/pages/home/report/UserTypingEventListener.tsx b/src/pages/home/report/UserTypingEventListener.tsx index 45e56cfe7ff1..494bf3425612 100644 --- a/src/pages/home/report/UserTypingEventListener.tsx +++ b/src/pages/home/report/UserTypingEventListener.tsx @@ -1,14 +1,14 @@ import type {RouteProp} from '@react-navigation/native'; import {useIsFocused, useRoute} from '@react-navigation/native'; -import {useEffect, useRef} from 'react'; +import {useEffect} from 'react'; import {InteractionManager} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import Navigation from '@libs/Navigation/Navigation'; import type {CentralPaneNavigatorParamList} from '@libs/Navigation/types'; -import * as Report from '@userActions/Report'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; +import type {ReportPusherSubscriptionManager} from './useReportPusherEventSubscription/types'; type UserTypingEventListenerOnyxProps = { /** Stores last visited path */ @@ -18,9 +18,10 @@ type UserTypingEventListenerOnyxProps = { type UserTypingEventListenerProps = UserTypingEventListenerOnyxProps & { /** The report currently being looked at */ report: OnyxTypes.Report; + + userTypingEventSubscriptionManager?: ReportPusherSubscriptionManager; }; -function UserTypingEventListener({report, lastVisitedPath}: UserTypingEventListenerProps) { - const didSubscribeToReportTypingEvents = useRef(false); +function UserTypingEventListener({report, lastVisitedPath, userTypingEventSubscriptionManager}: UserTypingEventListenerProps) { const reportID = report.reportID; const isFocused = useIsFocused(); const route = useRoute>(); @@ -37,19 +38,17 @@ function UserTypingEventListener({report, lastVisitedPath}: UserTypingEventListe // Existing reports created will have empty fields for `pendingFields`. const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat); - if (!didSubscribeToReportTypingEvents.current && didCreateReportSuccessfully) { + if (didCreateReportSuccessfully) { interactionTask = InteractionManager.runAfterInteractions(() => { - Report.subscribeToReportTypingEvents(reportID); - didSubscribeToReportTypingEvents.current = true; + userTypingEventSubscriptionManager?.subscribe(reportID); }); } } else { const topmostReportId = Navigation.getTopmostReportId(); - if (topmostReportId !== reportID && didSubscribeToReportTypingEvents.current) { - didSubscribeToReportTypingEvents.current = false; + if (topmostReportId !== reportID) { InteractionManager.runAfterInteractions(() => { - Report.unsubscribeFromReportChannel(reportID); + userTypingEventSubscriptionManager?.unsubscribe(reportID); }); } } @@ -59,7 +58,7 @@ function UserTypingEventListener({report, lastVisitedPath}: UserTypingEventListe } interactionTask.cancel(); }; - }, [isFocused, report.pendingFields, didSubscribeToReportTypingEvents, lastVisitedPath, reportID, route]); + }, [isFocused, report.pendingFields, lastVisitedPath, reportID, route, userTypingEventSubscriptionManager]); return null; } diff --git a/src/pages/home/report/useReportPusherEventSubscription/index.ts b/src/pages/home/report/useReportPusherEventSubscription/index.ts new file mode 100644 index 000000000000..ae5c9a34a14a --- /dev/null +++ b/src/pages/home/report/useReportPusherEventSubscription/index.ts @@ -0,0 +1,49 @@ +import {useRef} from 'react'; +import * as Pusher from '@libs/Pusher/pusher'; +import * as Report from '@userActions/Report'; +import type {ReportPusherSubscriptionManager, ReportSubscribeFunction} from './types'; + +const reportScreenPusherEventSubscriptionMap: Record> = { + [Pusher.TYPE.USER_IS_TYPING]: { + subscribe: Report.subscribeToReportTypingEvents, + unsubscribe: Report.unsubscribeFromReportChannel, + }, + [Pusher.TYPE.USER_IS_LEAVING_ROOM]: { + subscribe: Report.subscribeToReportLeavingEvents, + unsubscribe: Report.unsubscribeFromLeavingRoomReportChannel, + }, +}; + +const useReportPusherEventSubscription = (): Map => { + const didSubscribedMapRef = useRef>(Object.fromEntries(Object.keys(reportScreenPusherEventSubscriptionMap).map((eventName) => [eventName, false]))); + + const createSubscriptionFunctionRef = useRef( + (eventName: string, fn: ReportSubscribeFunction, isForSubscribed = true): ReportSubscribeFunction => + (reportID: string, ...args: unknown[]) => { + // if the subscription state is already in the desired state (i.e try to subscribe or resubscribe again => return early) + if (!!didSubscribedMapRef.current[eventName] === !!isForSubscribed) { + return; + } + + didSubscribedMapRef.current[eventName] = !!isForSubscribed; + fn(reportID, ...args); + }, + ); + + const reportSubscriptionManagerMapRef = useRef>( + new Map( + Object.entries(reportScreenPusherEventSubscriptionMap).map(([eventName, {subscribe, unsubscribe}]) => [ + eventName, + { + eventName, + subscribe: createSubscriptionFunctionRef.current(eventName, subscribe, true), + unsubscribe: createSubscriptionFunctionRef.current(eventName, unsubscribe, false), + }, + ]), + ), + ); + + return reportSubscriptionManagerMapRef.current; +}; + +export default useReportPusherEventSubscription; diff --git a/src/pages/home/report/useReportPusherEventSubscription/types.ts b/src/pages/home/report/useReportPusherEventSubscription/types.ts new file mode 100644 index 000000000000..29d58456c81a --- /dev/null +++ b/src/pages/home/report/useReportPusherEventSubscription/types.ts @@ -0,0 +1,9 @@ +type ReportSubscribeFunction = (reportID: string, ...args: unknown[]) => void; + +type ReportPusherSubscriptionManager = { + eventName: string; + subscribe: ReportSubscribeFunction; + unsubscribe: ReportSubscribeFunction; +}; + +export type {ReportPusherSubscriptionManager, ReportSubscribeFunction}; From 44314dab234f4e65f1e966fbab462ff5efd44d5a Mon Sep 17 00:00:00 2001 From: dominictb Date: Wed, 12 Jun 2024 19:09:10 +0700 Subject: [PATCH 3/4] Revert "chore: optimize the logic of sub/unsub report pusher events" This reverts commit 8ff0effbdc2d6a7a04f2802a4425754b4065acf3. --- src/pages/home/ReportScreen.tsx | 20 ++++---- src/pages/home/report/ReportActionsView.tsx | 9 +--- .../home/report/UserTypingEventListener.tsx | 21 ++++---- .../useReportPusherEventSubscription/index.ts | 49 ------------------- .../useReportPusherEventSubscription/types.ts | 9 ---- 5 files changed, 23 insertions(+), 85 deletions(-) delete mode 100644 src/pages/home/report/useReportPusherEventSubscription/index.ts delete mode 100644 src/pages/home/report/useReportPusherEventSubscription/types.ts diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 3ccbf6e78952..aa28aa170beb 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -35,7 +35,6 @@ import Navigation from '@libs/Navigation/Navigation'; import clearReportNotifications from '@libs/Notification/clearReportNotifications'; import Performance from '@libs/Performance'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; -import * as Pusher from '@libs/Pusher/pusher'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import shouldFetchReport from '@libs/shouldFetchReport'; @@ -53,7 +52,6 @@ import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import HeaderView from './HeaderView'; import ReportActionsView from './report/ReportActionsView'; import ReportFooter from './report/ReportFooter'; -import useReportPusherEventSubscription from './report/useReportPusherEventSubscription'; import type {ActionListContextType, ReactionListRef, ScrollPosition} from './ReportScreenContext'; import {ActionListContext, ReactionListContext} from './ReportScreenContext'; @@ -306,6 +304,7 @@ function ReportScreen({ const isSingleTransactionView = ReportUtils.isMoneyRequest(report) || ReportUtils.isTrackExpenseReport(report); const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`] ?? null; const isTopMostReportId = currentReportID === reportIDFromRoute; + const didSubscribeToReportLeavingEvents = useRef(false); useEffect(() => { if (!report.reportID || shouldHideReport) { @@ -454,8 +453,6 @@ function ReportScreen({ useEffect(clearNotifications, [clearNotifications]); useAppFocusEvent(clearNotifications); - const subscriptionManagerMap = useReportPusherEventSubscription(); - useEffect(() => { Timing.end(CONST.TIMING.CHAT_RENDER); Performance.markEnd(CONST.TIMING.CHAT_RENDER); @@ -465,7 +462,12 @@ function ReportScreen({ }); return () => { interactionTask.cancel(); - subscriptionManagerMap.forEach((manager) => manager.unsubscribe(report.reportID)); + Report.unsubscribeFromReportChannel(report.reportID); + if (!didSubscribeToReportLeavingEvents.current) { + return; + } + + Report.unsubscribeFromLeavingRoomReportChannel(report.reportID); }; // I'm disabling the warning, as it expects to use exhaustive deps, even though we want this useEffect to run only on the first render. @@ -581,9 +583,10 @@ function ReportScreen({ // Existing reports created will have empty fields for `pendingFields`. const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat); let interactionTask: ReturnType | null = null; - if (didCreateReportSuccessfully) { + if (!didSubscribeToReportLeavingEvents.current && didCreateReportSuccessfully) { interactionTask = InteractionManager.runAfterInteractions(() => { - subscriptionManagerMap.get(Pusher.TYPE.USER_IS_LEAVING_ROOM)?.subscribe(reportIDFromRoute); + Report.subscribeToReportLeavingEvents(reportIDFromRoute); + didSubscribeToReportLeavingEvents.current = true; }); } return () => { @@ -592,7 +595,7 @@ function ReportScreen({ } interactionTask.cancel(); }; - }, [report, reportIDFromRoute, subscriptionManagerMap]); + }, [report, didSubscribeToReportLeavingEvents, reportIDFromRoute]); const onListLayout = useCallback((event: LayoutChangeEvent) => { setListHeight((prev) => event.nativeEvent?.layout?.height ?? prev); @@ -715,7 +718,6 @@ function ReportScreen({ hasLoadingOlderReportActionsError={reportMetadata?.hasLoadingOlderReportActionsError} isReadyForCommentLinking={!shouldShowSkeleton} transactionThreadReportID={transactionThreadReportID} - userTypingEventSubscriptionManager={subscriptionManagerMap.get(Pusher.TYPE.USER_IS_TYPING)} /> )} diff --git a/src/pages/home/report/ReportActionsView.tsx b/src/pages/home/report/ReportActionsView.tsx index c1d8530f7d42..7084434733aa 100755 --- a/src/pages/home/report/ReportActionsView.tsx +++ b/src/pages/home/report/ReportActionsView.tsx @@ -32,7 +32,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject'; import getInitialPaginationSize from './getInitialPaginationSize'; import PopoverReactionList from './ReactionList/PopoverReactionList'; import ReportActionsList from './ReportActionsList'; -import type {ReportPusherSubscriptionManager} from './useReportPusherEventSubscription/types'; import UserTypingEventListener from './UserTypingEventListener'; type ReportActionsViewOnyxProps = { @@ -77,8 +76,6 @@ type ReportActionsViewProps = ReportActionsViewOnyxProps & { /** The reportID of the transaction thread report associated with this current report, if any */ // eslint-disable-next-line react/no-unused-prop-types transactionThreadReportID?: string | null; - - userTypingEventSubscriptionManager?: ReportPusherSubscriptionManager; }; const DIFF_BETWEEN_SCREEN_HEIGHT_AND_LIST = 120; @@ -99,7 +96,6 @@ function ReportActionsView({ isLoadingNewerReportActions = false, hasLoadingNewerReportActionsError = false, isReadyForCommentLinking = false, - userTypingEventSubscriptionManager, }: ReportActionsViewProps) { useCopySelectionHelper(); const reactionListRef = useContext(ReactionListContext); @@ -551,10 +547,7 @@ function ReportActionsView({ onContentSizeChange={onContentSizeChange} shouldEnableAutoScrollToTopThreshold={shouldEnableAutoScroll} /> - + ); diff --git a/src/pages/home/report/UserTypingEventListener.tsx b/src/pages/home/report/UserTypingEventListener.tsx index 494bf3425612..45e56cfe7ff1 100644 --- a/src/pages/home/report/UserTypingEventListener.tsx +++ b/src/pages/home/report/UserTypingEventListener.tsx @@ -1,14 +1,14 @@ import type {RouteProp} from '@react-navigation/native'; import {useIsFocused, useRoute} from '@react-navigation/native'; -import {useEffect} from 'react'; +import {useEffect, useRef} from 'react'; import {InteractionManager} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import Navigation from '@libs/Navigation/Navigation'; import type {CentralPaneNavigatorParamList} from '@libs/Navigation/types'; +import * as Report from '@userActions/Report'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; -import type {ReportPusherSubscriptionManager} from './useReportPusherEventSubscription/types'; type UserTypingEventListenerOnyxProps = { /** Stores last visited path */ @@ -18,10 +18,9 @@ type UserTypingEventListenerOnyxProps = { type UserTypingEventListenerProps = UserTypingEventListenerOnyxProps & { /** The report currently being looked at */ report: OnyxTypes.Report; - - userTypingEventSubscriptionManager?: ReportPusherSubscriptionManager; }; -function UserTypingEventListener({report, lastVisitedPath, userTypingEventSubscriptionManager}: UserTypingEventListenerProps) { +function UserTypingEventListener({report, lastVisitedPath}: UserTypingEventListenerProps) { + const didSubscribeToReportTypingEvents = useRef(false); const reportID = report.reportID; const isFocused = useIsFocused(); const route = useRoute>(); @@ -38,17 +37,19 @@ function UserTypingEventListener({report, lastVisitedPath, userTypingEventSubscr // Existing reports created will have empty fields for `pendingFields`. const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat); - if (didCreateReportSuccessfully) { + if (!didSubscribeToReportTypingEvents.current && didCreateReportSuccessfully) { interactionTask = InteractionManager.runAfterInteractions(() => { - userTypingEventSubscriptionManager?.subscribe(reportID); + Report.subscribeToReportTypingEvents(reportID); + didSubscribeToReportTypingEvents.current = true; }); } } else { const topmostReportId = Navigation.getTopmostReportId(); - if (topmostReportId !== reportID) { + if (topmostReportId !== reportID && didSubscribeToReportTypingEvents.current) { + didSubscribeToReportTypingEvents.current = false; InteractionManager.runAfterInteractions(() => { - userTypingEventSubscriptionManager?.unsubscribe(reportID); + Report.unsubscribeFromReportChannel(reportID); }); } } @@ -58,7 +59,7 @@ function UserTypingEventListener({report, lastVisitedPath, userTypingEventSubscr } interactionTask.cancel(); }; - }, [isFocused, report.pendingFields, lastVisitedPath, reportID, route, userTypingEventSubscriptionManager]); + }, [isFocused, report.pendingFields, didSubscribeToReportTypingEvents, lastVisitedPath, reportID, route]); return null; } diff --git a/src/pages/home/report/useReportPusherEventSubscription/index.ts b/src/pages/home/report/useReportPusherEventSubscription/index.ts deleted file mode 100644 index ae5c9a34a14a..000000000000 --- a/src/pages/home/report/useReportPusherEventSubscription/index.ts +++ /dev/null @@ -1,49 +0,0 @@ -import {useRef} from 'react'; -import * as Pusher from '@libs/Pusher/pusher'; -import * as Report from '@userActions/Report'; -import type {ReportPusherSubscriptionManager, ReportSubscribeFunction} from './types'; - -const reportScreenPusherEventSubscriptionMap: Record> = { - [Pusher.TYPE.USER_IS_TYPING]: { - subscribe: Report.subscribeToReportTypingEvents, - unsubscribe: Report.unsubscribeFromReportChannel, - }, - [Pusher.TYPE.USER_IS_LEAVING_ROOM]: { - subscribe: Report.subscribeToReportLeavingEvents, - unsubscribe: Report.unsubscribeFromLeavingRoomReportChannel, - }, -}; - -const useReportPusherEventSubscription = (): Map => { - const didSubscribedMapRef = useRef>(Object.fromEntries(Object.keys(reportScreenPusherEventSubscriptionMap).map((eventName) => [eventName, false]))); - - const createSubscriptionFunctionRef = useRef( - (eventName: string, fn: ReportSubscribeFunction, isForSubscribed = true): ReportSubscribeFunction => - (reportID: string, ...args: unknown[]) => { - // if the subscription state is already in the desired state (i.e try to subscribe or resubscribe again => return early) - if (!!didSubscribedMapRef.current[eventName] === !!isForSubscribed) { - return; - } - - didSubscribedMapRef.current[eventName] = !!isForSubscribed; - fn(reportID, ...args); - }, - ); - - const reportSubscriptionManagerMapRef = useRef>( - new Map( - Object.entries(reportScreenPusherEventSubscriptionMap).map(([eventName, {subscribe, unsubscribe}]) => [ - eventName, - { - eventName, - subscribe: createSubscriptionFunctionRef.current(eventName, subscribe, true), - unsubscribe: createSubscriptionFunctionRef.current(eventName, unsubscribe, false), - }, - ]), - ), - ); - - return reportSubscriptionManagerMapRef.current; -}; - -export default useReportPusherEventSubscription; diff --git a/src/pages/home/report/useReportPusherEventSubscription/types.ts b/src/pages/home/report/useReportPusherEventSubscription/types.ts deleted file mode 100644 index 29d58456c81a..000000000000 --- a/src/pages/home/report/useReportPusherEventSubscription/types.ts +++ /dev/null @@ -1,9 +0,0 @@ -type ReportSubscribeFunction = (reportID: string, ...args: unknown[]) => void; - -type ReportPusherSubscriptionManager = { - eventName: string; - subscribe: ReportSubscribeFunction; - unsubscribe: ReportSubscribeFunction; -}; - -export type {ReportPusherSubscriptionManager, ReportSubscribeFunction}; From ae03a7fe272b1ba8f72de5867e2e563f3a42921a Mon Sep 17 00:00:00 2001 From: dominictb Date: Wed, 12 Jun 2024 19:28:23 +0700 Subject: [PATCH 4/4] fix: revert to simple solution Signed-off-by: dominictb --- src/pages/home/ReportScreen.tsx | 1 - .../home/report/UserTypingEventListener.tsx | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index aa28aa170beb..8e9cb1f4396e 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -462,7 +462,6 @@ function ReportScreen({ }); return () => { interactionTask.cancel(); - Report.unsubscribeFromReportChannel(report.reportID); if (!didSubscribeToReportLeavingEvents.current) { return; } diff --git a/src/pages/home/report/UserTypingEventListener.tsx b/src/pages/home/report/UserTypingEventListener.tsx index 45e56cfe7ff1..87464074a491 100644 --- a/src/pages/home/report/UserTypingEventListener.tsx +++ b/src/pages/home/report/UserTypingEventListener.tsx @@ -24,6 +24,23 @@ function UserTypingEventListener({report, lastVisitedPath}: UserTypingEventListe const reportID = report.reportID; const isFocused = useIsFocused(); const route = useRoute>(); + + useEffect( + () => () => { + if (!didSubscribeToReportTypingEvents.current) { + return; + } + + // unsubscribe from report typing events when the component unmounts + didSubscribeToReportTypingEvents.current = false; + InteractionManager.runAfterInteractions(() => { + Report.unsubscribeFromReportChannel(reportID); + }); + }, + // eslint-disable-next-line react-hooks/exhaustive-deps + [], + ); + useEffect(() => { // Ensures any optimistic report that is being created (ex: a thread report) gets created and initialized successfully before subscribing if (route?.params?.reportID !== reportID) {