1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 14:15:07 +00:00

LibWeb: Move DOM event dispatch to its own class

For now, the new DOM::EventDispatcher is very simple, it just iterates
over the set of listeners on an EventTarget and invokes the callbacks
as it goes.

This simplifies EventTarget subclasses since they no longer have to
implement the callback mechanism themselves.
This commit is contained in:
Andreas Kling 2020-09-06 14:28:41 +02:00
parent 521475dc01
commit 8a6a9a8fb6
10 changed files with 185 additions and 30 deletions

View file

@ -31,6 +31,7 @@
#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/DOM/XMLHttpRequest.h>
@ -91,17 +92,12 @@ void XMLHttpRequest::send()
void XMLHttpRequest::dispatch_event(NonnullRefPtr<DOM::Event> event)
{
for (auto& listener : listeners()) {
if (listener.event_name == event->type()) {
auto& function = const_cast<DOM::EventListener&>(*listener.listener).function();
auto& global_object = function.global_object();
auto* this_value = wrap(global_object, *this);
auto& interpreter = function.interpreter();
(void)interpreter.call(function, this_value, wrap(global_object, *this));
if (interpreter.exception())
interpreter.clear_exception();
}
}
DOM::EventDispatcher::dispatch(*this, move(event));
}
Bindings::EventTargetWrapper* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object)
{
return wrap(global_object, *this);
}
}