2023-04-08 16:01:49 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
|
2023-11-18 19:30:57 -03:00
|
|
|
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
2023-04-08 16:01:49 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Forward.h>
|
|
|
|
|
#include <AK/Function.h>
|
|
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
|
|
|
|
#include <LibWeb/Forward.h>
|
|
|
|
|
#include <LibWeb/Streams/ReadableStreamGenericReader.h>
|
2024-07-08 11:39:52 -03:00
|
|
|
#include <LibWeb/WebIDL/Types.h>
|
2023-04-08 16:01:49 -03:00
|
|
|
|
|
|
|
|
namespace Web::Streams {
|
|
|
|
|
|
2024-07-08 11:39:52 -03:00
|
|
|
// https://streams.spec.whatwg.org/#dictdef-readablestreambyobreaderreadoptions
|
|
|
|
|
struct ReadableStreamBYOBReaderReadOptions {
|
|
|
|
|
WebIDL::UnsignedLongLong min = 1;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-08 16:01:49 -03:00
|
|
|
// https://streams.spec.whatwg.org/#read-into-request
|
2023-09-22 22:30:39 -03:00
|
|
|
class ReadIntoRequest : public JS::Cell {
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_CELL(ReadIntoRequest, JS::Cell);
|
2023-09-22 22:30:39 -03:00
|
|
|
|
2023-04-08 16:01:49 -03:00
|
|
|
public:
|
|
|
|
|
virtual ~ReadIntoRequest() = default;
|
|
|
|
|
|
|
|
|
|
// An algorithm taking a chunk, called when a chunk is available for reading
|
|
|
|
|
virtual void on_chunk(JS::Value chunk) = 0;
|
|
|
|
|
|
|
|
|
|
// An algorithm taking a chunk or undefined, called when no chunks are available because the stream is closed
|
|
|
|
|
virtual void on_close(JS::Value chunk_or_undefined) = 0;
|
|
|
|
|
|
|
|
|
|
// An algorithm taking a JavaScript value, called when no chunks are available because the stream is errored
|
|
|
|
|
virtual void on_error(JS::Value error) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://streams.spec.whatwg.org/#readablestreambyobreader
|
|
|
|
|
class ReadableStreamBYOBReader final
|
|
|
|
|
: public Bindings::PlatformObject
|
|
|
|
|
, public ReadableStreamGenericReaderMixin {
|
|
|
|
|
WEB_PLATFORM_OBJECT(ReadableStreamBYOBReader, Bindings::PlatformObject);
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(ReadableStreamBYOBReader);
|
2023-04-08 16:01:49 -03:00
|
|
|
|
|
|
|
|
public:
|
2024-11-14 12:01:23 -03:00
|
|
|
static WebIDL::ExceptionOr<GC::Ref<ReadableStreamBYOBReader>> construct_impl(JS::Realm&, GC::Ref<ReadableStream>);
|
2023-08-26 21:55:25 -03:00
|
|
|
|
2023-04-08 16:01:49 -03:00
|
|
|
virtual ~ReadableStreamBYOBReader() override = default;
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<WebIDL::Promise> read(GC::Root<WebIDL::ArrayBufferView>&, ReadableStreamBYOBReaderReadOptions options = {});
|
2023-11-18 20:44:42 -03:00
|
|
|
|
2023-08-26 22:29:32 -03:00
|
|
|
void release_lock();
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
Vector<GC::Ref<ReadIntoRequest>>& read_into_requests() { return m_read_into_requests; }
|
2023-04-08 16:01:49 -03:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
explicit ReadableStreamBYOBReader(JS::Realm&);
|
|
|
|
|
|
2023-11-18 19:30:57 -03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
|
|
|
|
2023-04-08 16:01:49 -03:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
|
|
|
|
// https://streams.spec.whatwg.org/#readablestreambyobreader-readintorequests
|
|
|
|
|
// A list of read-into requests, used when a consumer requests chunks sooner than they are available
|
2024-11-14 12:01:23 -03:00
|
|
|
Vector<GC::Ref<ReadIntoRequest>> m_read_into_requests;
|
2023-04-08 16:01:49 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|