Skip to content

Commit 61c6f77

Browse files
committed
Add missing extension methods, fix cancellation token passing
1 parent 264cafc commit 61c6f77

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/JSInterop/Microsoft.JSInterop/src/Implementation/JSObjectReference.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, C
7878
{
7979
ThrowIfDisposed();
8080

81-
return _jsRuntime.InvokeAsync<TValue>(Id, identifier, JSCallType.GetValue, null);
81+
return _jsRuntime.InvokeAsync<TValue>(Id, identifier, JSCallType.GetValue, cancellationToken, null);
8282
}
8383

8484
/// <inheritdoc />
@@ -94,7 +94,7 @@ public ValueTask<IJSObjectReference> InvokeConstructorAsync(string identifier, C
9494
{
9595
ThrowIfDisposed();
9696

97-
await _jsRuntime.InvokeAsync<TValue>(Id, identifier, JSCallType.SetValue, [value]);
97+
await _jsRuntime.InvokeAsync<TValue>(Id, identifier, JSCallType.SetValue, cancellationToken, [value]);
9898
}
9999

100100
/// <inheritdoc />

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,41 @@ public static ValueTask<IJSObjectReference> InvokeConstructorAsync(this IJSObjec
167167

168168
return jsObjectReference.InvokeConstructorAsync(identifier, cancellationToken, args);
169169
}
170+
171+
/// <summary>
172+
/// Reads the value of the specified JavaScript property asynchronously.
173+
/// </summary>
174+
/// <typeparam name="TValue">The JSON-serializable return type.</typeparam>
175+
/// <param name="jsObjectReference">The <see cref="IJSObjectReference"/>.</param>
176+
/// <param name="identifier">An identifier for the property to read. For example, the value <c>"someScope.someProp"</c> will read the value of the property <c>someScope.someProp</c>.</param>
177+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
178+
/// <returns>An instance of <typeparamref name="TValue"/> obtained by JSON-deserializing the return value.</returns>
179+
public static ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSObjectReference jsObjectReference, string identifier, TimeSpan timeout)
180+
{
181+
ArgumentNullException.ThrowIfNull(jsObjectReference);
182+
183+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
184+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
185+
186+
return jsObjectReference.GetValueAsync<TValue>(identifier, cancellationToken);
187+
}
188+
189+
/// <summary>
190+
/// Updates the value of the specified JavaScript property asynchronously. If the property is not defined on the target object, it will be created.
191+
/// </summary>
192+
/// <typeparam name="TValue">JSON-serializable argument type.</typeparam>
193+
/// <param name="jsObjectReference">The <see cref="IJSObjectReference"/>.</param>
194+
/// <param name="identifier">An identifier for the property to set. For example, the value <c>"someScope.someProp"</c> will update the property <c>someScope.someProp</c>.</param>
195+
/// <param name="value">JSON-serializable value.</param>
196+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
197+
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
198+
public static ValueTask SetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSObjectReference jsObjectReference, string identifier, TValue value, TimeSpan timeout)
199+
{
200+
ArgumentNullException.ThrowIfNull(jsObjectReference);
201+
202+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
203+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
204+
205+
return jsObjectReference.SetValueAsync<TValue>(identifier, value, cancellationToken);
206+
}
170207
}

