@@ -83,6 +83,7 @@ internal Broker(BiDi bidi, ITransport transport)
8383 new DateTimeOffsetConverter ( ) ,
8484 new PrintPageRangeConverter ( ) ,
8585 new InputOriginConverter ( ) ,
86+ new SubscriptionConverter ( ) ,
8687 new JsonStringEnumConverter ( JsonNamingPolicy . CamelCase ) ,
8788
8889 // https://github.com/dotnet/runtime/issues/72604
@@ -111,15 +112,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
111112 await _transport . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
112113
113114 _receiveMessagesCancellationTokenSource = new CancellationTokenSource ( ) ;
114- _receivingMessageTask = _myTaskFactory . StartNew ( async ( ) => await ReceiveMessagesAsync ( _receiveMessagesCancellationTokenSource . Token ) , TaskCreationOptions . LongRunning ) . Unwrap ( ) ;
115- _eventEmitterTask = _myTaskFactory . StartNew ( async ( ) => await ProcessEventsAwaiterAsync ( ) , TaskCreationOptions . LongRunning ) . Unwrap ( ) ;
115+ _receivingMessageTask = _myTaskFactory . StartNew ( async ( ) => await ReceiveMessagesAsync ( _receiveMessagesCancellationTokenSource . Token ) ) . Unwrap ( ) ;
116+ _eventEmitterTask = _myTaskFactory . StartNew ( ProcessEventsAwaiterAsync ) . Unwrap ( ) ;
116117 }
117118
118119 private async Task ReceiveMessagesAsync ( CancellationToken cancellationToken )
119120 {
120121 while ( ! cancellationToken . IsCancellationRequested )
121122 {
122- var message = await _transport . ReceiveAsJsonAsync < Message > ( _jsonSerializerContext , cancellationToken ) ;
123+ var data = await _transport . ReceiveAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
124+
125+ var message = JsonSerializer . Deserialize ( new ReadOnlySpan < byte > ( data ) , _jsonSerializerContext . Message ) ;
123126
124127 switch ( message )
125128 {
@@ -179,21 +182,21 @@ private async Task ProcessEventsAwaiterAsync()
179182 }
180183
181184 public async Task < TResult > ExecuteCommandAsync < TCommand , TResult > ( TCommand command , CommandOptions ? options )
182- where TCommand : Command
185+ where TCommand : Command
183186 {
184187 var jsonElement = await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
185188
186189 return ( TResult ) jsonElement . Deserialize ( typeof ( TResult ) , _jsonSerializerContext ) ! ;
187190 }
188191
189192 public async Task ExecuteCommandAsync < TCommand > ( TCommand command , CommandOptions ? options )
190- where TCommand : Command
193+ where TCommand : Command
191194 {
192195 await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
193196 }
194197
195198 private async Task < JsonElement > ExecuteCommandCoreAsync < TCommand > ( TCommand command , CommandOptions ? options )
196- where TCommand : Command
199+ where TCommand : Command
197200 {
198201 command . Id = Interlocked . Increment ( ref _currentCommandId ) ;
199202
@@ -207,7 +210,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
207210
208211 _pendingCommands [ command . Id ] = tcs ;
209212
210- await _transport . SendAsJsonAsync ( command , _jsonSerializerContext , cts . Token ) . ConfigureAwait ( false ) ;
213+ var data = JsonSerializer . SerializeToUtf8Bytes ( command , typeof ( TCommand ) , _jsonSerializerContext ) ;
214+
215+ await _transport . SendAsync ( data , cts . Token ) . ConfigureAwait ( false ) ;
211216
212217 return await tcs . Task . ConfigureAwait ( false ) ;
213218 }
@@ -219,23 +224,23 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Act
219224
220225 if ( options is BrowsingContextsSubscriptionOptions browsingContextsOptions )
221226 {
222- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
227+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
223228
224229 var eventHandler = new SyncEventHandler < TEventArgs > ( eventName , action , browsingContextsOptions ? . Contexts ) ;
225230
226231 handlers . Add ( eventHandler ) ;
227232
228- return new Subscription ( this , eventHandler ) ;
233+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
229234 }
230235 else
231236 {
232- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
237+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
233238
234239 var eventHandler = new SyncEventHandler < TEventArgs > ( eventName , action ) ;
235240
236241 handlers . Add ( eventHandler ) ;
237242
238- return new Subscription ( this , eventHandler ) ;
243+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
239244 }
240245 }
241246
@@ -246,44 +251,51 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Fun
246251
247252 if ( options is BrowsingContextsSubscriptionOptions browsingContextsOptions )
248253 {
249- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
254+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
250255
251256 var eventHandler = new AsyncEventHandler < TEventArgs > ( eventName , func , browsingContextsOptions . Contexts ) ;
252257
253258 handlers . Add ( eventHandler ) ;
254259
255- return new Subscription ( this , eventHandler ) ;
260+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
256261 }
257262 else
258263 {
259- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
264+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
260265
261266 var eventHandler = new AsyncEventHandler < TEventArgs > ( eventName , func ) ;
262267
263268 handlers . Add ( eventHandler ) ;
264269
265- return new Subscription ( this , eventHandler ) ;
270+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
266271 }
267272 }
268273
269- public async Task UnsubscribeAsync ( EventHandler eventHandler )
274+ public async Task UnsubscribeAsync ( Modules . Session . Subscription subscription , EventHandler eventHandler )
270275 {
271276 var eventHandlers = _eventHandlers [ eventHandler . EventName ] ;
272277
273278 eventHandlers . Remove ( eventHandler ) ;
274279
275- if ( eventHandler . Contexts is not null )
280+ if ( subscription is not null )
276281 {
277- if ( ! eventHandlers . Any ( h => eventHandler . Contexts . Equals ( h . Contexts ) ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
278- {
279- await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] , new ( ) { Contexts = eventHandler . Contexts } ) . ConfigureAwait ( false ) ;
280- }
282+ await _bidi . SessionModule . UnsubscribeAsync ( [ subscription ] ) . ConfigureAwait ( false ) ;
281283 }
282284 else
283285 {
284- if ( ! eventHandlers . Any ( h => h . Contexts is not null ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
286+ if ( eventHandler . Contexts is not null )
287+ {
288+ if ( ! eventHandlers . Any ( h => eventHandler . Contexts . Equals ( h . Contexts ) ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
289+ {
290+ await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] , new ( ) { Contexts = eventHandler . Contexts } ) . ConfigureAwait ( false ) ;
291+ }
292+ }
293+ else
285294 {
286- await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] ) . ConfigureAwait ( false ) ;
295+ if ( ! eventHandlers . Any ( h => h . Contexts is not null ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
296+ {
297+ await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] ) . ConfigureAwait ( false ) ;
298+ }
287299 }
288300 }
289301 }
0 commit comments