Skip to content

Commit ebc2c8e

Browse files
committed
move type declarations around to be at more reasonable places
1 parent 3af1609 commit ebc2c8e

File tree

6 files changed

+121
-98
lines changed

6 files changed

+121
-98
lines changed

src/matrixrtc/IMembershipManager.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2025 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import type { CallMembership } from "./CallMembership.ts";
18+
import type { Focus } from "./focus.ts";
19+
import type { Status } from "./types.ts";
20+
21+
export enum MembershipManagerEvent {
22+
StatusChanged = "StatusChanged",
23+
}
24+
25+
export type MembershipManagerEventHandlerMap = {
26+
[MembershipManagerEvent.StatusChanged]: (prefStatus: Status, newStatus: Status) => void;
27+
};
28+
29+
/**
30+
* This interface defines what a MembershipManager uses and exposes.
31+
* This interface is what we use to write tests and allows changing the actual implementation
32+
* without breaking tests because of some internal method renaming.
33+
*
34+
* @internal
35+
*/
36+
export interface IMembershipManager {
37+
/**
38+
* If we are trying to join, or have successfully joined the session.
39+
* It does not reflect if the room state is already configured to represent us being joined.
40+
* It only means that the Manager should be trying to connect or to disconnect running.
41+
* The Manager is still running right after isJoined becomes false to send the disconnect events.
42+
* @returns true if we intend to be participating in the MatrixRTC session
43+
* @deprecated This name is confusing and replaced by `isActivated()`. (Returns the same as `isActivated()`)
44+
*/
45+
isJoined(): boolean;
46+
/**
47+
* If the manager is activated. This means it tries to do its job to join the call, resend state events...
48+
* It does not imply that the room state is already configured to represent being joined.
49+
* It means that the Manager tries to connect or is connected. ("the manager is still active")
50+
* Once `leave()` is called the manager is not activated anymore but still running until `leave()` resolves.
51+
* @returns `true` if we intend to be participating in the MatrixRTC session
52+
*/
53+
isActivated(): boolean;
54+
/**
55+
* Get the actual connection status of the manager.
56+
*/
57+
get status(): Status;
58+
/**
59+
* The current status while the manager is activated
60+
*/
61+
/**
62+
* Start sending all necessary events to make this user participate in the RTC session.
63+
* @param fociPreferred the list of preferred foci to use in the joined RTC membership event.
64+
* @param fociActive the active focus to use in the joined RTC membership event.
65+
* @throws can throw if it exceeds a configured maximum retry.
66+
*/
67+
join(fociPreferred: Focus[], fociActive?: Focus, onError?: (error: unknown) => void): void;
68+
/**
69+
* Send all necessary events to make this user leave the RTC session.
70+
* @param timeout the maximum duration in ms until the promise is forced to resolve.
71+
* @returns It resolves with true in case the leave was sent successfully.
72+
* It resolves with false in case we hit the timeout before sending successfully.
73+
*/
74+
leave(timeout?: number): Promise<boolean>;
75+
/**
76+
* Call this if the MatrixRTC session members have changed.
77+
*/
78+
onRTCSessionMemberUpdate(memberships: CallMembership[]): Promise<void>;
79+
/**
80+
* The used active focus in the currently joined session.
81+
* @returns the used active focus in the currently joined session or undefined if not joined.
82+
*/
83+
getActiveFocus(): Focus | undefined;
84+
85+
// TypedEventEmitter methods:
86+
on(event: MembershipManagerEvent.StatusChanged, listener: (oldStatus: Status, newStatus: Status) => void): this;
87+
off(event: MembershipManagerEvent.StatusChanged, listener: (oldStatus: Status, newStatus: Status) => void): this;
88+
}

src/matrixrtc/LegacyMembershipManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import { type Focus } from "./focus.ts";
2727
import { isLivekitFocusActive } from "./LivekitFocus.ts";
2828
import { type MembershipConfig } from "./MatrixRTCSession.ts";
2929
import { type EmptyObject } from "../@types/common.ts";
30-
import { type IMembershipManager, type MembershipManagerEvent, Status } from "./types.ts";
30+
import { Status } from "./types.ts";
31+
import type { IMembershipManager, MembershipManagerEvent } from "./IMembershipManager.ts";
3132

