Skip to content

Commit f92b946

Browse files
Updated ProgressEvent to extend Event.
1 parent d5623e0 commit f92b946

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
1-
using KristofferStrube.Blazor.WebIDL;
1+
using KristofferStrube.Blazor.DOM;
2+
using KristofferStrube.Blazor.WebIDL;
23
using Microsoft.JSInterop;
34

45
namespace KristofferStrube.Blazor.FileAPI;
56

67
/// <summary>
78
/// <see href="https://xhr.spec.whatwg.org/#progressevent">ProgressEvent browser specs</see>
89
/// </summary>
9-
public class ProgressEvent : BaseJSWrapper, IJSCreatable<ProgressEvent>
10+
public class ProgressEvent : Event, IJSCreatable<ProgressEvent>
1011
{
12+
/// <summary>
13+
/// A lazily loaded task that evaluates to a helper module instance from the Blazor.FileAPI library.
14+
/// </summary>
15+
protected readonly Lazy<Task<IJSObjectReference>> fileApiHelperTask;
16+
1117
/// <inheritdoc/>
12-
public static async Task<ProgressEvent> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference)
18+
public static new async Task<ProgressEvent> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference)
1319
{
1420
return await CreateAsync(jSRuntime, jSReference, new());
1521
}
1622

1723
/// <inheritdoc/>
18-
public static Task<ProgressEvent> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options)
24+
public static new Task<ProgressEvent> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options)
1925
{
2026
return Task.FromResult(new ProgressEvent(jSRuntime, jSReference, options));
2127
}
2228

2329
/// <inheritdoc cref="CreateAsync(IJSRuntime, IJSObjectReference, CreationOptions)"/>
24-
protected internal ProgressEvent(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options) { }
30+
protected internal ProgressEvent(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options)
31+
{
32+
fileApiHelperTask = new(jSRuntime.GetHelperAsync);
33+
}
2534

2635
/// <summary>
2736
/// Indicates whether the total can be calculated.
2837
/// </summary>
2938
/// <returns>A <see langword="bool"/> indicating if the total length was computable.</returns>
3039
public async Task<bool> GetLengthComputableAsync()
3140
{
32-
IJSObjectReference helper = await helperTask.Value;
41+
IJSObjectReference helper = await fileApiHelperTask.Value;
3342
return await helper.InvokeAsync<bool>("getAttribute", JSReference, "lengthComputable");
3443
}
3544

@@ -39,7 +48,7 @@ public async Task<bool> GetLengthComputableAsync()
3948
/// <returns>The length of the currently loaded part.</returns>
4049
public async Task<ulong> GetLoadedAsync()
4150
{
42-
IJSObjectReference helper = await helperTask.Value;
51+
IJSObjectReference helper = await fileApiHelperTask.Value;
4352
return await helper.InvokeAsync<ulong>("getAttribute", JSReference, "loaded");
4453
}
4554

@@ -49,7 +58,19 @@ public async Task<ulong> GetLoadedAsync()
4958
/// <returns>The total length of the read.</returns>
5059
public async Task<ulong> GetTotalAsync()
5160
{
52-
IJSObjectReference helper = await helperTask.Value;
61+
IJSObjectReference helper = await fileApiHelperTask.Value;
5362
return await helper.InvokeAsync<ulong>("getAttribute", JSReference, "total");
5463
}
64+
65+
/// <inheritdoc/>
66+
public new async ValueTask DisposeAsync()
67+
{
68+
if (fileApiHelperTask.IsValueCreated)
69+
{
70+
IJSObjectReference module = await helperTask.Value;
71+
await module.DisposeAsync();
72+
}
73+
await base.DisposeAsync();
74+
GC.SuppressFinalize(this);
75+
}
5576
}

0 commit comments

Comments
 (0)