1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:37:35 +00:00

LibWeb: Add Streams::ReadableStreamDefaultReader read-loop reader

This algorithm is used by ReadableStreamDefaultReader to read all bytes
from a given stream. Currently the algorithm used is somewhat naive as
it is recursive, but given the initial use of this reader, it should not
be a problem.
This commit is contained in:
Shannon Booth 2023-06-17 15:24:33 +12:00 committed by Andreas Kling
parent f320406a4c
commit 46f9a49bd8
2 changed files with 85 additions and 0 deletions

View file

@ -29,6 +29,31 @@ public:
virtual void on_error(JS::Value error) = 0;
};
class ReadLoopReadRequest : public ReadRequest {
public:
// successSteps, which is an algorithm accepting a byte sequence
using SuccessSteps = JS::SafeFunction<void(ByteBuffer)>;
// failureSteps, which is an algorithm accepting a JavaScript value
using FailureSteps = JS::SafeFunction<void(JS::Value error)>;
ReadLoopReadRequest(JS::VM& vm, JS::Realm& realm, ReadableStreamDefaultReader& reader, SuccessSteps success_steps, FailureSteps failure_steps);
virtual void on_chunk(JS::Value chunk) override;
virtual void on_close() override;
virtual void on_error(JS::Value error) override;
private:
JS::VM& m_vm;
JS::Realm& m_realm;
ReadableStreamDefaultReader& m_reader;
ByteBuffer m_bytes;
SuccessSteps m_success_steps;
FailureSteps m_failure_steps;
};
// https://streams.spec.whatwg.org/#readablestreamdefaultreader
class ReadableStreamDefaultReader final
: public Bindings::PlatformObject