@@ -19,7 +19,8 @@ internal class ChromeTargetManager : ITargetManager
1919 private readonly Func < TargetInfo , CDPSession , Target > _targetFactoryFunc ;
2020 private readonly Func < TargetInfo , bool > _targetFilterFunc ;
2121 private readonly ILogger < ChromeTargetManager > _logger ;
22- private readonly ConcurrentDictionary < string , Target > _attachedTargetsByTargetId = new ( ) ;
22+ private readonly ConcurrentDictionary < string , Target > _availableTargetsByTargetIdDictionary = new ( ) ;
23+ private readonly AsyncDictionaryHelper < string , Target > _attachedTargetsByTargetId ;
2324 private readonly ConcurrentDictionary < string , Target > _attachedTargetsBySessionId = new ( ) ;
2425 private readonly ConcurrentDictionary < string , TargetInfo > _discoveredTargetsByTargetId = new ( ) ;
2526 private readonly ConcurrentDictionary < ICDPConnection , List < TargetInterceptor > > _targetInterceptors = new ( ) ;
@@ -36,6 +37,7 @@ public ChromeTargetManager(
3637 Func < TargetInfo , bool > targetFilterFunc ,
3738 int targetDiscoveryTimeout = 0 )
3839 {
40+ _attachedTargetsByTargetId = new AsyncDictionaryHelper < string , Target > ( _availableTargetsByTargetIdDictionary , "Target {0} not found" ) ;
3941 _connection = connection ;
4042 _targetFilterFunc = targetFilterFunc ;
4143 _targetFactoryFunc = targetFactoryFunc ;
@@ -86,7 +88,7 @@ public ChromeTargetManager(
8688
8789 public event EventHandler < TargetChangedArgs > TargetDiscovered ;
8890
89- public ConcurrentDictionary < string , Target > GetAvailableTargets ( ) => _attachedTargetsByTargetId ;
91+ public AsyncDictionaryHelper < string , Target > GetAvailableTargets ( ) => _attachedTargetsByTargetId ;
9092
9193 public async Task InitializeAsync ( )
9294 {
@@ -112,11 +114,7 @@ public void AddTargetInterceptor(CDPSession session, TargetInterceptor intercept
112114 public void RemoveTargetInterceptor ( CDPSession session , TargetInterceptor interceptor )
113115 {
114116 _targetInterceptors . TryGetValue ( session , out var interceptors ) ;
115-
116- if ( interceptors != null )
117- {
118- interceptors . Remove ( interceptor ) ;
119- }
117+ interceptors ? . Remove ( interceptor ) ;
120118 }
121119
122120 private void StoreExistingTargetsForInit ( )
@@ -190,13 +188,13 @@ private void OnTargetCreated(TargetCreatedResponse e)
190188
191189 if ( e . TargetInfo . Type == TargetType . Browser && e . TargetInfo . Attached )
192190 {
193- if ( _attachedTargetsByTargetId . ContainsKey ( e . TargetInfo . TargetId ) )
191+ if ( _availableTargetsByTargetIdDictionary . ContainsKey ( e . TargetInfo . TargetId ) )
194192 {
195193 return ;
196194 }
197195
198196 var target = _targetFactoryFunc ( e . TargetInfo , null ) ;
199- _attachedTargetsByTargetId [ e . TargetInfo . TargetId ] = target ;
197+ _attachedTargetsByTargetId . AddItem ( e . TargetInfo . TargetId , target ) ;
200198 }
201199 }
202200
@@ -206,7 +204,7 @@ private async void OnTargetDestroyed(TargetDestroyedResponse e)
206204 await EnsureTargetsIdsForInit ( ) . ConfigureAwait ( false ) ;
207205 FinishInitializationIfReady ( e . TargetId ) ;
208206
209- if ( targetInfo ? . Type == TargetType . ServiceWorker && _attachedTargetsByTargetId . TryRemove ( e . TargetId , out var target ) )
207+ if ( targetInfo ? . Type == TargetType . ServiceWorker && _availableTargetsByTargetIdDictionary . TryRemove ( e . TargetId , out var target ) )
210208 {
211209 TargetGone ? . Invoke ( this , new TargetChangedArgs { Target = target , TargetInfo = targetInfo } ) ;
212210 }
@@ -217,7 +215,7 @@ private void OnTargetInfoChanged(TargetCreatedResponse e)
217215 _discoveredTargetsByTargetId [ e . TargetInfo . TargetId ] = e . TargetInfo ;
218216
219217 if ( _ignoredTargets . Contains ( e . TargetInfo . TargetId ) ||
220- ! _attachedTargetsByTargetId . TryGetValue ( e . TargetInfo . TargetId , out var target ) ||
218+ ! _availableTargetsByTargetIdDictionary . TryGetValue ( e . TargetInfo . TargetId , out var target ) ||
221219 ! e . TargetInfo . Attached )
222220 {
223221 return ;
@@ -229,16 +227,10 @@ private void OnTargetInfoChanged(TargetCreatedResponse e)
229227 private async Task OnAttachedToTarget ( object sender , TargetAttachedToTargetResponse e )
230228 {
231229 var parent = sender as ICDPConnection ;
232- var parentSession = parent as CDPSession ;
233230 var targetInfo = e . TargetInfo ;
234- var session = _connection . GetSession ( e . SessionId ) ;
231+ var session = _connection . GetSession ( e . SessionId ) ?? throw new PuppeteerException ( $ "Session { e . SessionId } was not created." ) ;
235232
236- if ( session == null )
237- {
238- throw new PuppeteerException ( $ "Session { e . SessionId } was not created.") ;
239- }
240-
241- Func < Task > silentDetach = async ( ) =>
233+ async Task SilentDetach ( )
242234 {
243235 try
244236 {
@@ -254,7 +246,7 @@ await parent.SendAsync(
254246 {
255247 _logger . LogError ( ex , "silentDetach failed." ) ;
256248 }
257- } ;
249+ }
258250
259251 if ( ! _connection . IsAutoAttached ( targetInfo . TargetId ) )
260252 {
@@ -266,14 +258,14 @@ await parent.SendAsync(
266258 {
267259 await EnsureTargetsIdsForInit ( ) . ConfigureAwait ( false ) ;
268260 FinishInitializationIfReady ( targetInfo . TargetId ) ;
269- await silentDetach ( ) . ConfigureAwait ( false ) ;
270- if ( _attachedTargetsByTargetId . ContainsKey ( targetInfo . TargetId ) )
261+ await SilentDetach ( ) . ConfigureAwait ( false ) ;
262+ if ( _availableTargetsByTargetIdDictionary . ContainsKey ( targetInfo . TargetId ) )
271263 {
272264 return ;
273265 }
274266
275267 var workerTarget = _targetFactoryFunc ( targetInfo , null ) ;
276- _attachedTargetsByTargetId . TryAdd ( targetInfo . TargetId , workerTarget ) ;
268+ _attachedTargetsByTargetId . AddItem ( targetInfo . TargetId , workerTarget ) ;
277269 TargetAvailable ? . Invoke ( this , new TargetChangedArgs { Target = workerTarget } ) ;
278270 return ;
279271 }
@@ -283,11 +275,11 @@ await parent.SendAsync(
283275 _ignoredTargets . Add ( targetInfo . TargetId ) ;
284276 await EnsureTargetsIdsForInit ( ) . ConfigureAwait ( false ) ;
285277 FinishInitializationIfReady ( targetInfo . TargetId ) ;
286- await silentDetach ( ) . ConfigureAwait ( false ) ;
278+ await SilentDetach ( ) . ConfigureAwait ( false ) ;
287279 return ;
288280 }
289281
290- var existingTarget = _attachedTargetsByTargetId . TryGetValue ( targetInfo . TargetId , out var target ) ;
282+ var existingTarget = _availableTargetsByTargetIdDictionary . TryGetValue ( targetInfo . TargetId , out var target ) ;
291283 if ( ! existingTarget )
292284 {
293285 target = _targetFactoryFunc ( targetInfo , session ) ;
@@ -301,7 +293,7 @@ await parent.SendAsync(
301293 }
302294 else
303295 {
304- _attachedTargetsByTargetId . TryAdd ( targetInfo . TargetId , target ) ;
296+ _attachedTargetsByTargetId . AddItem ( targetInfo . TargetId , target ) ;
305297 _attachedTargetsBySessionId . TryAdd ( session . Id , target ) ;
306298 }
307299
@@ -310,7 +302,7 @@ await parent.SendAsync(
310302 foreach ( var interceptor in interceptors )
311303 {
312304 Target parentTarget = null ;
313- if ( parentSession != null && ! _attachedTargetsBySessionId . TryGetValue ( parentSession . Id , out parentTarget ) )
305+ if ( parent is CDPSession parentSession && ! _attachedTargetsBySessionId . TryGetValue ( parentSession . Id , out parentTarget ) )
314306 {
315307 throw new PuppeteerException ( "Parent session not found in attached targets" ) ;
316308 }
@@ -385,7 +377,7 @@ private void OnDetachedFromTarget(object sender, TargetDetachedFromTargetRespons
385377 return ;
386378 }
387379
388- _attachedTargetsByTargetId . TryRemove ( target . TargetId , out _ ) ;
380+ _availableTargetsByTargetIdDictionary . TryRemove ( target . TargetId , out _ ) ;
389381 TargetGone ? . Invoke ( this , new TargetChangedArgs { Target = target } ) ;
390382 }
391383 }
0 commit comments