Skip to content

Commit

Permalink
Merge pull request #12 from KristofferStrube/feature/added-readablest…
Browse files Browse the repository at this point in the history
…reambyobreaderreadoptions

Added `ReadableStreamBYOBReaderReadOptions` parameter to BYOB reader read method.
  • Loading branch information
KristofferStrube authored Mar 10, 2024
2 parents aeee6e2 + 046893b commit c1fb024
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ dictionary ReadableStreamReadResult {
interface ReadableStreamBYOBReader {
constructor(ReadableStream stream);
Promise<ReadableStreamReadResult> read(ArrayBufferView view);
Promise<ReadableStreamReadResult> read(ArrayBufferView view, optional ReadableStreamBYOBReaderReadOptions options = {});
undefined releaseLock();
};
ReadableStreamBYOBReader includes ReadableStreamGenericReader;
dictionary ReadableStreamBYOBReaderReadOptions {
[EnforceRange] unsigned long long min = 1;
};
[Exposed=*]
interface ReadableStreamDefaultController {
readonly attribute unrestricted double? desiredSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace KristofferStrube.Blazor.Streams;

/// <summary>
/// <see href="https://streams.spec.whatwg.org/#dictdef-readablestreambyobreaderreadoptions">Streams browser specs</see>
/// </summary>
public class ReadableStreamBYOBReaderReadOptions
{
/// <summary>
/// The minimal number of elements needed to be read for the <see cref="ReadableStreamBYOBReader.ReadAsync(WebIDL.IArrayBufferView, ReadableStreamBYOBReaderReadOptions?)"/> to finish.
/// </summary>
[JsonPropertyName("min")]
public ulong Min { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ internal ReadableStreamBYOBReaderInProcess(IJSRuntime jSRuntime, IJSInProcessObj
return new ReadableStreamReadResultInProcess(JSRuntime, inProcessHelper, jSInstance, new() { DisposesJSReference = true });
}

/// <summary>
/// Reads a chunk of a stream.
/// </summary>
/// <param name="view">The <see cref="IArrayBufferView"/> that is used as a buffer</param>
/// <returns>The next chunk of the underlying <see cref="ReadableStream"/>.</returns>
public new async Task<ReadableStreamReadResultInProcess> ReadAsync(IArrayBufferView view, ReadableStreamBYOBReaderReadOptions? options = null)
{
IJSInProcessObjectReference jSInstance = await inProcessHelper.InvokeAsync<IJSInProcessObjectReference>("read", view.JSReference, options);
return new ReadableStreamReadResultInProcess(JSRuntime, inProcessHelper, jSInstance, new() { DisposesJSReference = true });
}

/// <summary>
/// Sets the internal <c>reader</c> slot to <c>undefined</c>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ protected internal ReadableStreamBYOBReader(IJSRuntime jSRuntime, IJSObjectRefer
/// <returns>The next chunk of the underlying <see cref="ReadableStream"/>.</returns>
public async Task<ReadableStreamReadResult> ReadAsync(IArrayBufferView view)
{
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("read", view);
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("read", view.JSReference);
return new ReadableStreamReadResult(JSRuntime, jSInstance, new() { DisposesJSReference = true });
}

/// <summary>
/// Reads a chunk of a stream.
/// </summary>
/// <param name="view">The <see cref="IArrayBufferView"/> that acts as a buffer.</param>
/// <param name="options">The options for how the chunk is to be read.</param>
/// <returns>The next chunk of the underlying <see cref="ReadableStream"/>.</returns>
public async Task<ReadableStreamReadResult> ReadAsync(IArrayBufferView view, ReadableStreamBYOBReaderReadOptions? options = null)
{
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("read", view.JSReference, options);
return new ReadableStreamReadResult(JSRuntime, jSInstance, new() { DisposesJSReference = true });
}
}

0 comments on commit c1fb024

Please sign in to comment.