ReadableStream.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // https://streams.spec.whatwg.org/#readablestream
  13. class ReadableStream final : public Bindings::PlatformObject {
  14. WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
  15. public:
  16. enum class State {
  17. Readable,
  18. Closed,
  19. Errored,
  20. };
  21. static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&);
  22. virtual ~ReadableStream() override;
  23. JS::GCPtr<JS::Object> controller() const { return m_controller; }
  24. JS::GCPtr<JS::Object> reader() const { return m_reader; }
  25. JS::Value stored_error() const { return m_stored_error; }
  26. bool is_readable() const;
  27. bool is_closed() const;
  28. bool is_errored() const;
  29. bool is_locked() const;
  30. bool is_disturbed() const;
  31. private:
  32. explicit ReadableStream(JS::Realm&);
  33. virtual void visit_edges(Cell::Visitor&) override;
  34. // https://streams.spec.whatwg.org/#readablestream-controller
  35. // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
  36. JS::GCPtr<JS::Object> m_controller;
  37. // https://streams.spec.whatwg.org/#readablestream-detached
  38. // A boolean flag set to true when the stream is transferred
  39. bool m_detached { false };
  40. // https://streams.spec.whatwg.org/#readablestream-disturbed
  41. // A boolean flag set to true when the stream has been read from or canceled
  42. bool m_disturbed { false };
  43. // https://streams.spec.whatwg.org/#readablestream-reader
  44. // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
  45. JS::GCPtr<JS::Object> m_reader;
  46. // https://streams.spec.whatwg.org/#readablestream-state
  47. // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
  48. State m_state { State::Readable };
  49. // https://streams.spec.whatwg.org/#readablestream-storederror
  50. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
  51. JS::Value m_stored_error { JS::js_undefined() };
  52. };
  53. }