Skip to content

Commit 67adfbd

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

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,39 @@ 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="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>
176+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
177+
/// <returns>An instance of <typeparamref name="TValue"/> obtained by JSON-deserializing the return value.</returns>
178+
public static ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSObjectReference jsObjectReference, string identifier, TimeSpan timeout)
179+
{
180+
ArgumentNullException.ThrowIfNull(jsObjectReference);
181+
182+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
183+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
184+
185+
return jsObjectReference.GetValueAsync<TValue>(identifier, cancellationToken);
186+
}
187+
188+
/// <summary>
189+
/// Updates the value of the specified JavaScript property asynchronously. If the property is not defined on the target object, it will be created.
190+
/// </summary>
191+
/// <typeparam name="TValue">JSON-serializable argument type.</typeparam>
192+
/// <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>
193+
/// <param name="value">JSON-serializable value.</param>
194+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
195+
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
196+
public static ValueTask SetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSObjectReference jsObjectReference, string identifier, TValue value, TimeSpan timeout)
197+
{
198+
ArgumentNullException.ThrowIfNull(jsObjectReference);
199+
200+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
201+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
202+
203+
return jsObjectReference.SetValueAsync<TValue>(identifier, value, cancellationToken);
204+
}
170205
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,39 @@ 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="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>
176+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
177+
/// <returns>An instance of <typeparamref name="TValue"/> obtained by JSON-deserializing the return value.</returns>
178+
public static ValueTask<TValue> GetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSRuntime jsRuntime, string identifier, TimeSpan timeout)
179+
{
180+
ArgumentNullException.ThrowIfNull(jsRuntime);
181+
182+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
183+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
184+
185+
return jsRuntime.GetValueAsync<TValue>(identifier, cancellationToken);
186+
}
187+
188+
/// <summary>
189+
/// Updates the value of the specified JavaScript property asynchronously. If the property is not defined on the target object, it will be created.
190+
/// </summary>
191+
/// <typeparam name="TValue">JSON-serializable argument type.</typeparam>
192+
/// <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>
193+
/// <param name="value">JSON-serializable value.</param>
194+
/// <param name="timeout">The duration after which to cancel the async operation. Overrides default timeouts (<see cref="JSRuntime.DefaultAsyncTimeout"/>).</param>
195+
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous invocation operation.</returns>
196+
public static ValueTask SetValueAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(this IJSRuntime jsRuntime, string identifier, TValue value, TimeSpan timeout)
197+
{
198+
ArgumentNullException.ThrowIfNull(jsRuntime);
199+
200+
using var cancellationTokenSource = timeout == Timeout.InfiniteTimeSpan ? null : new CancellationTokenSource(timeout);
201+
var cancellationToken = cancellationTokenSource?.Token ?? CancellationToken.None;
202+
203+
return jsRuntime.SetValueAsync<TValue>(identifier, value, cancellationToken);
204+
}
170205
}

0 commit comments

Comments
 (0)