Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid call.get in all call.ring events #1615

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/client/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
AcceptCallResponse,
BlockUserRequest,
BlockUserResponse,
CallRingEvent,
CollectUserFeedbackRequest,
CollectUserFeedbackResponse,
Credentials,
Expand Down Expand Up @@ -595,6 +596,30 @@ export class Call {
return this.state.createdBy?.id === this.currentUserId;
}

/**
* Update from the call response from the "call.ring" event
* @internal
*/
updateFromRingingEvent = async (event: CallRingEvent) => {
await this.setup();
// call.ring event excludes the call creator in the members list
// as the creator does not get the ring event
// so update the member list accordingly
const creator = this.state.members.find(
(m) => m.user.id === event.call.created_by.id,
);
if (!creator) {
this.state.setMembers(event.members);
} else {
this.state.setMembers([creator, ...event.members]);
}
Comment on lines +611 to +615
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could skip setting members altogether because call.created event already would have set members

// update the call state with the latest event data
this.state.updateFromCallResponse(event.call);
this.ringingSubject.next(true);
this.watching = true;
await this.applyDeviceConfig(false);
};

/**
* Loads the information about the call.
*
Expand Down
36 changes: 17 additions & 19 deletions packages/client/src/StreamVideoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export class StreamVideoClient {
);
return;
}

this.logger('info', `New call created and registered: ${call.cid}`);
const newCall = new Call({
streamClient: this.streamClient,
Expand All @@ -287,25 +286,24 @@ export class StreamVideoClient {
);
return;
}

// The call might already be tracked by the client,
// if `call.created` was received before `call.ring`.
// In that case, we cleanup the already tracked call.
const prevCall = this.writeableStateStore.findCall(call.type, call.id);
await prevCall?.leave({ reason: 'cleaning-up in call.ring' });
// we create a new call
const theCall = new Call({
streamClient: this.streamClient,
type: call.type,
id: call.id,
members,
clientStore: this.writeableStateStore,
ringing: true,
});
theCall.state.updateFromCallResponse(call);
// we fetch the latest metadata for the call from the server
await theCall.get();
this.writeableStateStore.registerCall(theCall);
// the client already has the call instance and we just need to update the state
const theCall = this.writeableStateStore.findCall(call.type, call.id);
if (theCall) {
await theCall.updateFromRingingEvent(event);
} else {
// if client doesn't have the call instance, create the instance and fetch the latest state
// Note: related - we also have onRingingCall method to handle this case from push notifications
const newCallInstance = new Call({
streamClient: this.streamClient,
type: call.type,
id: call.id,
members,
clientStore: this.writeableStateStore,
ringing: true,
});
await newCallInstance.get();
}
}),
);

Expand Down
Loading