Skip to content

Commit 1551ddf

Browse files
Updated README to conform to best practices and updated related repositories.
1 parent 020f927 commit 1551ddf

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ You also need to reference the package in order to use it in your pages. This ca
4141
Most of this library is wrapper classes which can be instantiated from your code using the static `Create` and `CreateAsync` methods on the wrapper classes.
4242
An example could be to create an instance of a `Blob` that contains the text `"Hello World!"` and gets its `Size` and `Type`, read it as a `ReadableStream`, read as text directly, and slice it into a new `Blob` like this.
4343
```csharp
44-
Blob blob = await Blob.CreateAsync(
44+
await using Blob blob = await Blob.CreateAsync(
4545
JSRuntime,
4646
blobParts: new BlobPart[] {
4747
"Hello ",
@@ -51,13 +51,13 @@ Blob blob = await Blob.CreateAsync(
5151
);
5252
ulong size = await blob.GetSizeAsync(); // 12
5353
string type = await blob.GetTypeAsync(); // "text/plain"
54-
ReadableStream stream = await blob.StreamAsync();
54+
await using ReadableStream stream = await blob.StreamAsync();
5555
string text = await blob.TextAsync(); // "Hello World!"
56-
Blob worldBlob = await blob.SliceAsync(6, 11); // Blob containing "World"
56+
await using Blob worldBlob = await blob.SliceAsync(6, 11); // Blob containing "World"
5757
```
5858
All creator methods take an `IJSRuntime` instance as the first parameter. The above sample will work in both Blazor Server and Blazor WebAssembly. If we only want to work with Blazor WebAssembly we can use the `InProcess` variant of the wrapper class. This is equivalent to the relationship between `IJSRuntime` and `IJSInProcessRuntime`. We can recreate the above sample using the `BlobInProcess` which will simplify some of the methods we can call on the `Blob` and how we access attributes.
5959
```csharp
60-
BlobInProcess blob = await BlobInProcess.CreateAsync(
60+
await using BlobInProcess blob = await BlobInProcess.CreateAsync(
6161
JSRuntime,
6262
blobParts: new BlobPart[] {
6363
"Hello ",
@@ -67,21 +67,21 @@ BlobInProcess blob = await BlobInProcess.CreateAsync(
6767
);
6868
ulong size = blob.Size; // 12
6969
string type = blob.Type; // "text/plain"
70-
ReadableStreamInProcess stream = await blob.StreamAsync();
70+
await using ReadableStreamInProcess stream = await blob.StreamAsync();
7171
string text = await blob.TextAsync(); // "Hello World!"
72-
BlobInProcess worldBlob = blob.Slice(6, 11); // BlobInProcess containing "World"
72+
await using BlobInProcess worldBlob = blob.Slice(6, 11); // BlobInProcess containing "World"
7373
```
7474
Some of the methods wrap a `Promise` so even in the `InProcess` variant we need to await it like we see for `TextAsync` above.
7575

7676
If you have an `IJSObjectReference` or an `IJSInProcessObjectReference` for a type equivalent to one of the classes wrapped in this package then you can construct a wrapper for it using another set of overloads of the static `Create` and `CreateAsync` methods on the appropriate class. In the below example we create wrapper instances from existing JS references to a `File` object.
7777
```csharp
7878
// Blazor Server compatible.
7979
IJSObjectReference jSFile; // JS Reference from other package or your own JSInterop.
80-
File file = File.Create(JSRuntime, jSFile);
80+
await using File file = File.CreateAsync(JSRuntime, jSFile, new() { DisposesJSReference = true });
8181

8282
// InProcess only supported in Blazor WebAssembly.
8383
IJSInProcessObjectReference jSFileInProcess; // JS Reference from other package or your own JSInterop.
84-
FileInProcess fileInProcess = await File.CreateAsync(JSRuntime, jSFileInProcess);
84+
await using FileInProcess fileInProcess = await File.CreateAsync(JSRuntime, jSFileInProcess);
8585
```
8686

8787
## Add to service collection
@@ -127,11 +127,13 @@ You can likewise add the `InProcess` variant of the service (`IURLServiceInProce
127127
Feel free to open issues on the repository if you find any errors with the package or have wishes for features.
128128

129129
# Related repositories
130-
This project uses the *Blazor.Streams* package to return a rich `ReadableStream` from the `StreamAsync` method on a `Blob`.
131-
- https://github.com/KristofferStrube/Blazor.Streams
130+
The library uses the following other packages to support its features:
131+
- https://github.com/KristofferStrube/Blazor.Streams (To return a rich `ReadableStream` from the `StreamAsync` method on a `Blob`)
132+
- https://github.com/KristofferStrube/Blazor.DOM (To implement `FileReader` which extends `EventTarget` and to implement `ProgressEvent` which extends `Event`)
132133

133-
This project is used in the *Blazor.FileSystem* package to return a rich `File` object when getting the `File` from a `FileSystemFileHandle` and when writing a `Blob` to a `FileSystemWritableFileSystem`.
134-
- https://github.com/KristofferStrube/Blazor.FileSystemAccess
134+
The library is used in the following other packages to support their features:
135+
- https://github.com/KristofferStrube/Blazor.FileSystem (to return a rich `File` object when getting the `File` from a `FileSystemFileHandle` and when writing a `Blob` to a `FileSystemWritableFileSystem`)
136+
- https://github.com/KristofferStrube/Blazor.MediaStreamRecording (to get the data from a `BlobEvent` which is created when a chunk of a stream has been recorded)
135137

136138
# Related articles
137139
This repository was build with inspiration and help from the following series of articles:

0 commit comments

Comments
 (0)