mirror of
https://github.com/vector-im/element-call.git
synced 2026-02-02 04:05:56 +00:00
* Avoid reactivity bugs in how we track external state
Many of our hooks which attempt to bridge external state from an EventEmitter or EventTarget into React had subtle bugs which could cause them to fail to react to certain updates. The conditions necessary for triggering these bugs are explained by the tests that I've included.
In the majority of cases, I don't think we were triggering these bugs in practice. They could've become problems if we refactored our components in certain ways. The one concrete case I'm aware of in which we actually triggered such a bug was the race condition with the useRoomEncryptionSystem shared secret logic (addressed by a1110af6d5).
But, particularly with all the weird reactivity issues we're debugging this week, I think we need to eliminate the possibility that any of the bugs in these hooks are the cause of our current headaches.
* Reuse useTypedEventEmitterState in useLocalStorage
* Fix type error
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
/*
|
|
Copyright 2023, 2024 New Vector Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE in the repository root for full details.
|
|
*/
|
|
|
|
import { logger } from "matrix-js-sdk/lib/logger";
|
|
import {
|
|
type MatrixRTCSession,
|
|
MatrixRTCSessionEvent,
|
|
} from "matrix-js-sdk/lib/matrixrtc";
|
|
import { TypedEventEmitter } from "matrix-js-sdk";
|
|
import { useCallback, useEffect } from "react";
|
|
|
|
import { useTypedEventEmitterState } from "./useEvents";
|
|
|
|
const dummySession = new TypedEventEmitter();
|
|
|
|
export function useMatrixRTCSessionJoinState(
|
|
rtcSession: MatrixRTCSession | undefined,
|
|
): boolean {
|
|
// React doesn't allow you to run a hook conditionally, so we have to plug in
|
|
// a dummy event emitter in case there is no rtcSession yet
|
|
const isJoined = useTypedEventEmitterState(
|
|
rtcSession ?? dummySession,
|
|
MatrixRTCSessionEvent.JoinStateChanged,
|
|
useCallback(() => rtcSession?.isJoined() ?? false, [rtcSession]),
|
|
);
|
|
|
|
useEffect(() => {
|
|
logger.info(
|
|
`Session in room ${rtcSession?.room.roomId} changed to ${
|
|
isJoined ? "joined" : "left"
|
|
}`,
|
|
);
|
|
}, [rtcSession, isJoined]);
|
|
|
|
return isJoined;
|
|
}
|