mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibWeb: Implement AbortSignal.any()
This method takes a list of AbortSignals and returns an AbortSignal that is aborted when any of the input signals is aborted.
This commit is contained in:
parent
9eaae99da7
commit
7625d8a155
Notes:
sideshowbarker
2024-07-18 00:41:35 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/7625d8a155 Pull-request: https://github.com/SerenityOS/serenity/pull/23544
5 changed files with 198 additions and 1 deletions
39
Tests/LibWeb/Text/expected/abortsignal-any.txt
Normal file
39
Tests/LibWeb/Text/expected/abortsignal-any.txt
Normal file
|
@ -0,0 +1,39 @@
|
|||
Signal 0 aborted: false
|
||||
Signal 1 aborted: false
|
||||
Signal 2 aborted: false
|
||||
Signal 3 aborted: false
|
||||
Signal 4 aborted: false
|
||||
Abort callback for signal: 0. Reason: Test AbortController 1
|
||||
Abort callback for signal: 1. Reason: Test AbortController 1
|
||||
Abort callback for signal: 3. Reason: Test AbortController 1
|
||||
Abort callback for signal: 4. Reason: Test AbortController 1
|
||||
Signal 0 aborted: true
|
||||
Signal 1 aborted: true
|
||||
Signal 2 aborted: false
|
||||
Signal 3 aborted: true
|
||||
Signal 4 aborted: true
|
||||
Signal 0 aborted: false
|
||||
Signal 1 aborted: false
|
||||
Signal 2 aborted: false
|
||||
Signal 3 aborted: false
|
||||
Signal 4 aborted: false
|
||||
Abort callback for signal: 1. Reason: Test AbortController 2
|
||||
Abort callback for signal: 2. Reason: Test AbortController 2
|
||||
Abort callback for signal: 3. Reason: Test AbortController 2
|
||||
Abort callback for signal: 4. Reason: Test AbortController 2
|
||||
Signal 0 aborted: false
|
||||
Signal 1 aborted: true
|
||||
Signal 2 aborted: true
|
||||
Signal 3 aborted: true
|
||||
Signal 4 aborted: true
|
||||
Signal 0 aborted: false
|
||||
Signal 1 aborted: false
|
||||
Signal 2 aborted: false
|
||||
Signal 3 aborted: false
|
||||
Signal 4 aborted: false
|
||||
Abort callback for signal: 4. Reason: Test AbortController 3
|
||||
Signal 0 aborted: false
|
||||
Signal 1 aborted: false
|
||||
Signal 2 aborted: false
|
||||
Signal 3 aborted: false
|
||||
Signal 4 aborted: true
|
52
Tests/LibWeb/Text/input/abortsignal-any.html
Normal file
52
Tests/LibWeb/Text/input/abortsignal-any.html
Normal file
|
@ -0,0 +1,52 @@
|
|||
<script src="include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
function make_callback(signal, signal_number) {
|
||||
return () => println(`Abort callback for signal: ${signal_number}. Reason: ${signal.reason}`);
|
||||
}
|
||||
|
||||
function make_signal_list() {
|
||||
const controller1 = new AbortController();
|
||||
const controller2 = new AbortController();
|
||||
const controller3 = new AbortController();
|
||||
|
||||
let signals = [];
|
||||
signals.push(AbortSignal.any([controller1.signal]));
|
||||
signals.push(AbortSignal.any([controller1.signal, controller2.signal]));
|
||||
signals.push(AbortSignal.any([controller2.signal]));
|
||||
signals.push(AbortSignal.any([controller2.signal, controller1.signal]));
|
||||
|
||||
const nestedSignal = AbortSignal.any([...signals, controller3.signal]);
|
||||
signals.push(nestedSignal);
|
||||
|
||||
for (let i = 0; i < signals.length; i++) {
|
||||
const signal = signals[i];
|
||||
signal.onabort = make_callback(signal, i);
|
||||
}
|
||||
|
||||
return { controllers: [controller1, controller2, controller3], signals: signals};
|
||||
}
|
||||
|
||||
function print_aborted_signals(signals) {
|
||||
for (let i = 0; i < signals.length; i++) {
|
||||
const signal = signals[i];
|
||||
println(`Signal ${i} aborted: ${signal.aborted}`);
|
||||
}
|
||||
}
|
||||
|
||||
const signal_list1 = make_signal_list();
|
||||
print_aborted_signals(signal_list1.signals);
|
||||
signal_list1.controllers[0].abort("Test AbortController 1");
|
||||
print_aborted_signals(signal_list1.signals);
|
||||
|
||||
const signal_list2 = make_signal_list();
|
||||
print_aborted_signals(signal_list2.signals);
|
||||
signal_list2.controllers[1].abort("Test AbortController 2");
|
||||
print_aborted_signals(signal_list2.signals);
|
||||
|
||||
const signal_list3 = make_signal_list();
|
||||
print_aborted_signals(signal_list3.signals);
|
||||
signal_list3.controllers[2].abort("Test AbortController 3");
|
||||
print_aborted_signals(signal_list3.signals);
|
||||
});
|
||||
</script>
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||
* Copyright (c) 2024, Tim Ledbetter <timledbetter@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -67,6 +68,10 @@ void AbortSignal::signal_abort(JS::Value reason)
|
|||
auto abort_event = Event::create(realm(), HTML::EventNames::abort);
|
||||
abort_event->set_is_trusted(true);
|
||||
dispatch_event(abort_event);
|
||||
|
||||
// 6. For each dependentSignal of signal’s dependent signals, signal abort on dependentSignal with signal’s abort reason.
|
||||
for (auto const& dependent_signal : m_dependent_signals)
|
||||
dependent_signal->signal_abort(reason);
|
||||
}
|
||||
|
||||
void AbortSignal::set_onabort(WebIDL::CallbackType* event_handler)
|
||||
|
@ -95,6 +100,12 @@ void AbortSignal::visit_edges(JS::Cell::Visitor& visitor)
|
|||
visitor.visit(m_abort_reason);
|
||||
for (auto& algorithm : m_abort_algorithms)
|
||||
visitor.visit(algorithm);
|
||||
|
||||
for (auto& source_signal : m_source_signals)
|
||||
visitor.visit(source_signal);
|
||||
|
||||
for (auto& dependent_signal : m_dependent_signals)
|
||||
visitor.visit(dependent_signal);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-follow
|
||||
|
@ -162,4 +173,74 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> AbortSignal::timeout(JS::VM&
|
|||
return signal;
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-abortsignal-any
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> AbortSignal::any(JS::VM& vm, JS::Value signals)
|
||||
{
|
||||
Vector<JS::Handle<AbortSignal>> signals_list;
|
||||
auto iterator_record = TRY(get_iterator(vm, signals, JS::IteratorHint::Sync));
|
||||
while (true) {
|
||||
auto next = TRY(iterator_step_value(vm, iterator_record));
|
||||
if (!next.has_value())
|
||||
break;
|
||||
|
||||
auto value = next.release_value();
|
||||
if (!value.is_object() || !is<AbortSignal>(value.as_object()))
|
||||
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "AbortSignal");
|
||||
|
||||
auto& signal = static_cast<AbortSignal&>(value.as_object());
|
||||
signals_list.append(JS::make_handle(signal));
|
||||
}
|
||||
|
||||
// The static any(signals) method steps are to return the result of creating a dependent abort signal from signals using AbortSignal and the current realm.
|
||||
return create_dependent_abort_signal(*vm.current_realm(), signals_list);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#create-a-dependent-abort-signal
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> AbortSignal::create_dependent_abort_signal(JS::Realm& realm, Vector<JS::Handle<AbortSignal>> const& signals)
|
||||
{
|
||||
// 1. Let resultSignal be a new object implementing signalInterface using realm.
|
||||
auto result_signal = TRY(construct_impl(realm));
|
||||
|
||||
// 2. For each signal of signals: if signal is aborted, then set resultSignal’s abort reason to signal’s abort reason and return resultSignal.
|
||||
for (auto const& signal : signals) {
|
||||
if (signal->aborted()) {
|
||||
result_signal->set_reason(signal->reason());
|
||||
return result_signal;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Set resultSignal’s dependent to true.
|
||||
result_signal->set_dependent(true);
|
||||
|
||||
// 4. For each signal of signals:
|
||||
for (auto const& signal : signals) {
|
||||
// 1. If signal’s dependent is false, then:
|
||||
if (!signal->dependent()) {
|
||||
// 1. Append signal to resultSignal’s source signals.
|
||||
result_signal->append_source_signal({ signal });
|
||||
|
||||
// 2. Append resultSignal to signal’s dependent signals.
|
||||
signal->append_dependent_signal(result_signal);
|
||||
}
|
||||
// 2. Otherwise, for each sourceSignal of signal’s source signals:
|
||||
else {
|
||||
for (auto const& source_signal : signal->source_signals()) {
|
||||
// 1. Assert: sourceSignal is not aborted and not dependent.
|
||||
VERIFY(source_signal);
|
||||
VERIFY(!source_signal->aborted());
|
||||
VERIFY(!source_signal->dependent());
|
||||
|
||||
// 2. Append sourceSignal to resultSignal’s source signals.
|
||||
result_signal->append_source_signal(source_signal);
|
||||
|
||||
// 3. Append resultSignal to sourceSignal’s dependent signals.
|
||||
source_signal->append_dependent_signal(result_signal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Return resultSignal
|
||||
return result_signal;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||
* Copyright (c) 2024, Tim Ledbetter <timledbetter@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -46,6 +47,7 @@ public:
|
|||
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> abort(JS::VM&, JS::Value reason);
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> timeout(JS::VM&, Web::WebIDL::UnsignedLongLong milliseconds);
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> any(JS::VM&, JS::Value signals);
|
||||
|
||||
private:
|
||||
explicit AbortSignal(JS::Realm&);
|
||||
|
@ -53,6 +55,16 @@ private:
|
|||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(JS::Cell::Visitor&) override;
|
||||
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> create_dependent_abort_signal(JS::Realm&, Vector<JS::Handle<AbortSignal>> const&);
|
||||
|
||||
bool dependent() const { return m_dependent; }
|
||||
void set_dependent(bool dependent) { m_dependent = dependent; }
|
||||
|
||||
Vector<JS::GCPtr<AbortSignal>> source_signals() const { return m_source_signals; }
|
||||
|
||||
void append_source_signal(JS::GCPtr<AbortSignal> source_signal) { m_source_signals.append(source_signal); }
|
||||
void append_dependent_signal(JS::GCPtr<AbortSignal> dependent_signal) { m_dependent_signals.append(dependent_signal); }
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-abort-reason
|
||||
// An AbortSignal object has an associated abort reason, which is a JavaScript value. It is undefined unless specified otherwise.
|
||||
JS::Value m_abort_reason { JS::js_undefined() };
|
||||
|
@ -60,6 +72,18 @@ private:
|
|||
// https://dom.spec.whatwg.org/#abortsignal-abort-algorithms
|
||||
// FIXME: This should be a set.
|
||||
Vector<JS::NonnullGCPtr<JS::HeapFunction<void()>>> m_abort_algorithms;
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-source-signals
|
||||
// An AbortSignal object has associated source signals (a weak set of AbortSignal objects that the object is dependent on for its aborted state), which is initially empty.
|
||||
Vector<JS::GCPtr<AbortSignal>> m_source_signals;
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-dependent-signals
|
||||
// An AbortSignal object has associated dependent signals (a weak set of AbortSignal objects that are dependent on the object for their aborted state), which is initially empty.
|
||||
Vector<JS::GCPtr<AbortSignal>> m_dependent_signals;
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-dependent
|
||||
// An AbortSignal object has a dependent (a boolean), which is initially false.
|
||||
bool m_dependent { false };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
interface AbortSignal : EventTarget {
|
||||
[NewObject] static AbortSignal abort(optional any reason);
|
||||
[Exposed=(Window,Worker), NewObject] static AbortSignal timeout([EnforceRange] unsigned long long milliseconds);
|
||||
// FIXME: [NewObject] static AbortSignal _any(sequence<AbortSignal> signals);
|
||||
// FIXME: Argument should be of type: sequence<AbortSignal>.
|
||||
[NewObject] static AbortSignal _any(any signals);
|
||||
|
||||
readonly attribute boolean aborted;
|
||||
readonly attribute any reason;
|
||||
|
|
Loading…
Reference in a new issue