Skip to content

Commit e154631

Browse files
Changed FileReader to extend EventTarget.
1 parent 81bc19e commit e154631

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/KristofferStrube.Blazor.FileAPI/FileReader.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using KristofferStrube.Blazor.WebIDL;
1+
using KristofferStrube.Blazor.DOM;
2+
using KristofferStrube.Blazor.WebIDL;
23
using Microsoft.JSInterop;
34
using System.Text.Json.Serialization;
45

@@ -8,8 +9,13 @@ namespace KristofferStrube.Blazor.FileAPI;
89
/// <see href="https://www.w3.org/TR/FileAPI/#dfn-filereader">FileReader browser specs</see>
910
/// </summary>
1011
[IJSWrapperConverter]
11-
public class FileReader : BaseJSWrapper, IJSCreatable<FileReader>
12+
public class FileReader : EventTarget, IJSCreatable<FileReader>
1213
{
14+
/// <summary>
15+
/// A lazily loaded task that evaluates to a helper module instance from the Blazor.FileAPI library.
16+
/// </summary>
17+
protected readonly Lazy<Task<IJSObjectReference>> fileApiHelperTask;
18+
1319
/// <inheritdoc/>
1420
public static async Task<FileReader> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference)
1521
{
@@ -40,7 +46,10 @@ public static async Task<FileReader> CreateAsync(IJSRuntime jSRuntime)
4046
}
4147

4248
/// <inheritdoc cref="CreateAsync(IJSRuntime, IJSObjectReference, CreationOptions)"/>
43-
protected FileReader(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options) { }
49+
protected FileReader(IJSRuntime jSRuntime, IJSObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options)
50+
{
51+
fileApiHelperTask = new(jSRuntime.GetHelperAsync);
52+
}
4453

4554
/// <summary>
4655
/// Starts a new read for some <see cref="Blob"/> as an <see langword="byte"/>[] which can be read from <see cref="GetResultAsByteArrayAsync"/> once the load has ended which can be checked by setting the action <see cref="OnLoadEnd"/>.
@@ -114,7 +123,7 @@ public async Task AbortAsync(Blob blob)
114123
/// <returns>As a standard either <see cref="EMPTY"/>, <see cref="LOADING"/> or <see cref="DONE"/></returns>
115124
public async Task<ushort> GetReadyStateAsync()
116125
{
117-
IJSObjectReference helper = await helperTask.Value;
126+
IJSObjectReference helper = await fileApiHelperTask.Value;
118127
return await helper.InvokeAsync<ushort>("getAttribute", JSReference, "readyState");
119128
}
120129

@@ -124,7 +133,7 @@ public async Task<ushort> GetReadyStateAsync()
124133
/// <returns>Either the type of <see langword="string"/> or type of <see cref="byte"/>[].</returns>
125134
public async Task<Type?> GetResultTypeAsync()
126135
{
127-
IJSObjectReference helper = await helperTask.Value;
136+
IJSObjectReference helper = await fileApiHelperTask.Value;
128137
bool isArrayBuffer = await helper.InvokeAsync<bool>("isArrayBuffer", JSReference);
129138
return isArrayBuffer ? typeof(byte[]) : typeof(string);
130139
}
@@ -135,7 +144,7 @@ public async Task<ushort> GetReadyStateAsync()
135144
/// <returns>A <see langword="string"/> representing the read. If there was no result from the read or if the read has not ended yet then it will be <see langword="null"/>.</returns>
136145
public async Task<string?> GetResultAsStringAsync()
137146
{
138-
IJSObjectReference helper = await helperTask.Value;
147+
IJSObjectReference helper = await fileApiHelperTask.Value;
139148
return await helper.InvokeAsync<string?>("getAttribute", JSReference, "result");
140149
}
141150

@@ -145,7 +154,7 @@ public async Task<ushort> GetReadyStateAsync()
145154
/// <returns>A <see langword="byte"/>[] representing the read. If there was no result from the read or if the read has not ended yet then it will be <see langword="null"/>.</returns>
146155
public async Task<byte[]?> GetResultAsByteArrayAsync()
147156
{
148-
IJSObjectReference helper = await helperTask.Value;
157+
IJSObjectReference helper = await fileApiHelperTask.Value;
149158
IJSObjectReference jSResult = await helper.InvokeAsync<IJSObjectReference>("getAttribute", JSReference, "result");
150159
return await helper.InvokeAsync<byte[]?>("arrayBuffer", jSResult);
151160
}
@@ -156,7 +165,7 @@ public async Task<ushort> GetReadyStateAsync()
156165
/// <returns>A nullable IJSObjectReference because it was out of scope to wrap the Exception API.</returns>
157166
public async Task<IJSObjectReference?> GetErrorAsync()
158167
{
159-
IJSObjectReference helper = await helperTask.Value;
168+
IJSObjectReference helper = await fileApiHelperTask.Value;
160169
return await helper.InvokeAsync<IJSObjectReference?>("getAttribute", JSReference, "error");
161170
}
162171

0 commit comments

Comments
 (0)