@@ -70,6 +70,7 @@ internal Broker(BiDi bidi, ITransport transport)
70
70
{
71
71
new BrowsingContextConverter ( _bidi ) ,
72
72
new BrowserUserContextConverter ( bidi ) ,
73
+ new BrowserClientWindowConverter ( ) ,
73
74
new NavigationConverter ( ) ,
74
75
new InterceptConverter ( _bidi ) ,
75
76
new RequestConverter ( _bidi ) ,
@@ -82,6 +83,7 @@ internal Broker(BiDi bidi, ITransport transport)
82
83
new DateTimeOffsetConverter ( ) ,
83
84
new PrintPageRangeConverter ( ) ,
84
85
new InputOriginConverter ( ) ,
86
+ new SubscriptionConverter ( ) ,
85
87
new JsonStringEnumConverter ( JsonNamingPolicy . CamelCase ) ,
86
88
87
89
// https://github.com/dotnet/runtime/issues/72604
@@ -97,6 +99,7 @@ internal Broker(BiDi bidi, ITransport transport)
97
99
new Json . Converters . Enumerable . LocateNodesResultConverter ( ) ,
98
100
new Json . Converters . Enumerable . InputSourceActionsConverter ( ) ,
99
101
new Json . Converters . Enumerable . GetUserContextsResultConverter ( ) ,
102
+ new Json . Converters . Enumerable . GetClientWindowsResultConverter ( ) ,
100
103
new Json . Converters . Enumerable . GetRealmsResultConverter ( ) ,
101
104
}
102
105
} ;
@@ -109,15 +112,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
109
112
await _transport . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
110
113
111
114
_receiveMessagesCancellationTokenSource = new CancellationTokenSource ( ) ;
112
- _receivingMessageTask = _myTaskFactory . StartNew ( async ( ) => await ReceiveMessagesAsync ( _receiveMessagesCancellationTokenSource . Token ) , TaskCreationOptions . LongRunning ) . Unwrap ( ) ;
113
- _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 ( ) ;
114
117
}
115
118
116
119
private async Task ReceiveMessagesAsync ( CancellationToken cancellationToken )
117
120
{
118
121
while ( ! cancellationToken . IsCancellationRequested )
119
122
{
120
- 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 ) ;
121
126
122
127
switch ( message )
123
128
{
@@ -177,21 +182,21 @@ private async Task ProcessEventsAwaiterAsync()
177
182
}
178
183
179
184
public async Task < TResult > ExecuteCommandAsync < TCommand , TResult > ( TCommand command , CommandOptions ? options )
180
- where TCommand : Command
185
+ where TCommand : Command
181
186
{
182
187
var jsonElement = await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
183
188
184
189
return ( TResult ) jsonElement . Deserialize ( typeof ( TResult ) , _jsonSerializerContext ) ! ;
185
190
}
186
191
187
192
public async Task ExecuteCommandAsync < TCommand > ( TCommand command , CommandOptions ? options )
188
- where TCommand : Command
193
+ where TCommand : Command
189
194
{
190
195
await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
191
196
}
192
197
193
198
private async Task < JsonElement > ExecuteCommandCoreAsync < TCommand > ( TCommand command , CommandOptions ? options )
194
- where TCommand : Command
199
+ where TCommand : Command
195
200
{
196
201
command . Id = Interlocked . Increment ( ref _currentCommandId ) ;
197
202
@@ -205,7 +210,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
205
210
206
211
_pendingCommands [ command . Id ] = tcs ;
207
212
208
- 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 ) ;
209
216
210
217
return await tcs . Task . ConfigureAwait ( false ) ;
211
218
}
@@ -217,23 +224,23 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Act
217
224
218
225
if ( options is BrowsingContextsSubscriptionOptions browsingContextsOptions )
219
226
{
220
- 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 ) ;
221
228
222
229
var eventHandler = new SyncEventHandler < TEventArgs > ( eventName , action , browsingContextsOptions ? . Contexts ) ;
223
230
224
231
handlers . Add ( eventHandler ) ;
225
232
226
- return new Subscription ( this , eventHandler ) ;
233
+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
227
234
}
228
235
else
229
236
{
230
- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
237
+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
231
238
232
239
var eventHandler = new SyncEventHandler < TEventArgs > ( eventName , action ) ;
233
240
234
241
handlers . Add ( eventHandler ) ;
235
242
236
- return new Subscription ( this , eventHandler ) ;
243
+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
237
244
}
238
245
}
239
246
@@ -244,44 +251,51 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Fun
244
251
245
252
if ( options is BrowsingContextsSubscriptionOptions browsingContextsOptions )
246
253
{
247
- 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 ) ;
248
255
249
256
var eventHandler = new AsyncEventHandler < TEventArgs > ( eventName , func , browsingContextsOptions . Contexts ) ;
250
257
251
258
handlers . Add ( eventHandler ) ;
252
259
253
- return new Subscription ( this , eventHandler ) ;
260
+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
254
261
}
255
262
else
256
263
{
257
- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
264
+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
258
265
259
266
var eventHandler = new AsyncEventHandler < TEventArgs > ( eventName , func ) ;
260
267
261
268
handlers . Add ( eventHandler ) ;
262
269
263
- return new Subscription ( this , eventHandler ) ;
270
+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
264
271
}
265
272
}
266
273
267
- public async Task UnsubscribeAsync ( EventHandler eventHandler )
274
+ public async Task UnsubscribeAsync ( Modules . Session . Subscription subscription , EventHandler eventHandler )
268
275
{
269
276
var eventHandlers = _eventHandlers [ eventHandler . EventName ] ;
270
277
271
278
eventHandlers . Remove ( eventHandler ) ;
272
279
273
- if ( eventHandler . Contexts is not null )
280
+ if ( subscription is not null )
274
281
{
275
- if ( ! eventHandlers . Any ( h => eventHandler . Contexts . Equals ( h . Contexts ) ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
276
- {
277
- await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] , new ( ) { Contexts = eventHandler . Contexts } ) . ConfigureAwait ( false ) ;
278
- }
282
+ await _bidi . SessionModule . UnsubscribeAsync ( [ subscription ] ) . ConfigureAwait ( false ) ;
279
283
}
280
284
else
281
285
{
282
- 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
283
294
{
284
- 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
+ }
285
299
}
286
300
}
287
301
}
0 commit comments