|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <AK/Forward.h> |
| 10 | +#include <LibJS/Forward.h> |
| 11 | +#include <LibWeb/Bindings/PlatformObject.h> |
| 12 | +#include <LibWeb/Forward.h> |
| 13 | + |
| 14 | +namespace Web::Streams { |
| 15 | + |
| 16 | +// https://streams.spec.whatwg.org/#readablestream |
| 17 | +class ReadableStream final : public Bindings::PlatformObject { |
| 18 | + WEB_PLATFORM_OBJECT(Request, Bindings::PlatformObject); |
| 19 | + |
| 20 | +public: |
| 21 | + enum class State { |
| 22 | + Readable, |
| 23 | + Closed, |
| 24 | + Errored, |
| 25 | + }; |
| 26 | + |
| 27 | + static DOM::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> create_with_global_object(HTML::Window&); |
| 28 | + |
| 29 | + virtual ~ReadableStream() override; |
| 30 | + |
| 31 | + JS::GCPtr<JS::Object> controller() const { return m_controller; } |
| 32 | + JS::GCPtr<JS::Object> reader() const { return m_reader; } |
| 33 | + JS::Value stored_error() const { return m_stored_error; } |
| 34 | + |
| 35 | + bool is_readable() const; |
| 36 | + bool is_closed() const; |
| 37 | + bool is_errored() const; |
| 38 | + bool is_locked() const; |
| 39 | + bool is_disturbed() const; |
| 40 | + |
| 41 | +private: |
| 42 | + explicit ReadableStream(HTML::Window&); |
| 43 | + |
| 44 | + virtual void visit_edges(Cell::Visitor&) override; |
| 45 | + |
| 46 | + // https://streams.spec.whatwg.org/#readablestream-controller |
| 47 | + // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream |
| 48 | + JS::GCPtr<JS::Object> m_controller; |
| 49 | + |
| 50 | + // https://streams.spec.whatwg.org/#readablestream-detached |
| 51 | + // A boolean flag set to true when the stream is transferred |
| 52 | + bool m_detached { false }; |
| 53 | + |
| 54 | + // https://streams.spec.whatwg.org/#readablestream-disturbed |
| 55 | + // A boolean flag set to true when the stream has been read from or canceled |
| 56 | + bool m_disturbed { false }; |
| 57 | + |
| 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 | + JS::GCPtr<JS::Object> m_reader; |
| 61 | + |
| 62 | + // https://streams.spec.whatwg.org/#readablestream-state |
| 63 | + // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored" |
| 64 | + State m_state { State::Readable }; |
| 65 | + |
| 66 | + // https://streams.spec.whatwg.org/#readablestream-storederror |
| 67 | + // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream |
| 68 | + JS::Value m_stored_error { JS::js_undefined() }; |
| 69 | +}; |
| 70 | + |
| 71 | +} |
0 commit comments