ReadableStreamBYOBReader.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/Function.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/Streams/ReadableStreamGenericReader.h>
  14. namespace Web::Streams {
  15. // https://streams.spec.whatwg.org/#read-into-request
  16. class ReadIntoRequest : public JS::Cell {
  17. JS_CELL(ReadIntoRequest, JS::Cell);
  18. public:
  19. virtual ~ReadIntoRequest() = default;
  20. // An algorithm taking a chunk, called when a chunk is available for reading
  21. virtual void on_chunk(JS::Value chunk) = 0;
  22. // An algorithm taking a chunk or undefined, called when no chunks are available because the stream is closed
  23. virtual void on_close(JS::Value chunk_or_undefined) = 0;
  24. // An algorithm taking a JavaScript value, called when no chunks are available because the stream is errored
  25. virtual void on_error(JS::Value error) = 0;
  26. };
  27. // https://streams.spec.whatwg.org/#readablestreambyobreader
  28. class ReadableStreamBYOBReader final
  29. : public Bindings::PlatformObject
  30. , public ReadableStreamGenericReaderMixin {
  31. WEB_PLATFORM_OBJECT(ReadableStreamBYOBReader, Bindings::PlatformObject);
  32. JS_DECLARE_ALLOCATOR(ReadableStreamBYOBReader);
  33. public:
  34. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamBYOBReader>> construct_impl(JS::Realm&, JS::NonnullGCPtr<ReadableStream>);
  35. virtual ~ReadableStreamBYOBReader() override = default;
  36. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> read(JS::Handle<WebIDL::ArrayBufferView>&);
  37. void release_lock();
  38. Vector<JS::NonnullGCPtr<ReadIntoRequest>>& read_into_requests() { return m_read_into_requests; }
  39. private:
  40. explicit ReadableStreamBYOBReader(JS::Realm&);
  41. virtual void initialize(JS::Realm&) override;
  42. virtual void visit_edges(Cell::Visitor&) override;
  43. // https://streams.spec.whatwg.org/#readablestreambyobreader-readintorequests
  44. // A list of read-into requests, used when a consumer requests chunks sooner than they are available
  45. Vector<JS::NonnullGCPtr<ReadIntoRequest>> m_read_into_requests;
  46. };
  47. }