mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-02 15:46:33 +00:00
LibWeb: Add initial support for AbortController and AbortSignal
The DOM specification says that the primary use case for these is to give Promises abort semantics. It is also a prerequisite for Fetch, as it is used to make Fetch abortable. a
This commit is contained in:
parent
dd1a49ff93
commit
1d8f8ea5b1
Notes:
sideshowbarker
2024-07-18 04:54:37 +09:00
Author: https://github.com/Lubrsi
Commit: 1d8f8ea5b1
Pull-request: https://github.com/SerenityOS/serenity/pull/9738
11 changed files with 254 additions and 0 deletions
58
Userland/Libraries/LibWeb/DOM/AbortSignal.cpp
Normal file
58
Userland/Libraries/LibWeb/DOM/AbortSignal.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/AbortSignalWrapper.h>
|
||||
#include <LibWeb/DOM/AbortSignal.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/EventDispatcher.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
AbortSignal::AbortSignal(Document& document)
|
||||
: EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
|
||||
{
|
||||
}
|
||||
|
||||
AbortSignal::~AbortSignal()
|
||||
{
|
||||
}
|
||||
|
||||
bool AbortSignal::dispatch_event(NonnullRefPtr<Event> event)
|
||||
{
|
||||
return EventDispatcher::dispatch(*this, event);
|
||||
}
|
||||
|
||||
JS::Object* AbortSignal::create_wrapper(JS::GlobalObject& global_object)
|
||||
{
|
||||
return wrap(global_object, *this);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-add
|
||||
void AbortSignal::add_abort_algorithm(Function<void()> abort_algorithm)
|
||||
{
|
||||
if (m_aborted)
|
||||
return;
|
||||
|
||||
m_abort_algorithms.append(move(abort_algorithm));
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#abortsignal-signal-abort
|
||||
void AbortSignal::signal_abort()
|
||||
{
|
||||
if (m_aborted)
|
||||
return;
|
||||
|
||||
m_aborted = true;
|
||||
|
||||
for (auto& algorithm : m_abort_algorithms)
|
||||
algorithm();
|
||||
|
||||
m_abort_algorithms.clear();
|
||||
|
||||
dispatch_event(Event::create(HTML::EventNames::abort));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue