mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-28 21:06:27 -08:00
fix: stop leaking session_started/session_ended listeners per room
useCallSession subscribed to mx.matrixRTC.on('session_started'/'session_ended')
once per call, and is used by RoomNavItem (one per visible room). With 11+
visible rooms this exceeded Node's default EventEmitter limit of 10 and
stacked O(N) listeners that each did O(1) filtering per event.
Move the subscription into a single MatrixRTCSessionProvider that exposes
active RTC session room IDs via React context. useCallSession now reads
from that context instead of subscribing per-room.
This commit is contained in:
4 files changed
+91
-26
No files matched your search
@@ -1,33 +1,17 @@
|
||||
import type { Room } from '$types/matrix-sdk';
|
||||
import { MatrixRTCSession, MatrixRTCSessionEvent } from '$types/matrix-sdk';
|
||||
import type { CallMembership } from '$types/matrix-sdk';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { MatrixRTCSessionManagerEvents } from '$types/matrix-sdk';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
import { useActiveRTCSessionIds } from './useMatrixRTCSession';
|
||||
|
||||
export const useCallSession = (room: Room): MatrixRTCSession => {
|
||||
const mx = useMatrixClient();
|
||||
|
||||
const [session, setSession] = useState(mx.matrixRTC.getRoomSession(room));
|
||||
|
||||
useEffect(() => {
|
||||
const start = (roomId: string) => {
|
||||
if (roomId !== room.roomId) return;
|
||||
setSession(mx.matrixRTC.getRoomSession(room));
|
||||
};
|
||||
const end = (roomId: string) => {
|
||||
if (roomId !== room.roomId) return;
|
||||
setSession(mx.matrixRTC.getRoomSession(room));
|
||||
};
|
||||
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionStarted, start);
|
||||
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionEnded, end);
|
||||
return () => {
|
||||
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionStarted, start);
|
||||
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionEnded, end);
|
||||
};
|
||||
}, [mx, room]);
|
||||
|
||||
return session;
|
||||
const activeRoomIds = useActiveRTCSessionIds();
|
||||
// Re-derive the session whenever the active-session set changes. The returned
|
||||
// session object is stable across renders unless the set changes.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
return useMemo(() => mx.matrixRTC.getRoomSession(room), [mx, room, activeRoomIds]);
|
||||
};
|
||||
|
||||
export const useCallMembers = (room: Room, session: MatrixRTCSession): CallMembership[] => {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import type { ReactElement, ReactNode } from 'react';
|
||||
import { createContext, useContext, useEffect, useState } from 'react';
|
||||
import { MatrixRTCSessionManagerEvents } from '$types/matrix-sdk';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
|
||||
/**
|
||||
* The set of room IDs that currently have an active MatrixRTC session.
|
||||
* Updated by a single global subscription to `mx.matrixRTC`, so N consumers
|
||||
* share one listener pair instead of each adding their own (which exceeded
|
||||
* Node's default EventEmitter limit of 10 when 11+ rooms were visible).
|
||||
*/
|
||||
type ActiveRTCSessionIds = ReadonlySet<string>;
|
||||
|
||||
const MatrixRTCSessionContext = createContext<ActiveRTCSessionIds>(new Set());
|
||||
|
||||
export function MatrixRTCSessionProvider({ children }: { children: ReactNode }): ReactElement {
|
||||
const mx = useMatrixClient();
|
||||
const [activeRoomIds, setActiveRoomIds] = useState<ActiveRTCSessionIds>(() => {
|
||||
// Seed with rooms that currently have an active session.
|
||||
// There is no public `getRoomSessions()` bulk method on MatrixRTCSessionManager,
|
||||
// so we iterate all client rooms and check each one individually.
|
||||
const ids = new Set<string>();
|
||||
try {
|
||||
for (const room of mx.getRooms()) {
|
||||
try {
|
||||
const session = mx.matrixRTC.getRoomSession(room);
|
||||
if (session.memberships.length > 0) {
|
||||
ids.add(room.roomId);
|
||||
}
|
||||
} catch {
|
||||
// Individual room session lookup failure is non-fatal.
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Best-effort seed; the subscription below will keep it accurate.
|
||||
}
|
||||
return ids;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handleStart = (roomId: string) => {
|
||||
setActiveRoomIds((prev) => {
|
||||
if (prev.has(roomId)) return prev;
|
||||
const next = new Set(prev);
|
||||
next.add(roomId);
|
||||
return next;
|
||||
});
|
||||
};
|
||||
const handleEnd = (roomId: string) => {
|
||||
setActiveRoomIds((prev) => {
|
||||
if (!prev.has(roomId)) return prev;
|
||||
const next = new Set(prev);
|
||||
next.delete(roomId);
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionStarted, handleStart);
|
||||
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionEnded, handleEnd);
|
||||
|
||||
return () => {
|
||||
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionStarted, handleStart);
|
||||
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionEnded, handleEnd);
|
||||
};
|
||||
}, [mx]);
|
||||
|
||||
return (
|
||||
<MatrixRTCSessionContext.Provider value={activeRoomIds}>
|
||||
{children}
|
||||
</MatrixRTCSessionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the set of room IDs that currently have an active MatrixRTC session.
|
||||
* Backed by a single global subscription (see MatrixRTCSessionProvider).
|
||||
*/
|
||||
export function useActiveRTCSessionIds(): ActiveRTCSessionIds {
|
||||
return useContext(MatrixRTCSessionContext);
|
||||
}
|
||||
@@ -65,6 +65,7 @@ import { NotificationBanner } from '$components/notification-banner';
|
||||
import { ThemeMigrationBanner } from '$components/theme/ThemeMigrationBanner';
|
||||
import { TelemetryConsentBanner } from '$components/telemetry-consent';
|
||||
import { useIncomingCallSignaling } from '$hooks/useCallSignaling';
|
||||
import { MatrixRTCSessionProvider } from '$hooks/useMatrixRTCSession';
|
||||
import { lastVisitedRoomAtom } from '$state/room/lastRoom';
|
||||
import { useSettingsSyncEffect } from '$hooks/useSettingsSync';
|
||||
import { resolveIncomingCallFromNotificationData } from '$features/call/callNotificationBridge';
|
||||
@@ -1004,7 +1005,7 @@ function NativeNotificationClickRouting() {
|
||||
export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
||||
useIncomingCallSignaling();
|
||||
return (
|
||||
<>
|
||||
<MatrixRTCSessionProvider>
|
||||
<SettingsSyncFeature />
|
||||
<SystemEmojiFeature />
|
||||
<PageZoomFeature />
|
||||
@@ -1032,6 +1033,6 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) {
|
||||
<HealthMonitor />
|
||||
<ShareTargetFeature />
|
||||
<IconSizesProvider>{children}</IconSizesProvider>
|
||||
</>
|
||||
</MatrixRTCSessionProvider>
|
||||
);
|
||||
}
|
||||
@@ -1006,7 +1006,7 @@ export class SlidingSyncManager {
|
||||
*/
|
||||
private reassertOptimisticJoins(): void {
|
||||
if (this.optimisticallyJoinedRoomIds.size === 0) return;
|
||||
for (const roomId of [...this.optimisticallyJoinedRoomIds]) {
|
||||
for (const roomId of this.optimisticallyJoinedRoomIds) {
|
||||
const room = this.mx.getRoom(roomId);
|
||||
if (!room) {
|
||||
this.optimisticallyJoinedRoomIds.delete(roomId);
|
||||
|
||||
Reference in new issue
Block a user