3233
/**
3334
* This internal class is used by the MatrixRTCSession to manage the local user's own membership of the session.

src/matrixrtc/MatrixRTCSession.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,12 @@ import { MembershipManager } from "./NewMembershipManager.ts";
2828
import { EncryptionManager, type IEncryptionManager } from "./EncryptionManager.ts";
2929
import { LegacyMembershipManager } from "./LegacyMembershipManager.ts";
3030
import { logDurationSync } from "../utils.ts";
31-
import type { IMembershipManager } from "./types.ts";
3231
import { RoomKeyTransport } from "./RoomKeyTransport.ts";
32+
import { type IMembershipManager } from "./IMembershipManager.ts";
33+
import { type Statistics } from "./types.ts";
3334

3435
const logger = rootLogger.getChild("MatrixRTCSession");
3536

36-
/**
37-
* A type collecting call encryption statistics for a session.
38-
*/
39-
export type Statistics = {
40-
counters: {
41-
/**
42-
* The number of times we have sent a room event containing encryption keys.
43-
*/
44-
roomEventEncryptionKeysSent: number;
45-
/**
46-
* The number of times we have received a room event containing encryption keys.
47-
*/
48-
roomEventEncryptionKeysReceived: number;
49-
};
50-
totals: {
51-
/**
52-
* The total age (in milliseconds) of all room events containing encryption keys that we have received.
53-
* We track the total age so that we can later calculate the average age of all keys received.
54-
*/
55-
roomEventEncryptionKeysReceivedTotalAge: number;
56-
};
57-
};
58-
5937
export enum MatrixRTCSessionEvent {
6038
// A member joined, left, or updated a property of their membership.
6139
MembershipsChanged = "memberships_changed",

src/matrixrtc/RoomKeyTransport.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ limitations under the License.
1515
*/
1616

1717
import type { MatrixClient } from "../client.ts";
18-
import type { EncryptionKeysEventContent } from "./types.ts";
18+
import type { EncryptionKeysEventContent, Statistics } from "./types.ts";
1919
import { EventType } from "../@types/event.ts";
2020
import { type MatrixError } from "../http-api/errors.ts";
2121
import { logger, type Logger } from "../logger.ts";
22-
import { KeyTransportEvents, KeyTransportEventsHandlerMap, type IKeyTransport } from "./IKeyTransport.ts";
22+
import { KeyTransportEvents, type KeyTransportEventsHandlerMap, type IKeyTransport } from "./IKeyTransport.ts";
2323
import { type MatrixEvent } from "../models/event.ts";
2424
import { type CallMembership } from "./CallMembership.ts";
25-
import { Room, RoomEvent } from "../matrix.ts";
25+
import { type Room, RoomEvent } from "../matrix.ts";
2626
import { TypedEventEmitter } from "../models/typed-event-emitter.ts";
27-
import { Statistics } from "./MatrixRTCSession.ts";
2827

2928
export class RoomKeyTransport
3029
extends TypedEventEmitter<KeyTransportEvents, KeyTransportEventsHandlerMap>
@@ -44,10 +43,10 @@ export class RoomKeyTransport
4443
this.prefixedLogger = logger.getChild(`[RTC: ${room.roomId} RoomKeyTransport]`);
4544
}
4645
public start(): void {
47-
this.room.on(RoomEvent.Timeline, (ev) => this.consumeCallEncryptionEvent(ev));
46+
this.room.on(RoomEvent.Timeline, (ev) => void this.consumeCallEncryptionEvent(ev));
4847
}
4948
public stop(): void {
50-
this.room.off(RoomEvent.Timeline, (ev) => this.consumeCallEncryptionEvent(ev));
49+
this.room.off(RoomEvent.Timeline, (ev) => void this.consumeCallEncryptionEvent(ev));
5150
}
5251

