Selaa lähdekoodia

LibWeb: Add AO transform_stream_default_controller_terminate()

Kenneth Myhra 2 vuotta sitten
vanhempi
commit
e1740938fc

+ 24 - 0
Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp

@@ -2799,6 +2799,30 @@ WebIDL::ExceptionOr<void> transform_stream_default_controller_error(TransformStr
     return {};
 }
 
+// https://streams.spec.whatwg.org/#transform-stream-default-controller-terminate
+WebIDL::ExceptionOr<void> transform_stream_default_controller_terminate(TransformStreamDefaultController& controller)
+{
+    auto& realm = controller.realm();
+
+    // 1. Let stream be controller.[[stream]].
+    auto stream = controller.stream();
+
+    // 2. Let readableController be stream.[[readable]].[[controller]].
+    VERIFY(stream->readable()->controller().has_value() && stream->readable()->controller()->has<JS::NonnullGCPtr<ReadableStreamDefaultController>>());
+    auto readable_controller = stream->readable()->controller()->get<JS::NonnullGCPtr<ReadableStreamDefaultController>>();
+
+    // 3. Perform ! ReadableStreamDefaultControllerClose(readableController).
+    readable_stream_default_controller_close(readable_controller);
+
+    // 4. Let error be a TypeError exception indicating that the stream has been terminated.
+    auto error = MUST_OR_THROW_OOM(JS::TypeError::create(realm, "Stream has been terminated."sv));
+
+    // 5. Perform ! TransformStreamErrorWritableAndUnblockWrite(stream, error).
+    TRY(transform_stream_error_writable_and_unblock_write(*stream, error));
+
+    return {};
+}
+
 // https://streams.spec.whatwg.org/#transform-stream-error
 WebIDL::ExceptionOr<void> transform_stream_error(TransformStream& stream, JS::Value error)
 {

+ 1 - 0
Userland/Libraries/LibWeb/Streams/AbstractOperations.h

@@ -136,6 +136,7 @@ WebIDL::ExceptionOr<void> writable_stream_default_controller_write(WritableStrea
 void transform_stream_default_controller_clear_algorithms(TransformStreamDefaultController&);
 WebIDL::ExceptionOr<void> transform_stream_default_controller_enqueue(TransformStreamDefaultController&, JS::Value chunk);
 WebIDL::ExceptionOr<void> transform_stream_default_controller_error(TransformStreamDefaultController&, JS::Value error);
+WebIDL::ExceptionOr<void> transform_stream_default_controller_terminate(TransformStreamDefaultController&);
 WebIDL::ExceptionOr<void> transform_stream_error(TransformStream&, JS::Value error);
 WebIDL::ExceptionOr<void> transform_stream_error_writable_and_unblock_write(TransformStream&, JS::Value error);
 WebIDL::ExceptionOr<void> transform_stream_set_backpressure(TransformStream&, bool backpressure);