ReadableStream.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::Streams {
  12. // FIXME: Variant<DefaultReader, ByteStreamReader>
  13. // https://streams.spec.whatwg.org/#typedefdef-readablestreamreader
  14. using ReadableStreamReader = JS::GCPtr<ReadableStreamDefaultReader>;
  15. // https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
  16. using ReadableStreamController = Variant<JS::NonnullGCPtr<ReadableStreamDefaultController>, JS::NonnullGCPtr<ReadableByteStreamController>>;
  17. // https://streams.spec.whatwg.org/#readablestream
  18. class ReadableStream final : public Bindings::PlatformObject {
  19. WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
  20. public:
  21. enum class State {
  22. Readable,
  23. Closed,
  24. Errored,
  25. };
  26. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source);
  27. virtual ~ReadableStream() override;
  28. bool locked();
  29. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> cancel(JS::Value view);
  30. WebIDL::ExceptionOr<ReadableStreamReader> get_reader();
  31. Optional<ReadableStreamController>& controller() { return m_controller; }
  32. void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); }
  33. JS::Value stored_error() const { return m_stored_error; }
  34. void set_stored_error(JS::Value value) { m_stored_error = value; }
  35. ReadableStreamReader reader() const { return m_reader; }
  36. void set_reader(ReadableStreamReader value) { m_reader = value; }
  37. bool is_disturbed() const;
  38. void set_disturbed(bool value) { m_disturbed = value; }
  39. bool is_readable() const;
  40. bool is_closed() const;
  41. bool is_errored() const;
  42. bool is_locked() const;
  43. State state() const { return m_state; }
  44. void set_state(State value) { m_state = value; }
  45. private:
  46. explicit ReadableStream(JS::Realm&);
  47. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  48. virtual void visit_edges(Cell::Visitor&) override;
  49. // https://streams.spec.whatwg.org/#readablestream-controller
  50. // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
  51. Optional<ReadableStreamController> m_controller;
  52. // https://streams.spec.whatwg.org/#readablestream-detached
  53. // A boolean flag set to true when the stream is transferred
  54. bool m_detached { false };
  55. // https://streams.spec.whatwg.org/#readablestream-disturbed
  56. // A boolean flag set to true when the stream has been read from or canceled
  57. bool m_disturbed { false };
  58. // https://streams.spec.whatwg.org/#readablestream-reader
  59. // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
  60. ReadableStreamReader m_reader;
  61. // https://streams.spec.whatwg.org/#readablestream-state
  62. // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
  63. State m_state { State::Readable };
  64. // https://streams.spec.whatwg.org/#readablestream-storederror
  65. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
  66. JS::Value m_stored_error { JS::js_undefined() };
  67. };
  68. }