@@ -41,7 +41,9 @@ typedef OnCallPermissionRequest = void Function(
4141typedef GetCurrentUserId = String ? Function ();
4242
4343typedef SetActiveCall = Future <void > Function (Call ? );
44- typedef GetActiveCallCid = StreamCallCid ? Function ();
44+ typedef SetOutgoingCall = Future <void > Function (Call ? );
45+ typedef GetActiveCall = Call ? Function ();
46+ typedef GetOutgoingCall = Call ? Function ();
4547
4648const _idState = 1 ;
4749const _idUserId = 2 ;
@@ -68,7 +70,9 @@ class Call {
6870 required CoordinatorClient coordinatorClient,
6971 required StateEmitter <User ?> currentUser,
7072 required SetActiveCall setActiveCall,
71- required GetActiveCallCid getActiveCallCid,
73+ required SetOutgoingCall setOutgoingCall,
74+ required GetActiveCall getActiveCall,
75+ required GetOutgoingCall getOutgoingCall,
7276 RetryPolicy ? retryPolicy,
7377 SdpPolicy ? sdpPolicy,
7478 CallPreferences ? preferences,
@@ -79,7 +83,9 @@ class Call {
7983 coordinatorClient: coordinatorClient,
8084 currentUser: currentUser,
8185 setActiveCall: setActiveCall,
82- getActiveCallCid: getActiveCallCid,
86+ setOutgoingCall: setOutgoingCall,
87+ getActiveCall: getActiveCall,
88+ getOutgoingCall: getOutgoingCall,
8389 retryPolicy: retryPolicy,
8490 sdpPolicy: sdpPolicy,
8591 preferences: preferences,
@@ -94,7 +100,9 @@ class Call {
94100 required CoordinatorClient coordinatorClient,
95101 required StateEmitter <User ?> currentUser,
96102 required SetActiveCall setActiveCall,
97- required GetActiveCallCid getActiveCallCid,
103+ required SetOutgoingCall setOutgoingCall,
104+ required GetActiveCall getActiveCall,
105+ required GetOutgoingCall getOutgoingCall,
98106 RetryPolicy ? retryPolicy,
99107 SdpPolicy ? sdpPolicy,
100108 CallPreferences ? preferences,
@@ -105,7 +113,9 @@ class Call {
105113 coordinatorClient: coordinatorClient,
106114 currentUser: currentUser,
107115 setActiveCall: setActiveCall,
108- getActiveCallCid: getActiveCallCid,
116+ setOutgoingCall: setOutgoingCall,
117+ getActiveCall: getActiveCall,
118+ getOutgoingCall: getOutgoingCall,
109119 retryPolicy: retryPolicy,
110120 sdpPolicy: sdpPolicy,
111121 preferences: preferences,
@@ -125,7 +135,9 @@ class Call {
125135 required CoordinatorClient coordinatorClient,
126136 required StateEmitter <User ?> currentUser,
127137 required SetActiveCall setActiveCall,
128- required GetActiveCallCid getActiveCallCid,
138+ required SetOutgoingCall setOutgoingCall,
139+ required GetActiveCall getActiveCall,
140+ required GetOutgoingCall getOutgoingCall,
129141 RetryPolicy ? retryPolicy,
130142 SdpPolicy ? sdpPolicy,
131143 CallPreferences ? preferences,
@@ -136,7 +148,9 @@ class Call {
136148 coordinatorClient: coordinatorClient,
137149 currentUser: currentUser,
138150 setActiveCall: setActiveCall,
139- getActiveCallCid: getActiveCallCid,
151+ setOutgoingCall: setOutgoingCall,
152+ getActiveCall: getActiveCall,
153+ getOutgoingCall: getOutgoingCall,
140154 retryPolicy: retryPolicy,
141155 sdpPolicy: sdpPolicy,
142156 preferences: preferences,
@@ -148,7 +162,9 @@ class Call {
148162 required CoordinatorClient coordinatorClient,
149163 required StateEmitter <User ?> currentUser,
150164 required SetActiveCall setActiveCall,
151- required GetActiveCallCid getActiveCallCid,
165+ required SetOutgoingCall setOutgoingCall,
166+ required GetActiveCall getActiveCall,
167+ required GetOutgoingCall getOutgoingCall,
152168 RetryPolicy ? retryPolicy,
153169 SdpPolicy ? sdpPolicy,
154170 CallPreferences ? preferences,
@@ -172,7 +188,9 @@ class Call {
172188 coordinatorClient: coordinatorClient,
173189 currentUser: currentUser,
174190 setActiveCall: setActiveCall,
175- getActiveCallCid: getActiveCallCid,
191+ setOutgoingCall: setOutgoingCall,
192+ getActiveCall: getActiveCall,
193+ getOutgoingCall: getOutgoingCall,
176194 preferences: finalCallPreferences,
177195 stateManager: stateManager,
178196 credentials: credentials,
@@ -185,7 +203,9 @@ class Call {
185203 Call ._({
186204 required StateEmitter <User ?> currentUser,
187205 required SetActiveCall setActiveCall,
188- required GetActiveCallCid getActiveCallCid,
206+ required SetOutgoingCall setOutgoingCall,
207+ required GetActiveCall getActiveCall,
208+ required GetOutgoingCall getOutgoingCall,
189209 required CoordinatorClient coordinatorClient,
190210 required CallPreferences preferences,
191211 required CallStateNotifier stateManager,
@@ -206,7 +226,9 @@ class Call {
206226 .whereNotNull ()
207227 .distinct (),
208228 _setActiveCall = setActiveCall,
209- _getActiveCallCid = getActiveCallCid,
229+ _setOutgoingCall = setOutgoingCall,
230+ _getActiveCall = getActiveCall,
231+ _getOutgoingCall = getOutgoingCall,
210232 _coordinatorClient = coordinatorClient,
211233 _preferences = preferences,
212234 _retryPolicy = retryPolicy,
@@ -226,7 +248,9 @@ class Call {
226248 final GetCurrentUserId _getCurrentUserId;
227249 final Stream <String > _currentUserIdUpdates;
228250 final SetActiveCall _setActiveCall;
229- final GetActiveCallCid _getActiveCallCid;
251+ final SetOutgoingCall _setOutgoingCall;
252+ final GetActiveCall _getActiveCall;
253+ final GetOutgoingCall _getOutgoingCall;
230254 final CoordinatorClient _coordinatorClient;
231255 final RetryPolicy _retryPolicy;
232256 final CallPreferences _preferences;
@@ -241,6 +265,8 @@ class Call {
241265
242266 StreamCallType get type => state.value.callType;
243267
268+ bool get isActiveCall => _getActiveCall ()? .callCid == callCid;
269+
244270 String get id => state.value.callId;
245271
246272 StateEmitter <CallState > get state => _stateManager.callStateStream;
@@ -380,6 +406,21 @@ class Call {
380406 _logger.w (() => '[acceptCall] rejected (invalid status): $status ' );
381407 return Result .error ('invalid status: $status ' );
382408 }
409+
410+ final outgoingCall = _getOutgoingCall ();
411+ if (outgoingCall? .callCid != callCid) {
412+ await outgoingCall? .reject (reason: 'cancel' );
413+ await outgoingCall? .leave ();
414+ await _setOutgoingCall (null );
415+ }
416+
417+ final activeCall = _getActiveCall ();
418+ if (activeCall? .callCid != callCid) {
419+ await activeCall? .reject (reason: 'cancel' );
420+ await activeCall? .leave ();
421+ await _setActiveCall (null );
422+ }
423+
383424 final result = await _coordinatorClient.acceptCall (cid: state.callCid);
384425 if (result is Success <None >) {
385426 _stateManager.lifecycleCallAccepted ();
@@ -448,7 +489,7 @@ class Call {
448489 return const Result .success (none);
449490 }
450491
451- if (_getActiveCallCid () == callCid) {
492+ if (_getActiveCall () ? .callCid == callCid) {
452493 _logger.w (
453494 () => '[join] rejected (a call with the same cid is in progress)' ,
454495 );
@@ -997,6 +1038,7 @@ class Call {
9971038 await _session? .dispose ();
9981039 _session = null ;
9991040 await _setActiveCall (null );
1041+ await _setOutgoingCall (null );
10001042 _logger.v (() => '[clear] completed' );
10011043 }
10021044
@@ -1291,6 +1333,10 @@ class Call {
12911333 return Result .error ('[getOrCreate] failed; no user_id found' );
12921334 }
12931335
1336+ if (ringing) {
1337+ await _setOutgoingCall (this );
1338+ }
1339+
12941340 final response = await _coordinatorClient.getOrCreateCall (
12951341 callCid: callCid,
12961342 ringing: ringing,
0 commit comments