-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathJSRuntimeExtensions.cs
More file actions
112 lines (97 loc) · 4.11 KB
/
JSRuntimeExtensions.cs
File metadata and controls
112 lines (97 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
namespace Bunit.JSInterop.Implementation;
internal static class JSRuntimeExtensions
{
internal static ValueTask<TValue> HandleInvokeAsync<TValue>(this BunitJSInterop jSInterop, string identifier, object?[]? args)
{
var invocationMethodName = GetInvokeAsyncMethodName<TValue>();
var invocation = new JSRuntimeInvocation(identifier, null, args, typeof(TValue), invocationMethodName);
return jSInterop.HandleInvocation<TValue>(invocation);
}
[SuppressMessage("Design", "CA1068:CancellationToken parameters must come last", Justification = "Matching Blazor's JSRuntime design.")]
internal static ValueTask<TValue> HandleInvokeAsync<TValue>(this BunitJSInterop jSInterop, string identifier, CancellationToken cancellationToken, object?[]? args)
{
var invocationMethodName = GetInvokeAsyncMethodName<TValue>();
var invocation = new JSRuntimeInvocation(identifier, cancellationToken, args, typeof(TValue), invocationMethodName);
return jSInterop.HandleInvocation<TValue>(invocation);
}
internal static TValue HandleInvoke<TValue>(this BunitJSInterop jSInterop, string identifier, params object?[]? args)
{
var invocationMethodName = GetInvokeMethodName<TValue>();
var invocation = new JSRuntimeInvocation(identifier, null, args, typeof(TValue), invocationMethodName);
var resultTask = jSInterop.HandleInvocation<TValue>(invocation).AsTask();
return resultTask.GetAwaiter().GetResult();
}
internal static TResult HandleInvokeUnmarshalled<TResult>(this BunitJSInterop jSInterop, string identifier)
{
var invocation = new JSRuntimeInvocation(
identifier,
null,
Array.Empty<object?>(),
typeof(TResult),
"InvokeUnmarshalled");
return jSInterop.HandleInvocation<TResult>(invocation)
.AsTask()
.GetAwaiter()
.GetResult();
}
internal static TResult HandleInvokeUnmarshalled<T0, TResult>(this BunitJSInterop jSInterop, string identifier, T0 arg0)
{
var invocation = new JSRuntimeInvocation(
identifier,
null,
new object?[] { arg0 },
typeof(TResult),
"InvokeUnmarshalled");
return jSInterop.HandleInvocation<TResult>(invocation)
.AsTask()
.GetAwaiter()
.GetResult();
}
internal static TResult HandleInvokeUnmarshalled<T0, T1, TResult>(this BunitJSInterop jSInterop, string identifier, T0 arg0, T1 arg1)
{
var invocation = new JSRuntimeInvocation(
identifier,
null,
new object?[] { arg0, arg1 },
typeof(TResult),
"InvokeUnmarshalled");
return jSInterop.HandleInvocation<TResult>(invocation)
.AsTask()
.GetAwaiter()
.GetResult();
}
internal static TResult HandleInvokeUnmarshalled<T0, T1, T2, TResult>(this BunitJSInterop jSInterop, string identifier, T0 arg0, T1 arg1, T2 arg2)
{
var invocation = new JSRuntimeInvocation(
identifier,
null,
new object?[] { arg0, arg1, arg2 },
typeof(TResult),
"InvokeUnmarshalled");
return jSInterop.HandleInvocation<TResult>(invocation)
.AsTask()
.GetAwaiter()
.GetResult();
}
#if NET10_0_OR_GREATER
internal static ValueTask<IJSObjectReference> HandleInvokeConstructorAsync(this BunitJSInterop jSInterop, string identifier, object?[]? args)
{
var invocation = new JSRuntimeInvocation(identifier, null, args, typeof(IJSObjectReference), "InvokeConstructorAsync");
return jSInterop.HandleInvocation<IJSObjectReference>(invocation);
}
[SuppressMessage("Design", "CA1068:CancellationToken parameters must come last", Justification = "Matching Blazor's JSRuntime design.")]
internal static ValueTask<IJSObjectReference> HandleInvokeConstructorAsync(this BunitJSInterop jSInterop, string identifier, CancellationToken cancellationToken, object?[]? args)
{
var invocation = new JSRuntimeInvocation(identifier, cancellationToken, args, typeof(IJSObjectReference), "InvokeConstructorAsync");
return jSInterop.HandleInvocation<IJSObjectReference>(invocation);
}
#endif
private static string GetInvokeAsyncMethodName<TValue>()
=> typeof(TValue) == typeof(Microsoft.JSInterop.Infrastructure.IJSVoidResult)
? "InvokeVoidAsync"
: "InvokeAsync";
private static string GetInvokeMethodName<TValue>()
=> typeof(TValue) == typeof(Microsoft.JSInterop.Infrastructure.IJSVoidResult)
? "InvokeVoid"
: "Invoke";
}