Skip to content

Commit b0b5a6b

Browse files
committed
Update signatures to leverage SerializedArgs
1 parent cdba736 commit b0b5a6b

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

src/Components/Server/src/Circuits/CircuitHost.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.AspNetCore.SignalR;
1313
using Microsoft.Extensions.DependencyInjection;
1414
using Microsoft.Extensions.Logging;
15+
using Microsoft.JSInterop;
1516
using Microsoft.JSInterop.Infrastructure;
1617

1718
namespace Microsoft.AspNetCore.Components.Server.Circuits
@@ -336,7 +337,7 @@ public async Task OnRenderCompletedAsync(long renderId, string errorMessageOrNul
336337

337338
// BeginInvokeDotNetFromJS is used in a fire-and-forget context, so it's responsible for its own
338339
// error handling.
339-
public async Task BeginInvokeDotNetFromJS(string callId, string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson, byte[][]? byteArrays)
340+
public async Task BeginInvokeDotNetFromJS(string callId, string assemblyName, string methodIdentifier, long dotNetObjectId, SerializedArgs serializedArgs)
340341
{
341342
AssertInitialized();
342343
AssertNotDisposed();
@@ -347,7 +348,7 @@ await Renderer.Dispatcher.InvokeAsync(() =>
347348
{
348349
Log.BeginInvokeDotNet(_logger, callId, assemblyName, methodIdentifier, dotNetObjectId);
349350
var invocationInfo = new DotNetInvocationInfo(assemblyName, methodIdentifier, dotNetObjectId, callId);
350-
DotNetDispatcher.BeginInvokeDotNet(JSRuntime, invocationInfo, argsJson, byteArrays);
351+
DotNetDispatcher.BeginInvokeDotNet(JSRuntime, invocationInfo, serializedArgs);
351352
});
352353
}
353354
catch (Exception ex)
@@ -362,7 +363,7 @@ await Renderer.Dispatcher.InvokeAsync(() =>
362363

363364
// EndInvokeJSFromDotNet is used in a fire-and-forget context, so it's responsible for its own
364365
// error handling.
365-
public async Task EndInvokeJSFromDotNet(long callId, bool succeeded, string resultOrError, byte[][]? byteArrays)
366+
public async Task EndInvokeJSFromDotNet(long callId, bool succeeded, SerializedArgs serializedArgs)
366367
{
367368
AssertInitialized();
368369
AssertNotDisposed();
@@ -374,14 +375,14 @@ await Renderer.Dispatcher.InvokeAsync(() =>
374375
if (!succeeded)
375376
{
376377
// We can log the arguments here because it is simply the JS error with the call stack.
377-
Log.EndInvokeJSFailed(_logger, callId, resultOrError);
378+
Log.EndInvokeJSFailed(_logger, callId, serializedArgs.ArgsJson);
378379
}
379380
else
380381
{
381382
Log.EndInvokeJSSucceeded(_logger, callId);
382383
}
383384

384-
DotNetDispatcher.EndInvokeJS(JSRuntime, resultOrError, byteArrays);
385+
DotNetDispatcher.EndInvokeJS(JSRuntime, serializedArgs);
385386
});
386387
}
387388
catch (Exception ex)

src/Components/Server/src/ComponentHub.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Http;
1010
using Microsoft.AspNetCore.SignalR;
1111
using Microsoft.Extensions.Logging;
12+
using Microsoft.JSInterop;
1213

1314
namespace Microsoft.AspNetCore.Components.Server
1415
{
@@ -195,7 +196,8 @@ public async ValueTask BeginInvokeDotNetFromJS(string callId, string assemblyNam
195196
return;
196197
}
197198

198-
_ = circuitHost.BeginInvokeDotNetFromJS(callId, assemblyName, methodIdentifier, dotNetObjectId, argsJson, byteArrays);
199+
var serializedArgs = new SerializedArgs(argsJson, byteArrays);
200+
_ = circuitHost.BeginInvokeDotNetFromJS(callId, assemblyName, methodIdentifier, dotNetObjectId, serializedArgs);
199201
}
200202

201203
public async ValueTask EndInvokeJSFromDotNet(long callId, bool succeeded, string resultOrError, byte[][] byteArrays)
@@ -206,7 +208,8 @@ public async ValueTask EndInvokeJSFromDotNet(long callId, bool succeeded, string
206208
return;
207209
}
208210

209-
_ = circuitHost.EndInvokeJSFromDotNet(callId, succeeded, resultOrError, byteArrays);
211+
var serializedArgs = new SerializedArgs(resultOrError, byteArrays);
212+
_ = circuitHost.EndInvokeJSFromDotNet(callId, succeeded, serializedArgs);
210213
}
211214

