Skip to content

Commit 93d3006

Browse files
committed
Add missing extension overloads, clean up code
1 parent c7f49d6 commit 93d3006

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

src/Components/Endpoints/src/DependencyInjection/UnsupportedJavaScriptRuntime.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics.CodeAnalysis;
54
using Microsoft.JSInterop;
65

76
namespace Microsoft.AspNetCore.Components.Endpoints;
@@ -22,15 +21,15 @@ public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, object?[]
2221
public ValueTask<IJSObjectReference> InvokeNewAsync(string identifier, CancellationToken cancellationToken, object?[]? args)
2322
=> throw new InvalidOperationException(Message);
2423

25-
public ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier)
24+
public ValueTask<TValue> GetValueAsync<TValue>(string identifier)
2625
=> throw new InvalidOperationException(Message);
2726

28-
public ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, CancellationToken cancellationToken)
27+
public ValueTask<TValue> GetValueAsync<TValue>(string identifier, CancellationToken cancellationToken)
2928
=> throw new InvalidOperationException(Message);
3029

31-
public ValueTask SetValueAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, TValue value)
30+
public ValueTask SetValueAsync<TValue>(string identifier, TValue value)
3231
=> throw new InvalidOperationException(Message);
3332

34-
public ValueTask SetValueAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, TValue value, CancellationToken cancellationToken)
33+
public ValueTask SetValueAsync<TValue>(string identifier, TValue value, CancellationToken cancellationToken)
3534
=> throw new InvalidOperationException(Message);
3635
}

src/Components/test/testassets/BasicTestApp/DotNetToJSInterop.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@
133133
private async Task GetObjectModelWithInvoke()
134134
{
135135
var model = await JSRuntime.InvokeAsync<TestObjectModel>("getTestObject");
136-
TestObjectDisplay = $"Serialized state: {model.Num} | {model.Text} | {model.GetOnlyProperty}";
136+
TestObjectDisplay = $"State loaded from model with Invoke: {model.Num} | {model.Text} | {model.GetOnlyProperty}";
137137
}
138138

139139
private async Task GetObjectModelWithGetValue()
140140
{
141141
var objectRef = await JSRuntime.GetValueAsync<IJSObjectReference>("testObject");
142142
var model = await objectRef.GetValueAsync<TestObjectModel>();
143-
TestObjectDisplay = $"Serialized state via reference: {model.Num} | {model.Text} | {model.GetOnlyProperty}";
143+
TestObjectDisplay = $"State loaded from reference with GetValue: {model.Num} | {model.Text} | {model.GetOnlyProperty}";
144144
}
145145

146146
private async Task GetObjectPropertiesWithGetValue()
@@ -149,7 +149,7 @@
149149
var numValue = await objectRef.GetValueAsync<int>("num");
150150
var textValue = await objectRef.GetValueAsync<string>("text");
151151
var getOnlyProperty = await objectRef.GetValueAsync<int>("getOnlyProperty");
152-
TestObjectDisplay = $"State via reference from function: {numValue} | {textValue} | {getOnlyProperty}";
152+
TestObjectDisplay = $"State loaded from properties with GetValue: {numValue} | {textValue} | {getOnlyProperty}";
153153
}
154154

155155
private async Task CreateInstanceByConstructorFunction()

