LibWeb: Support removing callbacks from AbortSignal

This will be needed by Streams. To support this, we now store callbacks
in a hash table, keyed by an ID. Callers may use that ID to remove the
callback at a later point.
This commit is contained in:
Timothy Flynn 2025-04-10 09:04:01 -04:00 committed by Tim Flynn
commit 4010c4643a
Notes: github-actions[bot] 2025-04-11 16:11:54 +00:00
5 changed files with 22 additions and 11 deletions

View file

@ -35,14 +35,22 @@ void AbortSignal::initialize(JS::Realm& realm)
}
// https://dom.spec.whatwg.org/#abortsignal-add
void AbortSignal::add_abort_algorithm(Function<void()> abort_algorithm)
Optional<AbortSignal::AbortAlgorithmID> AbortSignal::add_abort_algorithm(Function<void()> abort_algorithm)
{
// 1. If signal is aborted, then return.
if (aborted())
return;
return {};
// 2. Append algorithm to signals abort algorithms.
m_abort_algorithms.append(GC::create_function(vm().heap(), move(abort_algorithm)));
m_abort_algorithms.set(++m_next_abort_algorithm_id, GC::create_function(vm().heap(), move(abort_algorithm)));
return m_next_abort_algorithm_id;
}
// https://dom.spec.whatwg.org/#abortsignal-remove
void AbortSignal::remove_abort_algorithm(AbortAlgorithmID id)
{
// To remove an algorithm algorithm from an AbortSignal signal, remove algorithm from signals abort algorithms.
m_abort_algorithms.remove(id);
}
// https://dom.spec.whatwg.org/#abortsignal-signal-abort
@ -76,8 +84,8 @@ void AbortSignal::signal_abort(JS::Value reason)
// https://dom.spec.whatwg.org/#run-the-abort-steps
auto run_the_abort_steps = [](auto& signal) {
// 1. For each algorithm in signals abort algorithms: run algorithm.
for (auto& algorithm : signal.m_abort_algorithms)
algorithm->function()();
for (auto const& algorithm : signal.m_abort_algorithms)
algorithm.value->function()();
// 2. Empty signals abort algorithms.
signal.m_abort_algorithms.clear();