src/JSInterop/Microsoft.JSInterop/src/JSRuntimeExtensions.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,41 @@ public static ValueTask<IJSObjectReference> InvokeConstructorAsync(this IJSRunti
167167

168168
return jsRuntime.InvokeConstructorAsync(identifier, cancellationToken, args);
169169
}
170+
171+
/// <summary>
172+
/// Reads the value of the specified JavaScript property asynchronously.
173+
/// </summary>
174+
/// <typeparam name="TValue">The JSON-serializable return type.</typeparam>
175+
/// <param name="jsRuntime">The <see cref="IJSRuntime"/>.</param>
176+
/// <param name="identifier">An identifier for the property to read. For example, the value <c>"someScope.someProp"</c> will read the value of the property <c>window.someScope.someProp</c>.</param>
177+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
178+
/// <returns>An instance of <typeparamref name="TValue"/> obtained by JSON-deserializing the return value.</returns>
179+
public static ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSRuntime jsRuntime, string identifier, TimeSpan timeout)
180+
{
181+
ArgumentNullException.ThrowIfNull(jsRuntime);
182+
183+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
184+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
185+
186+
return jsRuntime.GetValueAsync<TValue>(identifier, cancellationToken);
187+
}
188+
189+
/// <summary>
190+
/// Updates the value of the specified JavaScript property asynchronously. If the property is not defined on the target object, it will be created.
191+
/// </summary>
192+
/// <typeparam name="TValue">JSON-serializable argument type.</typeparam>
193+
/// <param name="jsRuntime">The <see cref="IJSRuntime"/>.</param>
194+
/// <param name="identifier">An identifier for the property to set. For example, the value <c>"someScope.someProp"</c> will update the property <c>window.someScope.someProp</c>.</param>
195+
/// <param name="value">JSON-serializable value.</param>
196+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
197+
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
198+
public static ValueTask SetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSRuntime jsRuntime, string identifier, TValue value, TimeSpan timeout)
199+
{
200+
ArgumentNullException.ThrowIfNull(jsRuntime);
201+
202+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
203+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
204+
205+
return jsRuntime.SetValueAsync<TValue>(identifier, value, cancellationToken);
206+
}
170207
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ Microsoft.JSInterop.JSRuntime.SetValueAsync<TValue>(string! identifier, TValue v
5757
static Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeConstructorAsync(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, params object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
5858
static Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeConstructorAsync(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, System.Threading.CancellationToken cancellationToken, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
5959
static Microsoft.JSInterop.JSObjectReferenceExtensions.InvokeConstructorAsync(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, System.TimeSpan timeout, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
60+
static Microsoft.JSInterop.JSObjectReferenceExtensions.GetValueAsync<TValue>(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, System.TimeSpan timeout) -> System.Threading.Tasks.ValueTask<TValue>
61+
static Microsoft.JSInterop.JSObjectReferenceExtensions.SetValueAsync<TValue>(this Microsoft.JSInterop.IJSObjectReference! jsObjectReference, string! identifier, TValue value, System.TimeSpan timeout) -> System.Threading.Tasks.ValueTask
6062
static Microsoft.JSInterop.JSRuntimeExtensions.InvokeConstructorAsync(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, params object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
6163
static Microsoft.JSInterop.JSRuntimeExtensions.InvokeConstructorAsync(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, System.Threading.CancellationToken cancellationToken, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
6264
static Microsoft.JSInterop.JSRuntimeExtensions.InvokeConstructorAsync(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, System.TimeSpan timeout, object?[]? args) -> System.Threading.Tasks.ValueTask<Microsoft.JSInterop.IJSObjectReference!>
65+
static Microsoft.JSInterop.JSRuntimeExtensions.GetValueAsync<TValue>(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, System.TimeSpan timeout) -> System.Threading.Tasks.ValueTask<TValue>
66+
static Microsoft.JSInterop.JSRuntimeExtensions.SetValueAsync<TValue>(this Microsoft.JSInterop.IJSRuntime! jsRuntime, string! identifier, TValue value, System.TimeSpan timeout) -> System.Threading.Tasks.ValueTask
6367
virtual Microsoft.JSInterop.JSInProcessRuntime.InvokeJS(in Microsoft.JSInterop.Infrastructure.JSInvocationInfo invocationInfo) -> string?
6468
virtual Microsoft.JSInterop.JSRuntime.BeginInvokeJS(in Microsoft.JSInterop.Infrastructure.JSInvocationInfo invocationInfo) -> void

0 commit comments

Comments
 (0)