LibWeb: Add the ability for an AbortSignal to follow another

Following another abort signal basically means to make an abort signal
abort when another abort signal is aborted, unless the following signal
is already aborted.
This commit is contained in:
Luke Wilde 2022-10-26 18:15:46 +01:00 committed by Linus Groh
parent 07e3bb729d
commit dce6327ae7
Notes: sideshowbarker 2024-07-17 09:39:38 +09:00
2 changed files with 25 additions and 0 deletions

View file

@ -84,4 +84,27 @@ void AbortSignal::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_abort_reason);
}
// https://dom.spec.whatwg.org/#abortsignal-follow
void AbortSignal::follow(JS::NonnullGCPtr<AbortSignal> parent_signal)
{
// A followingSignal (an AbortSignal) is made to follow a parentSignal (an AbortSignal) by running these steps:
// 1. If followingSignal is aborted, then return.
if (aborted())
return;
// 2. If parentSignal is aborted, then signal abort on followingSignal with parentSignals abort reason.
if (parent_signal->aborted()) {
signal_abort(parent_signal->reason());
return;
}
// 3. Otherwise, add the following abort steps to parentSignal:
// NOTE: `this` and `parent_signal` are protected by AbortSignal using JS::SafeFunction.
parent_signal->add_abort_algorithm([this, parent_signal]() mutable {
// 1. Signal abort on followingSignal with parentSignals abort reason.
signal_abort(parent_signal->reason());
});
}
}

View file

@ -38,6 +38,8 @@ public:
JS::ThrowCompletionOr<void> throw_if_aborted() const;
void follow(JS::NonnullGCPtr<AbortSignal> parent_signal);
private:
explicit AbortSignal(JS::Realm&);