@@ -10,7 +10,16 @@ internal interface IReconnectScheduler
1010 event Action ReconnectionScheduled ;
1111
1212 void Reset ( ) ;
13+
14+ void SetTarget ( IReconnectTarget target ) ;
15+ }
16+
17+ internal interface IReconnectTarget
18+ {
19+ event ConnectionStateChangeHandler ConnectionStateChanged ;
20+ ConnectionState ConnectionState { get ; }
1321 }
22+
1423 /// <summary>
1524 /// Schedules next reconnection time based on the past attempts and network availability
1625 /// </summary>
@@ -44,29 +53,26 @@ private set
4453 }
4554 }
4655
47- public ReconnectScheduler ( ITimeService timeService , IStreamVideoLowLevelClient lowLevelClient ,
48- INetworkMonitor networkMonitor , Func < bool > shouldReconnect )
56+ public ReconnectScheduler ( ITimeService timeService , INetworkMonitor networkMonitor , Func < bool > shouldReconnect )
4957 {
5058 _shouldReconnect = shouldReconnect ?? throw new ArgumentNullException ( nameof ( shouldReconnect ) ) ;
51- _client = lowLevelClient ?? throw new ArgumentNullException ( nameof ( lowLevelClient ) ) ;
5259 _timeService = timeService ?? throw new ArgumentNullException ( nameof ( timeService ) ) ;
5360 _networkMonitor = networkMonitor ?? throw new ArgumentNullException ( nameof ( networkMonitor ) ) ;
5461
5562 _networkMonitor . NetworkAvailabilityChanged += OnNetworkAvailabilityChanged ;
63+ }
5664
57- _client . Connected += OnConnected ;
58- //_client.Reconnecting += OnReconnecting;
59- _client . ConnectionStateChanged += OnConnectionStateChanged ;
65+ //StreamTODO: refactor so that each WS instance creates the reconnector internally and injects its instance
66+ public void SetTarget ( IReconnectTarget target )
67+ {
68+ UnsubscribeFromTarget ( ) ;
69+ _target = target ?? throw new ArgumentNullException ( nameof ( target ) ) ;
70+ SubscribeToTarget ( ) ;
6071 }
6172
6273 public void Dispose ( )
6374 {
64- if ( _client != null )
65- {
66- _client . Connected -= OnConnected ;
67- //_client.Reconnecting -= OnReconnecting;
68- _client . ConnectionStateChanged -= OnConnectionStateChanged ;
69- }
75+ UnsubscribeFromTarget ( ) ;
7076 }
7177
7278 public void SetReconnectStrategySettings ( ReconnectStrategy reconnectStrategy , float ? exponentialMinInterval ,
@@ -114,14 +120,35 @@ public void Stop()
114120 }
115121
116122 //StreamTodo: connection info could be split to separate interface
117- private readonly IStreamVideoLowLevelClient _client ;
118123 private readonly ITimeService _timeService ;
119124 private readonly INetworkMonitor _networkMonitor ;
120125
121126 private int _reconnectAttempts ;
122127 private bool _isStopped ;
123128 private double ? _nextReconnectTime ;
124129 private Func < bool > _shouldReconnect ;
130+
131+ private IReconnectTarget _target ;
132+
133+ private void SubscribeToTarget ( )
134+ {
135+ if ( _target == null )
136+ {
137+ return ;
138+ }
139+
140+ _target . ConnectionStateChanged += OnConnectionStateChanged ;
141+ }
142+
143+ private void UnsubscribeFromTarget ( )
144+ {
145+ if ( _target == null )
146+ {
147+ return ;
148+ }
149+
150+ _target . ConnectionStateChanged -= OnConnectionStateChanged ;
151+ }
125152
126153 private void TryScheduleNextReconnectTime ( )
127154 {
@@ -198,8 +225,8 @@ private void OnNetworkAvailabilityChanged(bool isNetworkAvailable)
198225 return ;
199226 }
200227
201- if ( _client . ConnectionState == ConnectionState . Connected ||
202- _client . ConnectionState == ConnectionState . Connecting )
228+ if ( _target . ConnectionState == ConnectionState . Connected ||
229+ _target . ConnectionState == ConnectionState . Connecting )
203230 {
204231 return ;
205232 }
0 commit comments