Skip to content

Commit 020f927

Browse files
Updated demos to conform to best practices.
1 parent f420ccc commit 020f927

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/FileReaderSample.razor

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@page "/FileReaderSample"
22
@using System.Text
3+
@implements IAsyncDisposable
34

45
@inject IJSRuntime JSRuntime
56
@inject HttpClient HttpClient
@@ -127,4 +128,12 @@ We then use the methods of the <code>FileReader</code> interface to read the <co
127128
};
128129
await fileReader.ReadAsDataURLAsync(blob!);
129130
}
131+
132+
public async ValueTask DisposeAsync()
133+
{
134+
if (blob is not null)
135+
{
136+
await blob.DisposeAsync();
137+
}
138+
}
130139
}

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/Index.razor

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/"
2-
@implements IDisposable
2+
@implements IAsyncDisposable
33

44
@inject IJSRuntime JSRuntime
55
@inject HttpClient HttpClient
@@ -38,8 +38,15 @@ content type: @file?.Type
3838
blobURL = URL.CreateObjectURL(file);
3939
}
4040

41-
public void Dispose()
41+
public async ValueTask DisposeAsync()
4242
{
43-
URL.RevokeObjectURL(blobURL);
43+
if (file is not null)
44+
{
45+
await file.DisposeAsync();
46+
}
47+
if (blobURL is not "")
48+
{
49+
URL.RevokeObjectURL(blobURL);
50+
}
4451
}
4552
}

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/Slice.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Here we can see how we can slice a <code>Blob</code>. We construct a <code>Blob<
2828

2929
protected override async Task OnInitializedAsync()
3030
{
31-
var blob = await BlobInProcess.CreateAsync(
31+
await using var blob = await BlobInProcess.CreateAsync(
3232
JSRuntime,
3333
blobParts: new BlobPart[] { Enumerable.Range(0, 50).Select(i => (byte)i).ToArray() },
3434
options: new() { Type = "text/plain" }

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/StreamLargeBlob.razor

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ This sample will illustrate stream reading a large Blob to .NET using the inbuil
3232

3333
var jSBlob = await JSRuntime.InvokeAsync<IJSObjectReference>("hugeBlob");
3434

35-
var blob = Blob.Create(JSRuntime, jSBlob);
35+
await using var blob = await Blob.CreateAsync(JSRuntime, jSBlob);
3636

37-
var stream = await blob.StreamAsync();
37+
await using var stream = await blob.StreamAsync();
3838

39-
var reader = await stream.GetDefaultReaderAsync();
39+
await using var reader = await stream.GetDefaultReaderAsync();
4040

4141
await foreach (var chunk in reader.IterateStringsAsync())
4242
{

0 commit comments

Comments
 (0)