5352
private async consumeCallEncryptionEvent(event: MatrixEvent, isRetry = false): Promise<void> {

src/matrixrtc/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export * from "./LivekitFocus.ts";
2020
export * from "./MatrixRTCSession.ts";
2121
export * from "./MatrixRTCSessionManager.ts";
2222
export type * from "./types.ts";
23-
export { Status, MembershipManagerEvent } from "./types.ts";
23+
export { Status } from "./types.ts";
24+
export { MembershipManagerEvent } from "./IMembershipManager.ts";

src/matrixrtc/types.ts

Lines changed: 22 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
*/
1616
import type { IMentions } from "../matrix.ts";
1717
import type { CallMembership } from "./CallMembership.ts";
18-
import type { Focus } from "./focus.ts";
1918

2019
export interface EncryptionKeyEntry {
2120
index: number;
@@ -49,71 +48,28 @@ export enum Status {
4948
Unknown = "Unknown",
5049
}
5150

52-
export enum MembershipManagerEvent {
53-
StatusChanged = "StatusChanged",
54-
}
55-
56-
export type MembershipManagerEventHandlerMap = {
57-
[MembershipManagerEvent.StatusChanged]: (prefStatus: Status, newStatus: Status) => void;
58-
};
59-
6051
/**
61-
* This interface defines what a MembershipManager uses and exposes.
62-
* This interface is what we use to write tests and allows changing the actual implementation
63-
* without breaking tests because of some internal method renaming.
64-
*
65-
* @internal
52+
* A type collecting call encryption statistics for a session.
6653
*/
67-
export interface IMembershipManager {
68-
/**
69-
* If we are trying to join, or have successfully joined the session.
70-
* It does not reflect if the room state is already configured to represent us being joined.
71-
* It only means that the Manager should be trying to connect or to disconnect running.
72-
* The Manager is still running right after isJoined becomes false to send the disconnect events.
73-
* @returns true if we intend to be participating in the MatrixRTC session
74-
* @deprecated This name is confusing and replaced by `isActivated()`. (Returns the same as `isActivated()`)
75-
*/
76-
isJoined(): boolean;
77-
/**
78-
* If the manager is activated. This means it tries to do its job to join the call, resend state events...
79-
* It does not imply that the room state is already configured to represent being joined.
80-
* It means that the Manager tries to connect or is connected. ("the manager is still active")
81-
* Once `leave()` is called the manager is not activated anymore but still running until `leave()` resolves.
82-
* @returns `true` if we intend to be participating in the MatrixRTC session
83-
*/
84-
isActivated(): boolean;
85-
/**
86-
* Get the actual connection status of the manager.
87-
*/
88-
get status(): Status;
89-
/**
90-
* The current status while the manager is activated
91-
*/
92-
/**
93-
* Start sending all necessary events to make this user participate in the RTC session.
94-
* @param fociPreferred the list of preferred foci to use in the joined RTC membership event.
95-
* @param fociActive the active focus to use in the joined RTC membership event.
96-
* @throws can throw if it exceeds a configured maximum retry.
97-
*/
98-
join(fociPreferred: Focus[], fociActive?: Focus, onError?: (error: unknown) => void): void;
99-
/**
100-
* Send all necessary events to make this user leave the RTC session.
101-
* @param timeout the maximum duration in ms until the promise is forced to resolve.
102-
* @returns It resolves with true in case the leave was sent successfully.
103-
* It resolves with false in case we hit the timeout before sending successfully.
104-
*/
105-
leave(timeout?: number): Promise<boolean>;
106-
/**
107-
* Call this if the MatrixRTC session members have changed.
108-
*/
109-
onRTCSessionMemberUpdate(memberships: CallMembership[]): Promise<void>;
110-
/**
111-
* The used active focus in the currently joined session.
112-
* @returns the used active focus in the currently joined session or undefined if not joined.
113-
*/
114-
getActiveFocus(): Focus | undefined;
54+
export type Statistics = {
55+
counters: {
56+
/**
57+
* The number of times we have sent a room event containing encryption keys.
58+
*/
59+
roomEventEncryptionKeysSent: number;
60+
/**
61+
* The number of times we have received a room event containing encryption keys.
62+
*/
63+
roomEventEncryptionKeysReceived: number;
64+
};
65+
totals: {
66+
/**
67+
* The total age (in milliseconds) of all room events containing encryption keys that we have received.
68+
* We track the total age so that we can later calculate the average age of all keys received.
69+
*/
70+
roomEventEncryptionKeysReceivedTotalAge: number;
71+
};
72+
};
11573

116-
// TypedEventEmitter methods:
117-
on(event: MembershipManagerEvent.StatusChanged, listener: (oldStatus: Status, newStatus: Status) => void): this;
118-
off(event: MembershipManagerEvent.StatusChanged, listener: (oldStatus: Status, newStatus: Status) => void): this;
119-
}
74+
export const isMyMembership = (m: CallMembership, userId: string, deviceId: string): boolean =>
75+
m.sender === userId && m.deviceId === deviceId;

0 commit comments

Comments
 (0)