Serenity Operating System
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
14namespace Web::Streams {
15
16// https://streams.spec.whatwg.org/#readablestream
17class ReadableStream final : public Bindings::PlatformObject {
18 WEB_PLATFORM_OBJECT(ReadableStream, Bindings::PlatformObject);
19
20public:
21 enum class State {
22 Readable,
23 Closed,
24 Errored,
25 };
26
27 static WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> construct_impl(JS::Realm&);
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
41private:
42 explicit ReadableStream(JS::Realm&);
43
44 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
45 virtual void visit_edges(Cell::Visitor&) override;
46
47 // https://streams.spec.whatwg.org/#readablestream-controller
48 // A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
49 JS::GCPtr<JS::Object> m_controller;
50
51 // https://streams.spec.whatwg.org/#readablestream-detached
52 // A boolean flag set to true when the stream is transferred
53 bool m_detached { false };
54
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
59 // https://streams.spec.whatwg.org/#readablestream-reader
60 // A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
61 JS::GCPtr<JS::Object> m_reader;
62
63 // https://streams.spec.whatwg.org/#readablestream-state
64 // A string containing the stream’s current state, used internally; one of "readable", "closed", or "errored"
65 State m_state { State::Readable };
66
67 // https://streams.spec.whatwg.org/#readablestream-storederror
68 // A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
69 JS::Value m_stored_error { JS::js_undefined() };
70};
71
72}