3
3
4
4
using System ;
5
5
using System . Collections . Concurrent ;
6
- using System . Collections . Generic ;
7
6
using System . Diagnostics ;
8
7
using System . Diagnostics . CodeAnalysis ;
9
- using System . IO ;
10
8
using System . Text . Json ;
11
9
using System . Threading ;
12
10
using System . Threading . Tasks ;
@@ -22,16 +20,16 @@ public abstract partial class JSRuntime : IJSRuntime
22
20
{
23
21
private long _nextObjectReferenceId = 0 ; // 0 signals no object, but we increment prior to assignment. The first tracked object should have id 1
24
22
private long _nextPendingTaskId = 1 ; // Start at 1 because zero signals "no response needed"
25
- private readonly ConcurrentDictionary < long , object > _pendingTasks = new ConcurrentDictionary < long , object > ( ) ;
26
- private readonly ConcurrentDictionary < long , IDotNetObjectReference > _trackedRefsById = new ConcurrentDictionary < long , IDotNetObjectReference > ( ) ;
27
- private readonly ConcurrentDictionary < long , CancellationTokenRegistration > _cancellationRegistrations =
28
- new ConcurrentDictionary < long , CancellationTokenRegistration > ( ) ;
23
+ private readonly ConcurrentDictionary < long , object > _pendingTasks = new ( ) ;
24
+ private readonly ConcurrentDictionary < long , IDotNetObjectReference > _trackedRefsById = new ( ) ;
25
+ private readonly ConcurrentDictionary < long , CancellationTokenRegistration > _cancellationRegistrations = new ( ) ;
29
26
30
27
/// <summary>
31
28
/// Initializes a new instance of <see cref="JSRuntime"/>.
32
29
/// </summary>
33
30
protected JSRuntime ( )
34
31
{
32
+ ByteArrayJsonConverter = new ByteArrayJsonConverter ( ) ;
35
33
JsonSerializerOptions = new JsonSerializerOptions
36
34
{
37
35
MaxDepth = 32 ,
@@ -41,7 +39,7 @@ protected JSRuntime()
41
39
{
42
40
new DotNetObjectReferenceJsonConverterFactory ( this ) ,
43
41
new JSObjectReferenceJsonConverter ( this ) ,
44
- new ByteArrayJsonConverter ( this ) ,
42
+ ByteArrayJsonConverter ,
45
43
}
46
44
} ;
47
45
}
@@ -52,19 +50,14 @@ protected JSRuntime()
52
50
protected internal JsonSerializerOptions JsonSerializerOptions { get ; }
53
51
54
52
/// <summary>
55
- /// Gets or sets the default timeout for asynchronous JavaScript calls .
53
+ /// Gets the <see cref="Infrastructure.ByteArrayJsonConverter"/> used to serialize and deserialize byte arrays from the interop payloads .
56
54
/// </summary>
57
- protected TimeSpan ? DefaultAsyncTimeout { get ; set ; }
55
+ internal ByteArrayJsonConverter ByteArrayJsonConverter { get ; }
58
56
59
57
/// <summary>
60
- /// Contains the combined byte array(s) being serialized.
61
- /// </summary>
62
- internal readonly List < byte [ ] > ByteArraysToSerialize = new ( ) ;
63
-
64
- /// <summary>
65
- /// Contains the combined byte array(s) being deserialized.
58
+ /// Gets or sets the default timeout for asynchronous JavaScript calls.
66
59
/// </summary>
67
- internal byte [ ] [ ] ? ByteArraysToDeserialize { get ; set ; }
60
+ protected TimeSpan ? DefaultAsyncTimeout { get ; set ; }
68
61
69
62
/// <summary>
70
63
/// Invokes the specified JavaScript function asynchronously.
@@ -135,12 +128,12 @@ protected JSRuntime()
135
128
}
136
129
137
130
var serializedArgs = args is not null && args . Length != 0 ?
138
- SerializeArgs ( args ) :
131
+ DotNetDispatcher . SerializeArgs ( this , args ) :
139
132
new SerializedArgs ( null , null ) ;
140
133
var resultType = JSCallResultTypeHelper . FromGeneric < TValue > ( ) ;
141
134
142
135
143
- BeginInvokeJS ( taskId , identifier , serializedArgs . ArgsJson , serializedArgs . ByteArrays , resultType , targetInstanceId ) ;
136
+ BeginInvokeJS ( taskId , identifier , serializedArgs , resultType , targetInstanceId ) ;
144
137
145
138
return new ValueTask < TValue > ( tcs . Task ) ;
146
139
}
@@ -160,44 +153,24 @@ private void CleanupTasksAndRegistrations(long taskId)
160
153
}
161
154
}
162
155
163
- /// <summary>
164
- /// Serialize the args to a json string, with the byte arrays
165
- /// extracted out for seperate transmission.
166
- /// </summary>
167
- /// <param name="args">Arguments to be converted to json.</param>
168
- /// <returns>
169
- /// A tuple of the json string and an array containing the extracted
170
- /// byte arrays from the args.
171
- /// </returns>
172
- protected internal SerializedArgs SerializeArgs ( object ? args )
173
- {
174
- ByteArraysToSerialize . Clear ( ) ;
175
- var serializedArgs = JsonSerializer . Serialize ( args , JsonSerializerOptions ) ;
176
- var byteArrays = ByteArraysToSerialize . ToArray ( ) ;
177
-
178
- return new ( serializedArgs , byteArrays ) ;
179
- }
180
-
181
156
/// <summary>
182
157
/// Begins an asynchronous function invocation.
183
158
/// </summary>
184
159
/// <param name="taskId">The identifier for the function invocation, or zero if no async callback is required.</param>
185
160
/// <param name="identifier">The identifier for the function to invoke.</param>
186
- /// <param name="argsJson">A JSON representation of the arguments.</param>
187
- /// <param name="byteArrays">Byte array data extracted from the arguments for direct transfer.</param>
188
- protected virtual void BeginInvokeJS ( long taskId , string identifier , string ? argsJson , byte [ ] [ ] ? byteArrays )
189
- => BeginInvokeJS ( taskId , identifier , argsJson , byteArrays , JSCallResultType . Default , 0 ) ;
161
+ /// <param name="serializedArgs">The serialized args containing the JSON representation along with the extracted byte arrays.</param>
162
+ protected virtual void BeginInvokeJS ( long taskId , string identifier , SerializedArgs serializedArgs )
163
+ => BeginInvokeJS ( taskId , identifier , serializedArgs , JSCallResultType . Default , 0 ) ;
190
164
191
165
/// <summary>
192
166
/// Begins an asynchronous function invocation.
193
167
/// </summary>
194
168
/// <param name="taskId">The identifier for the function invocation, or zero if no async callback is required.</param>
195
169
/// <param name="identifier">The identifier for the function to invoke.</param>
196
- /// <param name="argsJson">A JSON representation of the arguments.</param>
197
- /// <param name="byteArrays">Byte array data extracted from the arguments for direct transfer.</param>
170
+ /// <param name="serializedArgs">The serialized args containing the JSON representation along with the extracted byte arrays.</param>
198
171
/// <param name="resultType">The type of result expected from the invocation.</param>
199
172
/// <param name="targetInstanceId">The instance ID of the target JS object.</param>
200
- protected abstract void BeginInvokeJS ( long taskId , string identifier , string ? argsJson , byte [ ] [ ] ? byteArrays , JSCallResultType resultType , long targetInstanceId ) ;
173
+ protected abstract void BeginInvokeJS ( long taskId , string identifier , SerializedArgs serializedArgs , JSCallResultType resultType , long targetInstanceId ) ;
201
174
202
175
/// <summary>
203
176
/// Completes an async JS interop call from JavaScript to .NET
@@ -226,10 +199,10 @@ internal void EndInvokeJS(long taskId, bool succeeded, ref Utf8JsonReader jsonRe
226
199
{
227
200
var resultType = TaskGenericsUtil . GetTaskCompletionSourceResultType ( tcs ) ;
228
201
229
- ByteArraysToDeserialize = byteArrays ;
202
+ ByteArrayJsonConverter . ByteArraysToDeserialize = byteArrays ;
230
203
var result = JsonSerializer . Deserialize ( ref jsonReader , resultType , JsonSerializerOptions ) ;
231
204
TaskGenericsUtil . SetTaskCompletionSourceResult ( tcs , result ) ;
232
- ByteArraysToDeserialize = null ;
205
+ ByteArrayJsonConverter . ByteArraysToDeserialize = null ;
233
206
}
234
207
else
235
208
{
0 commit comments