ReadableStreamBYOBReader.h 1.9 KB

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