-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSExtensionsWithDisposible.cs
74 lines (61 loc) · 2.81 KB
/
JSExtensionsWithDisposible.cs
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
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop.Infrastructure;
using System.Collections.Concurrent;
namespace Microsoft.JSInterop
{
public static class JSExtensions
{
private static readonly ConcurrentDictionary<string, IJSObjectReference> _references = new();
private static async Task removeReference(KeyValuePair<string, IJSObjectReference> reference)
{
await reference.Value.DisposeAsync();
_references.TryRemove(reference.Key, out _);
}
public static async Task DisposeAsync(this IJSRuntime jsRuntime)
{
await Task.WhenAll(_references.Select(removeReference));
}
public static async ValueTask InvokeVoidAsync(this IJSRuntime jsRuntime, Type component, string identifier, params object?[]? args)
{
ArgumentNullException.ThrowIfNull(jsRuntime);
IJSObjectReference _jsRuntime = await jsRuntime.GetJsReference(component);
await _jsRuntime.InvokeAsync<IJSVoidResult>(identifier, args);
}
public static async ValueTask InvokeVoidAsync(this IJSRuntime jsRuntime, ComponentBase component, string identifier, params object?[]? args)
{
ArgumentNullException.ThrowIfNull(jsRuntime);
IJSObjectReference _jsRuntime = await jsRuntime.GetJsReference(component.GetType());
await _jsRuntime.InvokeAsync<IJSVoidResult>(identifier, args);
}
public static async ValueTask<TValue> InvokeAsync<TValue>(this IJSRuntime jsRuntime, Type component, string identifier, params object?[]? args)
{
ArgumentNullException.ThrowIfNull(jsRuntime);
IJSObjectReference _jsRuntime = await jsRuntime.GetJsReference(component);
TValue result = await _jsRuntime.InvokeAsync<TValue>(identifier, args);
return result;
}
public static async ValueTask<TValue> InvokeAsync<TValue>(this IJSRuntime jsRuntime, ComponentBase component, string identifier, params object?[]? args)
{
ArgumentNullException.ThrowIfNull(jsRuntime);
IJSObjectReference _jsRuntime = await jsRuntime.GetJsReference(component.GetType());
TValue result = await _jsRuntime.InvokeAsync<TValue>(identifier, args);
return result;
}
private async static Task<IJSObjectReference> GetJsReference(this IJSRuntime jsRuntime, Type compType)
{
ArgumentNullException.ThrowIfNull(compType);
IJSObjectReference result;
string fullName = compType.FullName ?? "";
if (_references.TryGetValue(fullName, out result))
return result;
var projectName = compType.Assembly.GetName().Name + ".";
var fileName = fullName.TrimStart(projectName.ToCharArray());
string jsFileName = $"./{fileName.Replace('.', '/')}.razor.js";
if (!File.Exists(jsFileName))
throw new Exception($"File {jsFileName} do not exists");
result = await jsRuntime.InvokeAsync<IJSObjectReference>("import", jsFileName);
_references.TryAdd(fullName, result);
return result;
}
}
}