ReadableStream.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  34. virtual void visit_edges(Cell::Visitor&) override;
  35. // https://streams.spec.whatwg.org/#readablestream-controller
  36. // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
  37. JS::GCPtr<JS::Object> m_controller;
  38. // https://streams.spec.whatwg.org/#readablestream-detached
  39. // A boolean flag set to true when the stream is transferred
  40. bool m_detached { false };
  41. // https://streams.spec.whatwg.org/#readablestream-disturbed
  42. // A boolean flag set to true when the stream has been read from or canceled
  43. bool m_disturbed { false };
  44. // https://streams.spec.whatwg.org/#readablestream-reader
  45. // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
  46. JS::GCPtr<JS::Object> m_reader;
  47. // https://streams.spec.whatwg.org/#readablestream-state
  48. // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
  49. State m_state { State::Readable };
  50. // https://streams.spec.whatwg.org/#readablestream-storederror
  51. // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
  52. JS::Value m_stored_error { JS::js_undefined() };
  53. };
  54. }