Skip to content

Commit 93d3ae4

Browse files
authored
API Review: JSRuntime (#2166)
* API Review: JSRuntime * Rename JSRuntimeBase -> JSRuntime * Rename JSInProcessRuntimeBase -> JSInProcessRuntime * Rename DotNetObjectRef -> DotNetObjectReference * Update JSRuntime to return ValueTask * Make InvokeAsync APIs that explicitly cancels and API that default cancels more crisp * Introduce void invoking APIs * Fixup method names on DotNetDispatcher
1 parent d6bfc28 commit 93d3ae4

28 files changed

+1133
-748
lines changed

src/JSInterop/Microsoft.JSInterop/ref/Microsoft.JSInterop.netcoreapp3.0.cs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33

44
namespace Microsoft.JSInterop
55
{
6-
public static partial class DotNetDispatcher
6+
public static partial class DotNetObjectReference
77
{
8-
public static void BeginInvoke(string callId, string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { }
9-
public static void EndInvoke(string arguments) { }
10-
public static string Invoke(string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { throw null; }
8+
public static Microsoft.JSInterop.DotNetObjectReference<TValue> Create<TValue>(TValue value) where TValue : class { throw null; }
119
}
12-
public static partial class DotNetObjectRef
10+
public sealed partial class DotNetObjectReference<TValue> : System.IDisposable where TValue : class
1311
{
14-
public static Microsoft.JSInterop.DotNetObjectRef<TValue> Create<TValue>(TValue value) where TValue : class { throw null; }
15-
}
16-
public sealed partial class DotNetObjectRef<TValue> : System.IDisposable where TValue : class
17-
{
18-
internal DotNetObjectRef() { }
12+
internal DotNetObjectReference() { }
1913
public TValue Value { get { throw null; } }
2014
public void Dispose() { }
2115
}
@@ -25,38 +19,61 @@ public partial interface IJSInProcessRuntime : Microsoft.JSInterop.IJSRuntime
2519
}
2620
public partial interface IJSRuntime
2721
{
28-
System.Threading.Tasks.Task<TValue> InvokeAsync<TValue>(string identifier, System.Collections.Generic.IEnumerable<object> args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
29-
System.Threading.Tasks.Task<TValue> InvokeAsync<TValue>(string identifier, params object[] args);
22+
System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args);
23+
System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args);
3024
}
3125
public partial class JSException : System.Exception
3226
{
3327
public JSException(string message) { }
3428
public JSException(string message, System.Exception innerException) { }
3529
}
36-
public abstract partial class JSInProcessRuntimeBase : Microsoft.JSInterop.JSRuntimeBase, Microsoft.JSInterop.IJSInProcessRuntime, Microsoft.JSInterop.IJSRuntime
30+
public abstract partial class JSInProcessRuntime : Microsoft.JSInterop.JSRuntime, Microsoft.JSInterop.IJSInProcessRuntime, Microsoft.JSInterop.IJSRuntime
3731
{
38-
protected JSInProcessRuntimeBase() { }
32+
protected JSInProcessRuntime() { }
3933
protected abstract string InvokeJS(string identifier, string argsJson);
4034
public TValue Invoke<TValue>(string identifier, params object[] args) { throw null; }
4135
}
36+
public static partial class JSInProcessRuntimeExtensions
37+
{
38+
public static void InvokeVoid(this Microsoft.JSInterop.IJSInProcessRuntime jsRuntime, string identifier, params object[] args) { }
39+
}
4240
[System.AttributeUsageAttribute(System.AttributeTargets.Method, AllowMultiple=true)]
43-
public partial class JSInvokableAttribute : System.Attribute
41+
public sealed partial class JSInvokableAttribute : System.Attribute
4442
{
4543
public JSInvokableAttribute() { }
4644
public JSInvokableAttribute(string identifier) { }
4745
public string Identifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
4846
}
49-
public static partial class JSRuntime
50-
{
51-
public static void SetCurrentJSRuntime(Microsoft.JSInterop.IJSRuntime instance) { }
52-
}
53-
public abstract partial class JSRuntimeBase : Microsoft.JSInterop.IJSRuntime
47+
public abstract partial class JSRuntime : Microsoft.JSInterop.IJSRuntime
5448
{
55-
protected JSRuntimeBase() { }
49+
protected JSRuntime() { }
5650
protected System.TimeSpan? DefaultAsyncTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
5751
protected abstract void BeginInvokeJS(long taskId, string identifier, string argsJson);
5852
protected internal abstract void EndInvokeDotNet(string callId, bool success, object resultOrError, string assemblyName, string methodIdentifier, long dotNetObjectId);
59-
public System.Threading.Tasks.Task<T> InvokeAsync<T>(string identifier, System.Collections.Generic.IEnumerable<object> args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
60-
public System.Threading.Tasks.Task<T> InvokeAsync<T>(string identifier, params object[] args) { throw null; }
53+
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) { throw null; }
54+
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args) { throw null; }
55+
public static void SetCurrentJSRuntime(Microsoft.JSInterop.IJSRuntime instance) { }
56+
}
57+
public static partial class JSRuntimeExtensions
58+
{
59+
public static System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, params object[] args) { throw null; }
60+
public static System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.Threading.CancellationToken cancellationToken, params object[] args) { throw null; }
61+
[System.Diagnostics.DebuggerStepThroughAttribute]
62+
public static System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.TimeSpan timeout, params object[] args) { throw null; }
63+
[System.Diagnostics.DebuggerStepThroughAttribute]
64+
public static System.Threading.Tasks.ValueTask InvokeVoidAsync(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, params object[] args) { throw null; }
65+
[System.Diagnostics.DebuggerStepThroughAttribute]
66+
public static System.Threading.Tasks.ValueTask InvokeVoidAsync(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.Threading.CancellationToken cancellationToken, params object[] args) { throw null; }
67+
[System.Diagnostics.DebuggerStepThroughAttribute]
68+
public static System.Threading.Tasks.ValueTask InvokeVoidAsync(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.TimeSpan timeout, params object[] args) { throw null; }
69+
}
70+
}
71+
namespace Microsoft.JSInterop.Infrastructure
72+
{
73+
public static partial class DotNetDispatcher
74+
{
75+
public static void BeginInvokeDotNet(string callId, string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { }
76+
public static void EndInvokeJS(string arguments) { }
77+
public static string Invoke(string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { throw null; }
6178
}
6279
}

src/JSInterop/Microsoft.JSInterop/ref/Microsoft.JSInterop.netstandard2.0.cs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33

44
namespace Microsoft.JSInterop
55
{
6-
public static partial class DotNetDispatcher
6+
public static partial class DotNetObjectReference
77
{
8-
public static void BeginInvoke(string callId, string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { }
9-
public static void EndInvoke(string arguments) { }
10-
public static string Invoke(string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { throw null; }
8+
public static Microsoft.JSInterop.DotNetObjectReference<TValue> Create<TValue>(TValue value) where TValue : class { throw null; }
119
}
12-
public static partial class DotNetObjectRef
10+
public sealed partial class DotNetObjectReference<TValue> : System.IDisposable where TValue : class
1311
{
14-
public static Microsoft.JSInterop.DotNetObjectRef<TValue> Create<TValue>(TValue value) where TValue : class { throw null; }
15-
}
16-
public sealed partial class DotNetObjectRef<TValue> : System.IDisposable where TValue : class
17-
{
18-
internal DotNetObjectRef() { }
12+
internal DotNetObjectReference() { }
1913
public TValue Value { get { throw null; } }
2014
public void Dispose() { }
2115
}
@@ -25,38 +19,61 @@ public partial interface IJSInProcessRuntime : Microsoft.JSInterop.IJSRuntime
2519
}
2620
public partial interface IJSRuntime
2721
{
28-
System.Threading.Tasks.Task<TValue> InvokeAsync<TValue>(string identifier, System.Collections.Generic.IEnumerable<object> args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
29-
System.Threading.Tasks.Task<TValue> InvokeAsync<TValue>(string identifier, params object[] args);
22+
System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args);
23+
System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args);
3024
}
3125
public partial class JSException : System.Exception
3226
{
3327
public JSException(string message) { }
3428
public JSException(string message, System.Exception innerException) { }
3529
}
36-
public abstract partial class JSInProcessRuntimeBase : Microsoft.JSInterop.JSRuntimeBase, Microsoft.JSInterop.IJSInProcessRuntime, Microsoft.JSInterop.IJSRuntime
30+
public abstract partial class JSInProcessRuntime : Microsoft.JSInterop.JSRuntime, Microsoft.JSInterop.IJSInProcessRuntime, Microsoft.JSInterop.IJSRuntime
3731
{
38-
protected JSInProcessRuntimeBase() { }
32+
protected JSInProcessRuntime() { }
3933
protected abstract string InvokeJS(string identifier, string argsJson);
4034
public TValue Invoke<TValue>(string identifier, params object[] args) { throw null; }
4135
}
36+
public static partial class JSInProcessRuntimeExtensions
37+
{
38+
public static void InvokeVoid(this Microsoft.JSInterop.IJSInProcessRuntime jsRuntime, string identifier, params object[] args) { }
39+
}
4240
[System.AttributeUsageAttribute(System.AttributeTargets.Method, AllowMultiple=true)]
43-
public partial class JSInvokableAttribute : System.Attribute
41+
public sealed partial class JSInvokableAttribute : System.Attribute
4442
{
4543
public JSInvokableAttribute() { }
4644
public JSInvokableAttribute(string identifier) { }
4745
public string Identifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
4846
}
49-
public static partial class JSRuntime
50-
{
51-
public static void SetCurrentJSRuntime(Microsoft.JSInterop.IJSRuntime instance) { }
52-
}
53-
public abstract partial class JSRuntimeBase : Microsoft.JSInterop.IJSRuntime
47+
public abstract partial class JSRuntime : Microsoft.JSInterop.IJSRuntime
5448
{
55-
protected JSRuntimeBase() { }
49+
protected JSRuntime() { }
5650
protected System.TimeSpan? DefaultAsyncTimeout { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
5751
protected abstract void BeginInvokeJS(long taskId, string identifier, string argsJson);
5852
protected internal abstract void EndInvokeDotNet(string callId, bool success, object resultOrError, string assemblyName, string methodIdentifier, long dotNetObjectId);
59-
public System.Threading.Tasks.Task<T> InvokeAsync<T>(string identifier, System.Collections.Generic.IEnumerable<object> args, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
60-
public System.Threading.Tasks.Task<T> InvokeAsync<T>(string identifier, params object[] args) { throw null; }
53+
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) { throw null; }
54+
public System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(string identifier, System.Threading.CancellationToken cancellationToken, object[] args) { throw null; }
55+
public static void SetCurrentJSRuntime(Microsoft.JSInterop.IJSRuntime instance) { }
56+
}
57+
public static partial class JSRuntimeExtensions
58+
{
59+
public static System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, params object[] args) { throw null; }
60+
public static System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.Threading.CancellationToken cancellationToken, params object[] args) { throw null; }
61+
[System.Diagnostics.DebuggerStepThroughAttribute]
62+
public static System.Threading.Tasks.ValueTask<TValue> InvokeAsync<TValue>(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.TimeSpan timeout, params object[] args) { throw null; }
63+
[System.Diagnostics.DebuggerStepThroughAttribute]
64+
public static System.Threading.Tasks.ValueTask InvokeVoidAsync(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, params object[] args) { throw null; }
65+
[System.Diagnostics.DebuggerStepThroughAttribute]
66+
public static System.Threading.Tasks.ValueTask InvokeVoidAsync(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.Threading.CancellationToken cancellationToken, params object[] args) { throw null; }
67+
[System.Diagnostics.DebuggerStepThroughAttribute]
68+
public static System.Threading.Tasks.ValueTask InvokeVoidAsync(this Microsoft.JSInterop.IJSRuntime jsRuntime, string identifier, System.TimeSpan timeout, params object[] args) { throw null; }
69+
}
70+
}
71+
namespace Microsoft.JSInterop.Infrastructure
72+
{
73+
public static partial class DotNetDispatcher
74+
{
75+
public static void BeginInvokeDotNet(string callId, string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { }
76+
public static void EndInvokeJS(string arguments) { }
77+
public static string Invoke(string assemblyName, string methodIdentifier, long dotNetObjectId, string argsJson) { throw null; }
6178
}
6279
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using Microsoft.JSInterop.Infrastructure;
5+
46
namespace Microsoft.JSInterop
57
{
68
/// <summary>
7-
/// Provides convenience methods to produce a <see cref="DotNetObjectRef{TValue}" />.
9+
/// Provides convenience methods to produce a <see cref="DotNetObjectReference{TValue}" />.
810
/// </summary>
9-
public static class DotNetObjectRef
11+
public static class DotNetObjectReference
1012
{
1113
/// <summary>
12-
/// Creates a new instance of <see cref="DotNetObjectRef{TValue}" />.
14+
/// Creates a new instance of <see cref="DotNetObjectReference{TValue}" />.
1315
/// </summary>
1416
/// <param name="value">The reference type to track.</param>
15-
/// <returns>An instance of <see cref="DotNetObjectRef{TValue}" />.</returns>
16-
public static DotNetObjectRef<TValue> Create<TValue>(TValue value) where TValue : class
17+
/// <returns>An instance of <see cref="DotNetObjectReference{TValue}" />.</returns>
18+
public static DotNetObjectReference<TValue> Create<TValue>(TValue value) where TValue : class
1719
{
18-
return new DotNetObjectRef<TValue>(DotNetObjectRefManager.Current, value);
20+
return new DotNetObjectReference<TValue>(DotNetObjectReferenceManager.Current, value);
1921
}
2022
}
2123
}

src/JSInterop/Microsoft.JSInterop/src/DotNetObjectRefOfT.cs renamed to src/JSInterop/Microsoft.JSInterop/src/DotNetObjectReferenceOfT.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Text.Json.Serialization;
6+
using Microsoft.JSInterop.Infrastructure;
67

78
namespace Microsoft.JSInterop
89
{
@@ -14,31 +15,24 @@ namespace Microsoft.JSInterop
1415
/// </summary>
1516
/// <typeparam name="TValue">The type of the value to wrap.</typeparam>
1617
[JsonConverter(typeof(DotNetObjectReferenceJsonConverterFactory))]
17-
public sealed class DotNetObjectRef<TValue> : IDotNetObjectRef, IDisposable where TValue : class
18+
public sealed class DotNetObjectReference<TValue> : IDotNetObjectReference, IDisposable where TValue : class
1819
{
19-
private readonly DotNetObjectRefManager _referenceManager;
20+
private readonly DotNetObjectReferenceManager _referenceManager;
2021
private readonly TValue _value;
2122
private readonly long _objectId;
2223

2324
/// <summary>
24-
/// Initializes a new instance of <see cref="DotNetObjectRef{TValue}" />.
25+
/// Initializes a new instance of <see cref="DotNetObjectReference{TValue}" />.
2526
/// </summary>
2627
/// <param name="referenceManager"></param>
2728
/// <param name="value">The value to pass by reference.</param>
28-
internal DotNetObjectRef(DotNetObjectRefManager referenceManager, TValue value)
29+
internal DotNetObjectReference(DotNetObjectReferenceManager referenceManager, TValue value)
2930
{
3031
_referenceManager = referenceManager;
3132
_objectId = _referenceManager.TrackObject(this);
3233
_value = value;
3334
}
3435

35-
internal DotNetObjectRef(DotNetObjectRefManager referenceManager, long objectId, TValue value)
36-
{
37-
_referenceManager = referenceManager;
38-
_objectId = objectId;
39-
_value = value;
40-
}
41-
4236
/// <summary>
4337
/// Gets the object instance represented by this wrapper.
4438
/// </summary>
@@ -60,7 +54,7 @@ internal long ObjectId
6054
}
6155
}
6256

63-
object IDotNetObjectRef.Value => Value;
57+
object IDotNetObjectReference.Value => Value;
6458

6559
internal bool Disposed { get; private set; }
6660

0 commit comments

Comments
 (0)