Skip to content

Commit

Permalink
chore: simplify local state reconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Dec 11, 2023
1 parent c236de5 commit 2698ffa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
23 changes: 10 additions & 13 deletions packages/client/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
UpdateUserPermissionsRequest,
UpdateUserPermissionsResponse,
} from './gen/coordinator';
import { join, reconcileParticipantLocalState } from './rtc/flows/join';
import { join } from './rtc/flows/join';
import {
AudioTrackType,
CallConstructor,
Expand Down Expand Up @@ -1030,22 +1030,19 @@ export class Call {
const pins = callState?.pins ?? [];
this.state.setParticipants(() => {
const participantLookup = this.state.getParticipantLookupBySessionId();
return currentParticipants.map((p) => {
const participant: StreamVideoParticipant = Object.assign(p, {
isLocalParticipant: p.sessionId === sfuClient.sessionId,
viewportVisibilityState: {
videoTrack: VisibilityState.UNKNOWN,
screenShareTrack: VisibilityState.UNKNOWN,
},
});
return currentParticipants.map<StreamVideoParticipant>((p) => {
// We need to preserve the local state of the participant
// (e.g. videoDimension, visibilityState, pinnedAt, etc.)
// as it doesn't exist on the server.
const existingParticipant = participantLookup[p.sessionId];
return reconcileParticipantLocalState(
participant,
existingParticipant,
);
return Object.assign(p, existingParticipant, {
isLocalParticipant: p.sessionId === sfuClient.sessionId,
viewportVisibilityState:
existingParticipant?.viewportVisibilityState ?? {
videoTrack: VisibilityState.UNKNOWN,
screenShareTrack: VisibilityState.UNKNOWN,
},
} satisfies Partial<StreamVideoParticipant>);
});
});
this.state.setParticipantCount(participantCount?.total || 0);
Expand Down
18 changes: 1 addition & 17 deletions packages/client/src/rtc/flows/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
JoinCallRequest,
JoinCallResponse,
} from '../../gen/coordinator';
import { JoinCallData, StreamVideoParticipant } from '../../types';
import { JoinCallData } from '../../types';
import { StreamClient } from '../../coordinator/connection/client';

/**
Expand Down Expand Up @@ -61,19 +61,3 @@ const toRtcConfiguration = (config?: ICEServer[]) => {
};
return rtcConfig;
};

/**
* Reconciles the local state of the source participant into the target participant.
*
* @param target the participant to reconcile into.
* @param source the participant to reconcile from.
*/
export const reconcileParticipantLocalState = (
target: StreamVideoParticipant,
source?: StreamVideoParticipant,
) => {
if (!source) return target;

// copy everything from source to target
return Object.assign(target, source);
};

0 comments on commit 2698ffa

Please sign in to comment.