src/JSInterop/Microsoft.JSInterop/src/JSObjectReferenceExtensions.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,54 @@ public static async ValueTask InvokeVoidAsync(this IJSObjectReference jsObjectRe
117117

118118
await jsObjectReference.InvokeAsync<IJSVoidResult>(identifier, cancellationToken, args);
119119
}
120+
121+
/// <summary>
122+
/// Invokes the specified JavaScript constructor function asynchronously. The function is invoked with the <c>new</c> operator.
123+
/// </summary>
124+
/// <param name="jsObjectReference">The <see cref="IJSObjectReference"/>.</param>
125+
/// <param name="identifier">An identifier for the constructor function to invoke. For example, the value <c>"someScope.SomeClass"</c> will invoke the constructor <c>someScope.SomeClass</c>.</param>
126+
/// <param name="args">JSON-serializable arguments.</param>
127+
/// <returns>An <see cref="IJSObjectReference"/> instance that represents the created JS object.</returns>
128+
public static ValueTask<IJSObjectReference> InvokeNewAsync(this IJSObjectReference jsObjectReference, string identifier, params object?[]? args)
129+
{
130+
ArgumentNullException.ThrowIfNull(jsObjectReference);
131+
132+
return jsObjectReference.InvokeNewAsync(identifier, args);
133+
}
134+
135+
/// <summary>
136+
/// Invokes the specified JavaScript constructor function asynchronously. The function is invoked with the <c>new</c> operator.
137+
/// </summary>
138+
/// <param name="jsObjectReference">The <see cref="IJSObjectReference"/>.</param>
139+
/// <param name="identifier">An identifier for the constructor function to invoke. For example, the value <c>"someScope.SomeClass"</c> will invoke the constructor <c>someScope.SomeClass</c>.</param>
140+
/// <param name="cancellationToken">
141+
/// A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts
142+
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
143+
/// </param>
144+
/// <param name="args">JSON-serializable arguments.</param>
145+
/// <returns>An <see cref="IJSObjectReference"/> instance that represents the created JS object.</returns>
146+
public static ValueTask<IJSObjectReference> InvokeNewAsync(this IJSObjectReference jsObjectReference, string identifier, CancellationToken cancellationToken, object?[]? args)
147+
{
148+
ArgumentNullException.ThrowIfNull(jsObjectReference);
149+
150+
return jsObjectReference.InvokeNewAsync(identifier, cancellationToken, args);
151+
}
152+
153+
/// <summary>
154+
/// Invokes the specified JavaScript constructor function asynchronously. The function is invoked with the <c>new</c> operator.
155+
/// </summary>
156+
/// <param name="jsObjectReference">The <see cref="IJSObjectReference"/>.</param>
157+
/// <param name="identifier">An identifier for the constructor function to invoke. For example, the value <c>"someScope.SomeClass"</c> will invoke the constructor <c>someScope.SomeClass</c>.</param>
158+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
159+
/// <param name="args">JSON-serializable arguments.</param>
160+
/// <returns>An <see cref="IJSObjectReference"/> instance that represents the created JS object.</returns>
161+
public static ValueTask<IJSObjectReference> InvokeNewAsync(this IJSObjectReference jsObjectReference, string identifier, TimeSpan timeout, object?[]? args)
162+
{
163+
ArgumentNullException.ThrowIfNull(jsObjectReference);
164+
165+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
166+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
167+
168+
return jsObjectReference.InvokeNewAsync(identifier, cancellationToken, args);
169+
}
120170
}

src/JSInterop/Microsoft.JSInterop/src/JSRuntime.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,17 @@ private void CleanupTasksAndRegistrations(long taskId)
188188
/// <param name="argsJson">A JSON representation of the arguments.</param>
189189
protected virtual void BeginInvokeJS(long taskId, string identifier, [StringSyntax(StringSyntaxAttribute.Json)] string? argsJson)
190190
{
191-
BeginInvokeJS(new JSInvocationInfo
191+
var invocationInfo = new JSInvocationInfo
192192
{
193193
AsyncHandle = taskId,
194194
TargetInstanceId = 0,
195195
Identifier = identifier,
196196
CallType = JSCallType.FunctionCall,
197197
ResultType = JSCallResultType.Default,
198198
ArgsJson = argsJson,
199-
});
199+
};
200+
201+
BeginInvokeJS(invocationInfo);
200202
}
201203

202204
/// <summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ Microsoft.JSInterop.JSRuntime.InvokeNewAsync(string! identifier, object?[]? args
6161
Microsoft.JSInterop.JSRuntime.InvokeNewAsync(string! identifier, System.Threading.CancellationToken cancellationToken, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
6262
Microsoft.JSInterop.JSRuntime.SetValueAsync<TValue>(string! identifier, TValue value) -> System.Threading.Tasks.ValueTask
6363
Microsoft.JSInterop.JSRuntime.SetValueAsync<TValue>(string! identifier, TValue value, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
64+
static Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeNewAsync(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, params object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
65+
static Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeNewAsync(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, System.Threading.CancellationToken cancellationToken, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
66+
static Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeNewAsync(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, System.TimeSpan timeout, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
6467
static Microsoft.JSInterop.JSRuntimeExtensions.InvokeNewAsync(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, params object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
6568
static Microsoft.JSInterop.JSRuntimeExtensions.InvokeNewAsync(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, System.Threading.CancellationToken cancellationToken, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
6669
static Microsoft.JSInterop.JSRuntimeExtensions.InvokeNewAsync(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, System.TimeSpan timeout, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>

0 commit comments

Comments
 (0)