212215
public async ValueTask DispatchBrowserEvent(string eventDescriptor, string eventArgs)

src/JSInterop/Microsoft.JSInterop/src/Infrastructure/DotNetDispatcher.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ public static class DotNetDispatcher
3535
/// </summary>
3636
/// <param name="jsRuntime">The <see cref="JSRuntime"/>.</param>
3737
/// <param name="invocationInfo">The <see cref="DotNetInvocationInfo"/>.</param>
38-
/// <param name="argsJson">A JSON representation of the parameters.</param>
39-
/// <param name="byteArrays">Byte array data extracted from the arguments for direct transfer.</param>
38+
/// <param name="serializedArgs">The serialized args containing the JSON representation along with the extracted byte arrays.</param>
4039
/// <returns>A record containing the JSON representation of the return value, or null, along with the extracted byte arrays, or null.</returns>
41-
public static SerializedArgs Invoke(JSRuntime jsRuntime, in DotNetInvocationInfo invocationInfo, string argsJson, byte[][]? byteArrays)
40+
public static SerializedArgs Invoke(JSRuntime jsRuntime, in DotNetInvocationInfo invocationInfo, SerializedArgs serializedArgs)
4241
{
4342
// This method doesn't need [JSInvokable] because the platform is responsible for having
4443
// some way to dispatch calls here. The logic inside here is the thing that checks whether
@@ -51,7 +50,7 @@ public static SerializedArgs Invoke(JSRuntime jsRuntime, in DotNetInvocationInfo
5150
targetInstance = jsRuntime.GetObjectReference(invocationInfo.DotNetObjectId);
5251
}
5352

54-
var syncResult = InvokeSynchronously(jsRuntime, invocationInfo, targetInstance, argsJson, byteArrays);
53+
var syncResult = InvokeSynchronously(jsRuntime, invocationInfo, targetInstance, serializedArgs);
5554
if (syncResult == null)
5655
{
5756
return new SerializedArgs(null, null);
@@ -65,9 +64,8 @@ public static SerializedArgs Invoke(JSRuntime jsRuntime, in DotNetInvocationInfo
6564
/// </summary>
6665
/// <param name="jsRuntime">The <see cref="JSRuntime"/>.</param>
6766
/// <param name="invocationInfo">The <see cref="DotNetInvocationInfo"/>.</param>
68-
/// <param name="argsJson">A JSON representation of the parameters.</param>
69-
/// <param name="byteArrays">Byte array data extracted from the arguments for direct transfer.</param>
70-
public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo invocationInfo, string argsJson, byte[][]? byteArrays)
67+
/// <param name="serializedArgs">The serialized args containing the JSON representation along with the extracted byte arrays.</param>
68+
public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo invocationInfo, SerializedArgs serializedArgs)
7169
{
7270
// This method doesn't need [JSInvokable] because the platform is responsible for having
7371
// some way to dispatch calls here. The logic inside here is the thing that checks whether
@@ -89,7 +87,7 @@ public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo i
8987
targetInstance = jsRuntime.GetObjectReference(invocationInfo.DotNetObjectId);
9088
}
9189

92-
syncResult = InvokeSynchronously(jsRuntime, invocationInfo, targetInstance, argsJson, byteArrays);
90+
syncResult = InvokeSynchronously(jsRuntime, invocationInfo, targetInstance, serializedArgs);
9391
}
9492
catch (Exception ex)
9593
{
@@ -114,9 +112,8 @@ public static void BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo i
114112
}
115113
else
116114
{
117-
118-
var serializedArgs = SerializeArgs(jsRuntime, syncResult);
119-
var dispatchResult = new DotNetInvocationResult(serializedArgs);
115+
var serializedReturnArgs = SerializeArgs(jsRuntime, syncResult);
116+
var dispatchResult = new DotNetInvocationResult(serializedReturnArgs);
120117
jsRuntime.EndInvokeDotNet(invocationInfo, dispatchResult);
121118
}
122119
}
@@ -135,7 +132,7 @@ private static void EndInvokeDotNetAfterTask(Task task, JSRuntime jsRuntime, in
135132
jsRuntime.EndInvokeDotNet(invocationInfo, new DotNetInvocationResult(serializedArgs));
136133
}
137134

