/
JSObjectReference.cs
74 lines (61 loc) · 2.29 KB
/
JSObjectReference.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using static Microsoft.AspNetCore.Internal.LinkerFlags;
namespace Microsoft.JSInterop.Implementation
{
/// <summary>
/// Implements functionality for <see cref="IJSObjectReference"/>.
/// </summary>
public class JSObjectReference : IJSObjectReference
{
private readonly JSRuntime _jsRuntime;
internal bool Disposed { get; set; }
/// <summary>
/// The unique identifier assigned to this instance.
/// </summary>
protected internal long Id { get; }
/// <summary>
/// Inititializes a new <see cref="JSObjectReference"/> instance.
/// </summary>
/// <param name="jsRuntime">The <see cref="JSRuntime"/> used for invoking JS interop calls.</param>
/// <param name="id">The unique identifier.</param>
protected internal JSObjectReference(JSRuntime jsRuntime, long id)
{
_jsRuntime = jsRuntime;
Id = id;
}
/// <inheritdoc />
public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(string identifier, object?[]? args)
{
ThrowIfDisposed();
return _jsRuntime.InvokeAsync<TValue>(Id, identifier, args);
}
/// <inheritdoc />
public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(JsonSerialized)] TValue>(string identifier, CancellationToken cancellationToken, object?[]? args)
{
ThrowIfDisposed();
return _jsRuntime.InvokeAsync<TValue>(Id, identifier, cancellationToken, args);
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (!Disposed)
{
Disposed = true;
await _jsRuntime.InvokeVoidAsync("DotNet.jsCallDispatcher.disposeJSObjectReferenceById", Id);
}
}
/// <inheritdoc />
protected void ThrowIfDisposed()
{
if (Disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
}
}