Simplify local transport connection state tracking

This commit is contained in:
Robin
2025-10-14 14:43:13 -04:00
parent 2dc6134606
commit b0eb566a4f
2 changed files with 19 additions and 32 deletions

View File

@@ -392,22 +392,23 @@ export class CallViewModel extends ViewModel {
), ),
); );
public readonly livekitConnectionState$ = this.scope.behavior( public readonly livekitConnectionState$ =
this.localConnection$.pipe( this.scope.behavior<ConnectionState>(
switchMap((c) => this.localConnection$.pipe(
c?.state === "ready" switchMap((c) =>
? // TODO mapping to ConnectionState for compatibility, but we should use the full state? c?.state === "ready"
c.value.transportState$.pipe( ? // TODO mapping to ConnectionState for compatibility, but we should use the full state?
map((s) => { c.value.transportState$.pipe(
if (s.state === "ConnectedToLkRoom") return s.connectionState; switchMap((s) => {
return ConnectionState.Disconnected; if (s.state === "ConnectedToLkRoom")
}), return s.connectionState$;
distinctUntilChanged(), return of(ConnectionState.Disconnected);
) }),
: of(ConnectionState.Disconnected), )
: of(ConnectionState.Disconnected),
),
), ),
), );
);
/** /**
* Connections for each transport in use by one or more session members that * Connections for each transport in use by one or more session members that

View File

@@ -21,7 +21,7 @@ import {
type CallMembership, type CallMembership,
type LivekitTransport, type LivekitTransport,
} from "matrix-js-sdk/lib/matrixrtc"; } from "matrix-js-sdk/lib/matrixrtc";
import { BehaviorSubject, combineLatest } from "rxjs"; import { BehaviorSubject, combineLatest, type Observable } from "rxjs";
import { import {
getSFUConfigWithOpenID, getSFUConfigWithOpenID,
@@ -60,7 +60,7 @@ export type TransportState =
| { state: "FailedToStart"; error: Error; transport: LivekitTransport } | { state: "FailedToStart"; error: Error; transport: LivekitTransport }
| { | {
state: "ConnectedToLkRoom"; state: "ConnectedToLkRoom";
connectionState: ConnectionState; connectionState$: Observable<ConnectionState>;
transport: LivekitTransport; transport: LivekitTransport;
} }
| { state: "Stopped"; transport: LivekitTransport }; | { state: "Stopped"; transport: LivekitTransport };
@@ -159,7 +159,7 @@ export class Connection {
this._transportState$.next({ this._transportState$.next({
state: "ConnectedToLkRoom", state: "ConnectedToLkRoom",
transport: this.transport, transport: this.transport,
connectionState: this.livekitRoom.state, connectionState$: connectionStateObserver(this.livekitRoom),
}); });
} catch (error) { } catch (error) {
this._transportState$.next({ this._transportState$.next({
@@ -250,20 +250,6 @@ export class Connection {
[], [],
); );
scope
.behavior<ConnectionState>(connectionStateObserver(this.livekitRoom))
.subscribe((connectionState) => {
const current = this._transportState$.value;
// Only update the state if we are already connected to the LiveKit room.
if (current.state === "ConnectedToLkRoom") {
this._transportState$.next({
state: "ConnectedToLkRoom",
connectionState,
transport: current.transport,
});
}
});
scope.onEnd(() => void this.stop()); scope.onEnd(() => void this.stop());
} }
} }