138-
private static object? InvokeSynchronously(JSRuntime jsRuntime, in DotNetInvocationInfo callInfo, IDotNetObjectReference? objectReference, string argsJson, byte[][]? byteArrays)
135+
private static object? InvokeSynchronously(JSRuntime jsRuntime, in DotNetInvocationInfo callInfo, IDotNetObjectReference? objectReference, SerializedArgs serializedArgs)
139136
{
140137
var assemblyName = callInfo.AssemblyName;
141138
var methodIdentifier = callInfo.MethodIdentifier;
@@ -165,7 +162,7 @@ private static void EndInvokeDotNetAfterTask(Task task, JSRuntime jsRuntime, in
165162
(methodInfo, parameterTypes) = GetCachedMethodInfo(objectReference, methodIdentifier);
166163
}
167164

168-
var suppliedArgs = ParseArguments(jsRuntime, methodIdentifier, argsJson, byteArrays, parameterTypes);
165+
var suppliedArgs = ParseArguments(jsRuntime, methodIdentifier, serializedArgs, parameterTypes);
169166

170167
try
171168
{
@@ -184,14 +181,14 @@ private static void EndInvokeDotNetAfterTask(Task task, JSRuntime jsRuntime, in
184181
}
185182
}
186183

187-
internal static object?[] ParseArguments(JSRuntime jsRuntime, string methodIdentifier, string arguments, byte[][]? byteArrays, Type[] parameterTypes)
184+
internal static object?[] ParseArguments(JSRuntime jsRuntime, string methodIdentifier, SerializedArgs serializedArgs, Type[] parameterTypes)
188185
{
189186
if (parameterTypes.Length == 0)
190187
{
191188
return Array.Empty<object>();
192189
}
193190

194-
var utf8JsonBytes = Encoding.UTF8.GetBytes(arguments);
191+
var utf8JsonBytes = Encoding.UTF8.GetBytes(serializedArgs.ArgsJson ?? string.Empty);
195192
var reader = new Utf8JsonReader(utf8JsonBytes);
196193
if (!reader.Read() || reader.TokenType != JsonTokenType.StartArray)
197194
{
@@ -200,7 +197,7 @@ private static void EndInvokeDotNetAfterTask(Task task, JSRuntime jsRuntime, in
200197

201198
var suppliedArgs = new object?[parameterTypes.Length];
202199

203-
jsRuntime.ByteArrayJsonConverter.ByteArraysToDeserialize = byteArrays;
200+
jsRuntime.ByteArrayJsonConverter.ByteArraysToDeserialize = serializedArgs.ByteArrays;
204201

205202
var index = 0;
206203
while (index < parameterTypes.Length && reader.Read() && reader.TokenType != JsonTokenType.EndArray)
@@ -263,15 +260,14 @@ static bool IsIncorrectDotNetObjectRefUse(Type parameterType, Utf8JsonReader jso
263260
/// passed in as parameters.
264261
/// </remarks>
265262
/// <param name="jsRuntime">The <see cref="JSRuntime"/>.</param>
266-
/// <param name="arguments">The serialized arguments for the callback completion.</param>
267-
/// <param name="byteArrays">Byte array data extracted from the arguments for direct transfer.</param>
263+
/// <param name="serializedArgs">The serialized args containing the JSON representation along with the extracted byte arrays.</param>
268264
/// <exception cref="Exception">
269265
/// This method can throw any exception either from the argument received or as a result
270266
/// of executing any callback synchronously upon completion.
271267
/// </exception>
272-
public static void EndInvokeJS(JSRuntime jsRuntime, string arguments, byte[][]? byteArrays)
268+
public static void EndInvokeJS(JSRuntime jsRuntime, SerializedArgs serializedArgs)
273269
{
274-
var utf8JsonBytes = Encoding.UTF8.GetBytes(arguments);
270+
var utf8JsonBytes = Encoding.UTF8.GetBytes(serializedArgs.ArgsJson ?? string.Empty);
275271

276272
// The payload that we're trying to parse is of the format
277273
// [ taskId: long, success: boolean, value: string? | object ]
@@ -292,7 +288,7 @@ public static void EndInvokeJS(JSRuntime jsRuntime, string arguments, byte[][]?
292288
var success = reader.GetBoolean();
293289

294290
reader.Read();
295-
jsRuntime.EndInvokeJS(taskId, success, ref reader, byteArrays);
291+
jsRuntime.EndInvokeJS(taskId, success, ref reader, serializedArgs.ByteArrays);
296292

297293
if (!reader.Read() || reader.TokenType != JsonTokenType.EndArray)
298294
{

src/JSInterop/Microsoft.JSInterop/src/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ static Microsoft.JSInterop.Implementation.JSObjectReferenceJsonWorker.WriteJSObj
1414
*REMOVED*Microsoft.JSInterop.Infrastructure.DotNetInvocationResult.DotNetInvocationResult(object? result) -> void
1515
*REMOVED*Microsoft.JSInterop.Infrastructure.DotNetInvocationResult.Result.get -> object?
1616
*REMOVED*static Microsoft.JSInterop.JSInProcessObjectReferenceExtensions.InvokeVoid(this Microsoft.JSInterop.IJSInProcessObjectReference! jsObjectReference, string! identifier, params object?[]! args) -> void
17-
static Microsoft.JSInterop.Infrastructure.DotNetDispatcher.BeginInvokeDotNet(Microsoft.JSInterop.JSRuntime! jsRuntime, Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string! argsJson, byte[]![]? byteArrays) -> void
18-
static Microsoft.JSInterop.Infrastructure.DotNetDispatcher.EndInvokeJS(Microsoft.JSInterop.JSRuntime! jsRuntime, string! arguments, byte[]![]? byteArrays) -> void
19-
static Microsoft.JSInterop.Infrastructure.DotNetDispatcher.Invoke(Microsoft.JSInterop.JSRuntime! jsRuntime, in Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, string! argsJson, byte[]![]? byteArrays) -> Microsoft.JSInterop.SerializedArgs
17+
static Microsoft.JSInterop.Infrastructure.DotNetDispatcher.BeginInvokeDotNet(Microsoft.JSInterop.JSRuntime! jsRuntime, Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, Microsoft.JSInterop.SerializedArgs serializedArgs) -> void
18+
static Microsoft.JSInterop.Infrastructure.DotNetDispatcher.EndInvokeJS(Microsoft.JSInterop.JSRuntime! jsRuntime, Microsoft.JSInterop.SerializedArgs serializedArgs) -> void
19+
static Microsoft.JSInterop.Infrastructure.DotNetDispatcher.Invoke(Microsoft.JSInterop.JSRuntime! jsRuntime, in Microsoft.JSInterop.Infrastructure.DotNetInvocationInfo invocationInfo, Microsoft.JSInterop.SerializedArgs serializedArgs) -> Microsoft.JSInterop.SerializedArgs
2020
static Microsoft.JSInterop.JSInProcessObjectReferenceExtensions.InvokeVoid(this Microsoft.JSInterop.IJSInProcessObjectReference! jsObjectReference, string! identifier, params object?[]? args) -> void
2121
*REMOVED*static Microsoft.JSInterop.JSInProcessRuntimeExtensions.InvokeVoid(this Microsoft.JSInterop.IJSInProcessRuntime! jsRuntime, string! identifier, params object?[]! args) -> void
2222
static Microsoft.JSInterop.JSInProcessRuntimeExtensions.InvokeVoid(this Microsoft.JSInterop.IJSInProcessRuntime! jsRuntime, string! identifier, params object?[]? args) -> void

0 commit comments

Comments
 (0)