mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-04 23:30:20 +00:00
LibWeb: Implement static method ReadableStream.from(asyncIterable)
This commit is contained in:
parent
01a8b5ee54
commit
0ec0e92b10
Notes:
sideshowbarker
2024-07-17 01:46:43 +09:00
Author: https://github.com/kennethmyhra
Commit: 0ec0e92b10
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/108
Reviewed-by: https://github.com/shannonbooth ✅
5 changed files with 45 additions and 1 deletions
|
@ -0,0 +1,5 @@
|
||||||
|
Well
|
||||||
|
Hello
|
||||||
|
Friends
|
||||||
|
!
|
||||||
|
🦬
|
|
@ -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>
|
|
@ -68,6 +68,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_
|
||||||
return readable_stream;
|
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)
|
ReadableStream::ReadableStream(JS::Realm& realm)
|
||||||
: PlatformObject(realm)
|
: PlatformObject(realm)
|
||||||
{
|
{
|
||||||
|
|
|
@ -70,6 +70,8 @@ public:
|
||||||
|
|
||||||
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>> 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;
|
virtual ~ReadableStream() override;
|
||||||
|
|
||||||
bool locked() const;
|
bool locked() const;
|
||||||
|
|
|
@ -29,7 +29,7 @@ dictionary ReadableStreamGetReaderOptions {
|
||||||
interface ReadableStream {
|
interface ReadableStream {
|
||||||
constructor(optional object underlyingSource, optional QueuingStrategy strategy = {});
|
constructor(optional object underlyingSource, optional QueuingStrategy strategy = {});
|
||||||
|
|
||||||
[FIXME] static ReadableStream from(any asyncIterable);
|
static ReadableStream from(any asyncIterable);
|
||||||
|
|
||||||
readonly attribute boolean locked;
|
readonly attribute boolean locked;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue