2023-04-02 15:15:53 -07:00
/*
* Copyright ( c ) 2023 , Matthew Olsson < mattco @ serenityos . org >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
# include <AK/Forward.h>
# include <AK/SinglyLinkedList.h>
# include <LibJS/Forward.h>
# include <LibWeb/Bindings/PlatformObject.h>
# include <LibWeb/Forward.h>
2023-06-18 21:58:04 +12:00
# include <LibWeb/Streams/QueuingStrategy.h>
2023-04-02 15:15:53 -07:00
# include <LibWeb/WebIDL/Promise.h>
namespace Web : : Streams {
// https://streams.spec.whatwg.org/#pending-abort-request
struct PendingAbortRequest {
// https://streams.spec.whatwg.org/#pending-abort-request-promise
// A promise returned from WritableStreamAbort
2024-11-15 04:01:23 +13:00
GC : : Ref < WebIDL : : Promise > promise ;
2023-04-02 15:15:53 -07:00
// https://streams.spec.whatwg.org/#pending-abort-request-reason
// A JavaScript value that was passed as the abort reason to WritableStreamAbort
JS : : Value reason ;
// https://streams.spec.whatwg.org/#pending-abort-request-was-already-erroring
// A boolean indicating whether or not the stream was in the "erroring" state when WritableStreamAbort was called, which impacts the outcome of the abort request
bool was_already_erroring ;
} ;
// https://streams.spec.whatwg.org/#writablestream
class WritableStream final : public Bindings : : PlatformObject {
WEB_PLATFORM_OBJECT ( WritableStream , Bindings : : PlatformObject ) ;
2024-11-15 04:01:23 +13:00
GC_DECLARE_ALLOCATOR ( WritableStream ) ;
2023-04-02 15:15:53 -07:00
public :
enum class State {
Writable ,
Closed ,
Erroring ,
Errored ,
} ;
2024-11-15 04:01:23 +13:00
static WebIDL : : ExceptionOr < GC : : Ref < WritableStream > > construct_impl ( JS : : Realm & realm , Optional < GC : : Root < JS : : Object > > const & underlying_sink , QueuingStrategy const & = { } ) ;
2023-04-02 15:15:53 -07:00
virtual ~ WritableStream ( ) = default ;
bool locked ( ) const ;
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise > abort ( JS : : Value reason ) ;
GC : : Ptr < WebIDL : : Promise > close ( ) ;
WebIDL : : ExceptionOr < GC : : Ref < WritableStreamDefaultWriter > > get_writer ( ) ;
2023-04-02 15:15:53 -07:00
bool backpressure ( ) const { return m_backpressure ; }
void set_backpressure ( bool value ) { m_backpressure = value ; }
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise const > close_request ( ) const { return m_close_request ; }
GC : : Ptr < WebIDL : : Promise > close_request ( ) { return m_close_request ; }
void set_close_request ( GC : : Ptr < WebIDL : : Promise > value ) { m_close_request = value ; }
2023-04-02 15:15:53 -07:00
2024-11-15 04:01:23 +13:00
GC : : Ptr < WritableStreamDefaultController const > controller ( ) const { return m_controller ; }
GC : : Ptr < WritableStreamDefaultController > controller ( ) { return m_controller ; }
void set_controller ( GC : : Ptr < WritableStreamDefaultController > value ) { m_controller = value ; }
2023-04-02 15:15:53 -07:00
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise const > in_flight_write_request ( ) const { return m_in_flight_write_request ; }
void set_in_flight_write_request ( GC : : Ptr < WebIDL : : Promise > value ) { m_in_flight_write_request = value ; }
2023-04-02 15:15:53 -07:00
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise const > in_flight_close_request ( ) const { return m_in_flight_close_request ; }
void set_in_flight_close_request ( GC : : Ptr < WebIDL : : Promise > value ) { m_in_flight_close_request = value ; }
2023-04-02 15:15:53 -07:00
Optional < PendingAbortRequest > & pending_abort_request ( ) { return m_pending_abort_request ; }
void set_pending_abort_request ( Optional < PendingAbortRequest > & & value ) { m_pending_abort_request = move ( value ) ; }
State state ( ) const { return m_state ; }
void set_state ( State value ) { m_state = value ; }
JS : : Value stored_error ( ) const { return m_stored_error ; }
void set_stored_error ( JS : : Value value ) { m_stored_error = value ; }
2024-11-15 04:01:23 +13:00
GC : : Ptr < WritableStreamDefaultWriter const > writer ( ) const { return m_writer ; }
GC : : Ptr < WritableStreamDefaultWriter > writer ( ) { return m_writer ; }
void set_writer ( GC : : Ptr < WritableStreamDefaultWriter > value ) { m_writer = value ; }
2023-04-02 15:15:53 -07:00
2024-11-15 04:01:23 +13:00
SinglyLinkedList < GC : : Ref < WebIDL : : Promise > > & write_requests ( ) { return m_write_requests ; }
2023-04-02 15:15:53 -07:00
private :
explicit WritableStream ( JS : : Realm & ) ;
2023-08-07 08:41:28 +02:00
virtual void initialize ( JS : : Realm & ) override ;
2023-04-02 15:15:53 -07:00
virtual void visit_edges ( Cell : : Visitor & ) override ;
// https://streams.spec.whatwg.org/#writablestream-backpressure
// A boolean indicating the backpressure signal set by the controller
bool m_backpressure { false } ;
// https://streams.spec.whatwg.org/#writablestream-closerequest
// The promise returned from the writer’ s close() method
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise > m_close_request ;
2023-04-02 15:15:53 -07:00
// https://streams.spec.whatwg.org/#writablestream-controller
// A WritableStreamDefaultController created with the ability to control the state and queue of this stream
2024-11-15 04:01:23 +13:00
GC : : Ptr < WritableStreamDefaultController > m_controller ;
2023-04-02 15:15:53 -07:00
// https://streams.spec.whatwg.org/#writablestream-detached
// A boolean flag set to true when the stream is transferred
bool m_detached { false } ;
// https://streams.spec.whatwg.org/#writablestream-inflightwriterequest
// A slot set to the promise for the current in-flight write operation while the underlying sink's write algorithm is executing and has not yet fulfilled, used to prevent reentrant calls
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise > m_in_flight_write_request ;
2023-04-02 15:15:53 -07:00
// https://streams.spec.whatwg.org/#writablestream-inflightcloserequest
// A slot set to the promise for the current in-flight close operation while the underlying sink's close algorithm is executing and has not yet fulfilled, used to prevent the abort() method from interrupting close
2024-11-15 04:01:23 +13:00
GC : : Ptr < WebIDL : : Promise > m_in_flight_close_request ;
2023-04-02 15:15:53 -07:00
// https://streams.spec.whatwg.org/#writablestream-pendingabortrequest
// A pending abort request
Optional < PendingAbortRequest > m_pending_abort_request ;
// https://streams.spec.whatwg.org/#writablestream-state
// A string containing the stream’ s current state, used internally; one of "writable", "closed", "erroring", or "errored"
State m_state { State : : Writable } ;
// https://streams.spec.whatwg.org/#writablestream-storederror
// A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on the stream while in the "errored" state
JS : : Value m_stored_error { JS : : js_undefined ( ) } ;
// https://streams.spec.whatwg.org/#writablestream-writer
// A WritableStreamDefaultWriter instance, if the stream is locked to a writer, or undefined if it is not
2024-11-15 04:01:23 +13:00
GC : : Ptr < WritableStreamDefaultWriter > m_writer ;
2023-04-02 15:15:53 -07:00
// https://streams.spec.whatwg.org/#writablestream-writerequests
// A list of promises representing the stream’ s internal queue of write requests not yet processed by the underlying sink
2024-11-15 04:01:23 +13:00
SinglyLinkedList < GC : : Ref < WebIDL : : Promise > > m_write_requests ;
2023-04-02 15:15:53 -07:00
} ;
}