diff --git a/src/CallHandler.tsx b/src/CallHandler.tsx index 4f33714fef..0268ebfe46 100644 --- a/src/CallHandler.tsx +++ b/src/CallHandler.tsx @@ -141,6 +141,7 @@ export enum PlaceCallType { export enum CallHandlerEvent { CallsChanged = "calls_changed", + CallChangeRoom = "call_change_room", } export default class CallHandler extends EventEmitter { @@ -537,10 +538,7 @@ export default class CallHandler extends EventEmitter { this.removeCallForRoom(mappedRoomId); mappedRoomId = newMappedRoomId; this.calls.set(mappedRoomId, call); - dis.dispatch({ - action: Action.CallChangeRoom, - call, - }); + this.emit(CallHandlerEvent.CallChangeRoom, call); } } }); diff --git a/src/components/views/voip/CallPreview.tsx b/src/components/views/voip/CallPreview.tsx index d31afddec9..80fd64a820 100644 --- a/src/components/views/voip/CallPreview.tsx +++ b/src/components/views/voip/CallPreview.tsx @@ -19,7 +19,7 @@ import React from 'react'; import CallView from "./CallView"; import RoomViewStore from '../../../stores/RoomViewStore'; -import CallHandler from '../../../CallHandler'; +import CallHandler, { CallHandlerEvent } from '../../../CallHandler'; import dis from '../../../dispatcher/dispatcher'; import { ActionPayload } from '../../../dispatcher/payloads'; import PersistentApp from "../elements/PersistentApp"; @@ -27,7 +27,6 @@ import SettingsStore from "../../../settings/SettingsStore"; import { CallEvent, CallState, MatrixCall } from 'matrix-js-sdk/src/webrtc/call'; import { MatrixClientPeg } from '../../../MatrixClientPeg'; import {replaceableComponent} from "../../../utils/replaceableComponent"; -import { Action } from '../../../dispatcher/actions'; const SHOW_CALL_IN_STATES = [ CallState.Connected, @@ -110,12 +109,14 @@ export default class CallPreview extends React.Component { } public componentDidMount() { + CallHandler.sharedInstance().addListener(CallHandlerEvent.CallChangeRoom, this.updateCalls); this.roomStoreToken = RoomViewStore.addListener(this.onRoomViewStoreUpdate); this.dispatcherRef = dis.register(this.onAction); MatrixClientPeg.get().on(CallEvent.RemoteHoldUnhold, this.onCallRemoteHold); } public componentWillUnmount() { + CallHandler.sharedInstance().removeListener(CallHandlerEvent.CallChangeRoom, this.updateCalls); MatrixClientPeg.get().removeListener(CallEvent.RemoteHoldUnhold, this.onCallRemoteHold); if (this.roomStoreToken) { this.roomStoreToken.remove(); @@ -143,21 +144,24 @@ export default class CallPreview extends React.Component { switch (payload.action) { // listen for call state changes to prod the render method, which // may hide the global CallView if the call it is tracking is dead - case Action.CallChangeRoom: case 'call_state': { - const [primaryCall, secondaryCalls] = getPrimarySecondaryCalls( - CallHandler.sharedInstance().getAllActiveCallsNotInRoom(this.state.roomId), - ); - - this.setState({ - primaryCall: primaryCall, - secondaryCall: secondaryCalls[0], - }); + this.updateCalls(); break; } } }; + private updateCalls = () => { + const [primaryCall, secondaryCalls] = getPrimarySecondaryCalls( + CallHandler.sharedInstance().getAllActiveCallsNotInRoom(this.state.roomId), + ); + + this.setState({ + primaryCall: primaryCall, + secondaryCall: secondaryCalls[0], + }); + }; + private onCallRemoteHold = () => { const [primaryCall, secondaryCalls] = getPrimarySecondaryCalls( CallHandler.sharedInstance().getAllActiveCallsNotInRoom(this.state.roomId), diff --git a/src/dispatcher/actions.ts b/src/dispatcher/actions.ts index 46c962f160..cd32c3743f 100644 --- a/src/dispatcher/actions.ts +++ b/src/dispatcher/actions.ts @@ -114,9 +114,6 @@ export enum Action { */ VirtualRoomSupportUpdated = "virtual_room_support_updated", - // Probably would be better to have a VoIP states in a store and have the store emit changes - CallChangeRoom = "call_change_room", - /** * Fired when an upload has started. Should be used with UploadStartedPayload. */ diff --git a/test/CallHandler-test.ts b/test/CallHandler-test.ts index 754610b223..f7e2c27993 100644 --- a/test/CallHandler-test.ts +++ b/test/CallHandler-test.ts @@ -16,7 +16,7 @@ limitations under the License. import './skinned-sdk'; -import CallHandler, { PlaceCallType } from '../src/CallHandler'; +import CallHandler, { PlaceCallType, CallHandlerEvent } from '../src/CallHandler'; import { stubClient, mkStubRoom } from './test-utils'; import { MatrixClientPeg } from '../src/MatrixClientPeg'; import dis from '../src/dispatcher/dispatcher'; @@ -172,11 +172,9 @@ describe('CallHandler', () => { let callRoomChangeEventCount = 0; const roomChangePromise = new Promise(resolve => { - dispatchHandle = dis.register(payload => { - if (payload.action === Action.CallChangeRoom) { - ++callRoomChangeEventCount; - resolve(); - } + CallHandler.sharedInstance().addListener(CallHandlerEvent.CallChangeRoom, () => { + ++callRoomChangeEventCount; + resolve(); }); }); @@ -202,6 +200,7 @@ describe('CallHandler', () => { await roomChangePromise; dis.unregister(dispatchHandle); + CallHandler.sharedInstance().removeAllListeners(); // If everything's gone well, we should have seen only one room change // event and the call should now be in user 3's room.