Skip to content

Commit 28c8d39

Browse files
committed
Minor code review changes
1 parent 2f84ea0 commit 28c8d39

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#nullable enable
2+
override Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.BeginInvokeJS(long taskId, string! identifier, string? argsJson, Microsoft.JSInterop.JSCallResultType resultType, long targetInstanceId) -> void
23
override Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.BeginInvokeJS(Microsoft.JSInterop.Infrastructure.JSInvocationInfo! invocationInfo) -> void
3-
override Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.InvokeJS(Microsoft.JSInterop.Infrastructure.JSInvocationInfo! invocationInfo) -> string?
4+
override Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime.InvokeJS(Microsoft.JSInterop.Infrastructure.JSInvocationInfo! invocationInfo) -> string!

src/Components/WebAssembly/JSInterop/src/WebAssemblyJSRuntime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ protected override string InvokeJS(string identifier, [StringSyntax(StringSyntax
3535
ArgsJson = argsJson,
3636
};
3737

38-
return InvokeJS(invocationInfo) ?? "";
38+
return InvokeJS(invocationInfo);
3939
}
4040

4141
/// <inheritdoc />
42-
protected override string? InvokeJS(JSInvocationInfo invocationInfo)
42+
protected override string InvokeJS(JSInvocationInfo invocationInfo)
4343
{
4444
var invocationInfoJson = invocationInfo.ToJson();
4545

src/JSInterop/Microsoft.JSInterop.JS/src/src/Microsoft.JSInterop.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export module DotNet {
3535
}
3636

3737
class JSObject {
38-
// TODO(oroztocil): Is it correct to cache functions/properties when they can change?
38+
// We cache resolved members. Note that this means we can return stale values if the object has been modified since then.
3939
_cachedMembers: Map<string, ObjectMemberDescriptor>;
4040

4141
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -78,7 +78,8 @@ export module DotNet {
7878

7979
const result = { parent, name: lastKey } as ObjectMemberDescriptor;
8080

81-
if (typeof current === "function") {
81+
if (current instanceof Function) {
82+
// If the member is a function, we bind the parent object as its "this" context.
8283
result.func = current.bind(parent);
8384
}
8485

@@ -452,15 +453,15 @@ export module DotNet {
452453
try {
453454
const valueOrPromise = this.handleJSCall(targetInstanceId, identifier, callType, argsJson);
454455

455-
// We only listen for a result if the caller wants to be notified about it
456+
// We only await the result if the caller wants to be notified about it
456457
if (asyncHandle) {
457-
// On completion, dispatch result back to .NET
458458
const result = await valueOrPromise;
459459
const serializedResult = stringifyArgs(this, [
460460
asyncHandle,
461461
true,
462462
createJSCallResult(result, resultType)
463463
]);
464+
// On success, dispatch result back to .NET
464465
this._dotNetCallDispatcher.endInvokeJSFromDotNet(asyncHandle, true, serializedResult);
465466
}
466467
} catch(error: any) {
@@ -470,6 +471,7 @@ export module DotNet {
470471
false,
471472
formatError(error)
472473
]);
474+
// On failure, dispatch error back to .NET
473475
this._dotNetCallDispatcher.endInvokeJSFromDotNet(asyncHandle, false, serializedError);
474476
}
475477
}
@@ -496,15 +498,15 @@ export module DotNet {
496498
}
497499

498500
handleJSFunctionCall(identifier: string, func: any, args: unknown[]): any {
499-
if (typeof func === "function") {
501+
if (func instanceof Function) {
500502
return func(...args);
501503
} else {
502504
throw new Error(`The value '${identifier}' is not a function.`);
503505
}
504506
}
505507

506508
handleJSNewCall(identifier: string, func: any, args: unknown[]): any {
507-
if (typeof func === "function") {
509+
if (func instanceof Function) {
508510
try {
509511
// The new expression throws if the function is not constructible (e.g. an arrow function).
510512
return new func(...args);

src/JSInterop/Microsoft.JSInterop/src/Infrastructure/JSInvocationInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.JSInterop.Infrastructure;
1010
/// <summary>
1111
/// Configuration of an interop call from .NET to JavaScript.
1212
/// </summary>
13-
public sealed class JSInvocationInfo
13+
public readonly struct JSInvocationInfo
1414
{
1515
/// <summary>
1616
/// The identifier for the interop call, or zero if no async callback is required.

0 commit comments

Comments
 (0)