18
18
using System . Threading ;
19
19
using System . Diagnostics ;
20
20
using System . Collections . Concurrent ;
21
+ using Microsoft . Azure . Commands . Common . Authentication . Abstractions . Models ;
21
22
22
23
namespace Microsoft . Azure . Commands . Common . Authentication
23
24
{
@@ -30,6 +31,7 @@ public abstract class AzureSession : IAzureSession
30
31
static bool _initialized = false ;
31
32
static ReaderWriterLockSlim sessionLock = new ReaderWriterLockSlim ( LockRecursionPolicy . SupportsRecursion ) ;
32
33
private IDictionary < ComponentKey , object > _componentRegistry = new ConcurrentDictionary < ComponentKey , object > ( new ComponentKeyComparer ( ) ) ;
34
+ private event EventHandler < AzureSessionEventArgs > _eventHandler ;
33
35
34
36
/// <summary>
35
37
/// Gets or sets Azure client factory.
@@ -218,7 +220,7 @@ public bool TryGetComponent<T>(string componentName, out T component) where T :
218
220
219
221
public void RegisterComponent < T > ( string componentName , Func < T > componentInitializer ) where T : class
220
222
{
221
- RegisterComponent ( componentName , componentInitializer , false ) ; ;
223
+ RegisterComponent ( componentName , componentInitializer , false ) ;
222
224
}
223
225
224
226
public void RegisterComponent < T > ( string componentName , Func < T > componentInitializer , bool overwrite ) where T : class
@@ -229,7 +231,20 @@ public void RegisterComponent<T>(string componentName, Func<T> componentInitiali
229
231
var key = new ComponentKey ( componentName , typeof ( T ) ) ;
230
232
if ( ! _componentRegistry . ContainsKey ( key ) || overwrite )
231
233
{
232
- _componentRegistry [ key ] = componentInitializer ( ) ;
234
+ if ( _componentRegistry . ContainsKey ( key ) && overwrite )
235
+ {
236
+ var existed = _componentRegistry [ key ] ;
237
+ if ( existed is IAzureSessionListener existedListener )
238
+ {
239
+ _eventHandler -= existedListener . OnEvent ;
240
+ }
241
+ }
242
+ var component = componentInitializer ( ) ;
243
+ _componentRegistry [ key ] = component ;
244
+ if ( component is IAzureSessionListener listener )
245
+ {
246
+ _eventHandler += listener . OnEvent ;
247
+ }
233
248
}
234
249
} ) ;
235
250
}
@@ -242,16 +257,37 @@ public void UnregisterComponent<T>(string componentName) where T : class
242
257
var key = new ComponentKey ( componentName , typeof ( T ) ) ;
243
258
if ( _componentRegistry . ContainsKey ( key ) )
244
259
{
260
+ var component = _componentRegistry [ key ] ;
261
+ if ( component is IAzureSessionListener listener )
262
+ {
263
+ _eventHandler -= listener . OnEvent ;
264
+ }
245
265
_componentRegistry . Remove ( key ) ;
246
266
}
247
267
} ) ;
248
268
}
249
269
250
270
public void ClearComponents ( )
251
271
{
272
+ ChangeRegistry ( ClearHandlers ) ;
252
273
ChangeRegistry ( _componentRegistry . Clear ) ;
253
274
}
254
275
276
+ private void ClearHandlers ( )
277
+ {
278
+ _eventHandler = null ;
279
+ }
280
+
281
+ public void RaiseContextClearedEvent ( )
282
+ {
283
+ OnEvent ( new AzureSessionEventArgs ( AzureSessionEventType . ContextCleared ) ) ;
284
+ }
285
+
286
+ protected virtual void OnEvent ( AzureSessionEventArgs e )
287
+ {
288
+ _eventHandler ? . Invoke ( this , e ) ;
289
+ }
290
+
255
291
void ChangeRegistry ( Action changeAction )
256
292
{
257
293
changeAction ( ) ;
0 commit comments