2021-09-02 02:12:49 +01:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
|
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Bindings/AbortSignalWrapper.h>
|
2021-12-10 20:05:12 +00:00
|
|
|
|
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
|
|
|
|
|
|
#include <LibWeb/Bindings/Wrapper.h>
|
2021-09-02 02:12:49 +01:00
|
|
|
|
#include <LibWeb/DOM/AbortSignal.h>
|
|
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
|
|
#include <LibWeb/DOM/EventDispatcher.h>
|
2021-10-01 01:09:11 +01:00
|
|
|
|
#include <LibWeb/HTML/EventHandler.h>
|
2021-09-02 02:12:49 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
|
|
|
|
|
AbortSignal::AbortSignal(Document& document)
|
|
|
|
|
|
: EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbortSignal::~AbortSignal()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 1. If signal is aborted, then return.
|
|
|
|
|
|
if (aborted())
|
2021-09-02 02:12:49 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 2. Append algorithm to signal’s abort algorithms.
|
2021-09-02 02:12:49 +01:00
|
|
|
|
m_abort_algorithms.append(move(abort_algorithm));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-signal-abort
|
2021-12-10 20:05:12 +00:00
|
|
|
|
void AbortSignal::signal_abort(JS::Value reason)
|
2021-09-02 02:12:49 +01:00
|
|
|
|
{
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 1. If signal is aborted, then return.
|
|
|
|
|
|
if (aborted())
|
2021-09-02 02:12:49 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 2. Set signal’s abort reason to reason if it is given; otherwise to a new "AbortError" DOMException.
|
|
|
|
|
|
if (!reason.is_undefined())
|
|
|
|
|
|
m_abort_reason = reason;
|
|
|
|
|
|
else
|
|
|
|
|
|
m_abort_reason = wrap(wrapper()->global_object(), AbortError::create("Aborted without reason"));
|
2021-09-02 02:12:49 +01:00
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 3. For each algorithm in signal’s abort algorithms: run algorithm.
|
2021-09-02 02:12:49 +01:00
|
|
|
|
for (auto& algorithm : m_abort_algorithms)
|
|
|
|
|
|
algorithm();
|
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 4. Empty signal’s abort algorithms.
|
2021-09-02 02:12:49 +01:00
|
|
|
|
m_abort_algorithms.clear();
|
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 5. Fire an event named abort at signal.
|
2021-09-02 02:12:49 +01:00
|
|
|
|
dispatch_event(Event::create(HTML::EventNames::abort));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-01 01:09:11 +01:00
|
|
|
|
void AbortSignal::set_onabort(HTML::EventHandler event_handler)
|
|
|
|
|
|
{
|
|
|
|
|
|
set_event_handler_attribute(HTML::EventNames::abort, event_handler);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HTML::EventHandler AbortSignal::onabort()
|
|
|
|
|
|
{
|
|
|
|
|
|
return event_handler_attribute(HTML::EventNames::abort);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
void AbortSignal::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
|
{
|
|
|
|
|
|
visitor.visit(m_abort_reason);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-02 02:12:49 +01:00
|
|
|
|
}
|