Skip to content

Commit

Permalink
LibWeb: Add static method ReadableStream.from(any asyncIterable)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethmyhra committed Jun 8, 2024
1 parent 3da05f4 commit 337fc95
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Well
Hello
Friends
!
🦬
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script src="../include.js"></script>
<script>
async function* asyncGenerator() {
yield "Well";
yield "Hello";
yield "Friends";
yield "!";
yield "🦬";
}

async function readStream(stream) {
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done)
break;
println(value);
}
}

test(async () => {
const asyncIterable = {
[Symbol.asyncIterator]: asyncGenerator,
};

const readableStream = ReadableStream.from(asyncIterable);

await readStream(readableStream);
});
</script>
7 changes: 7 additions & 0 deletions Userland/Libraries/LibWeb/Streams/ReadableStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_
return readable_stream;
}

// https://streams.spec.whatwg.org/#rs-from
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::from(JS::VM& vm, JS::Value async_iterable)
{
// 1. Return ? ReadableStreamFromIterable(asyncIterable).
return TRY(readable_stream_from_iterable(vm, async_iterable));
}

ReadableStream::ReadableStream(JS::Realm& realm)
: PlatformObject(realm)
{
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/Streams/ReadableStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class ReadableStream final : public Bindings::PlatformObject {

static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source, QueuingStrategy const& = {});

static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> from(JS::VM& vm, JS::Value async_iterable);

virtual ~ReadableStream() override;

bool locked() const;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Streams/ReadableStream.idl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dictionary ReadableStreamGetReaderOptions {
interface ReadableStream {
constructor(optional object underlyingSource, optional QueuingStrategy strategy = {});

[FIXME] static ReadableStream from(any asyncIterable);
static ReadableStream from(any asyncIterable);

readonly attribute boolean locked;

Expand Down

0 comments on commit 337fc95

Please sign in to comment.