ReadableStream.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // FIXME: Variant<DefaultController, ByteStreamController>
  16. // https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
  17. using ReadableStreamController = JS::GCPtr<ReadableStreamDefaultController>;
  18. // https://streams.spec.whatwg.org/#readablestream
  19. class ReadableStream final : public Bindings::PlatformObject {
  20. WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
  21. public:
  22. enum class State {
  23. Readable,
  24. Closed,
  25. Errored,
  26. };
  27. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&, Optional<JS::Handle<JS::Object>> const& underlying_source);
  28. virtual ~ReadableStream() override;
  29. bool locked();
  30. WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> cancel(JS::Value view);
  31. WebIDL::ExceptionOr<ReadableStreamReader> get_reader();
  32. ReadableStreamController controller() { return m_controller; }
  33. void set_controller(ReadableStreamController value) { m_controller = value; }
  34. JS::Value stored_error() const { return m_stored_error; }
  35. void set_stored_error(JS::Value value) { m_stored_error = value; }
  36. ReadableStreamReader reader() const { return m_reader; }
  37. void set_reader(ReadableStreamReader value) { m_reader = value; }
  38. bool is_disturbed() const;
  39. void set_disturbed(bool value) { m_disturbed = value; }
  40. bool is_readable() const;
  41. bool is_closed() const;
  42. bool is_errored() const;
  43. bool is_locked() const;
  44. void set_stream_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. 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. }