mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-30 22:02:43 -08:00
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.
46 lines
1.7 KiB
TypeScript
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]);
|
|
};
|