Files
Sable/src/app/hooks/useCall.ts
T
Erwan Leboucher 02172cc998 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.
2026-07-23 17:51:38 +02:00

46 lines
1.7 KiB
TypeScript

import type { Room } from '$types/matrix-sdk';
import { MatrixRTCSession, MatrixRTCSessionEvent } from '$types/matrix-sdk';
import type { CallMembership } 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 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[] => {
const [memberships, setMemberships] = useState<CallMembership[]>([]);
useEffect(() => {
const updateMemberships = () => {
MatrixRTCSession.sessionMembershipsForSlot(room, session.slotDescription).then(
setMemberships
);
};
updateMemberships();
session.on(MatrixRTCSessionEvent.MembershipsChanged, updateMemberships);
return () => {
session.removeListener(MatrixRTCSessionEvent.MembershipsChanged, updateMemberships);
};
}, [session, room]);
return memberships;
};
export const useCallMembersChange = (session: MatrixRTCSession, callback: () => void): void => {
useEffect(() => {
session.on(MatrixRTCSessionEvent.MembershipsChanged, callback);
return () => {
session.removeListener(MatrixRTCSessionEvent.MembershipsChanged, callback);
};
}, [session, callback]);
};