summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2021-08-30 10:43:28 +0000
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-09-02 03:47:47 +0430
commite9121f8b1f9d31f0da89f34eac69a7da9f2901a0 (patch)
tree5e7087354e3a34b0f8494c60a16dfd64d5221a77 /Userland/Libraries/LibCore
parentac24842f4895c9b7b46b217617407841cca2334f (diff)
downloadserenity-e9121f8b1f9d31f0da89f34eac69a7da9f2901a0.zip
LibCore+Userland: Implement Core::deferred_invoke
Core::deferred_invoke is a way of executing an action after previously queued events have been processed. It removes the requirement of having/being a Core::Object subclass in order to defer invocation through Core::Object::deferred_invoke. Core::Object::deferred_invoke now delegates to Core::deferred_invoke. The version with the Object& argument is still present but will be removed in the following commits. This commit additionally fixes a new places where the DeferredInvocationEvent was dispatched to the event loop directly, and replaces them with the Core::deferred_invoke equivalent.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r--Userland/Libraries/LibCore/DeferredInvocationContext.h19
-rw-r--r--Userland/Libraries/LibCore/Event.h7
-rw-r--r--Userland/Libraries/LibCore/EventLoop.cpp2
-rw-r--r--Userland/Libraries/LibCore/EventLoop.h14
-rw-r--r--Userland/Libraries/LibCore/Forward.h1
-rw-r--r--Userland/Libraries/LibCore/Object.cpp7
-rw-r--r--Userland/Libraries/LibCore/Object.h1
7 files changed, 47 insertions, 4 deletions
diff --git a/Userland/Libraries/LibCore/DeferredInvocationContext.h b/Userland/Libraries/LibCore/DeferredInvocationContext.h
new file mode 100644
index 0000000000..b0e0e19c2b
--- /dev/null
+++ b/Userland/Libraries/LibCore/DeferredInvocationContext.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2018-2020, sin-ack <sin-ack@protonmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibCore/Object.h>
+
+namespace Core {
+
+class DeferredInvocationContext final : public Core::Object {
+ C_OBJECT(DeferredInvocationContext)
+private:
+ DeferredInvocationContext() { }
+};
+
+}
diff --git a/Userland/Libraries/LibCore/Event.h b/Userland/Libraries/LibCore/Event.h
index 4de780a5f9..bc7045704b 100644
--- a/Userland/Libraries/LibCore/Event.h
+++ b/Userland/Libraries/LibCore/Event.h
@@ -10,6 +10,7 @@
#include <AK/String.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
+#include <LibCore/DeferredInvocationContext.h>
#include <LibCore/Forward.h>
namespace Core {
@@ -50,14 +51,16 @@ class DeferredInvocationEvent : public Event {
friend class EventLoop;
public:
- DeferredInvocationEvent(Function<void(Object&)> invokee)
+ DeferredInvocationEvent(NonnullRefPtr<DeferredInvocationContext> context, Function<void()> invokee)
: Event(Event::Type::DeferredInvoke)
+ , m_context(move(context))
, m_invokee(move(invokee))
{
}
private:
- Function<void(Object&)> m_invokee;
+ NonnullRefPtr<DeferredInvocationContext> m_context;
+ Function<void()> m_invokee;
};
class TimerEvent final : public Event {
diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp
index 436aba6ee5..081c34acdb 100644
--- a/Userland/Libraries/LibCore/EventLoop.cpp
+++ b/Userland/Libraries/LibCore/EventLoop.cpp
@@ -394,7 +394,7 @@ void EventLoop::pump(WaitMode mode)
}
} else if (event.type() == Event::Type::DeferredInvoke) {
dbgln_if(DEFERRED_INVOKE_DEBUG, "DeferredInvoke: receiver = {}", *receiver);
- static_cast<DeferredInvocationEvent&>(event).m_invokee(*receiver);
+ static_cast<DeferredInvocationEvent&>(event).m_invokee();
} else {
NonnullRefPtr<Object> protector(*receiver);
receiver->dispatch_event(event);
diff --git a/Userland/Libraries/LibCore/EventLoop.h b/Userland/Libraries/LibCore/EventLoop.h
index 116d23c023..047de38234 100644
--- a/Userland/Libraries/LibCore/EventLoop.h
+++ b/Userland/Libraries/LibCore/EventLoop.h
@@ -11,9 +11,12 @@
#include <AK/HashMap.h>
#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h>
+#include <AK/NonnullRefPtr.h>
#include <AK/Time.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
+#include <LibCore/DeferredInvocationContext.h>
+#include <LibCore/Event.h>
#include <LibCore/Forward.h>
#include <sys/time.h>
#include <sys/types.h>
@@ -76,6 +79,12 @@ public:
static bool has_been_instantiated();
+ void deferred_invoke(Function<void()> invokee)
+ {
+ auto context = DeferredInvocationContext::construct();
+ post_event(context, make<Core::DeferredInvocationEvent>(context, move(invokee)));
+ }
+
private:
void wait_for_event(WaitMode);
Optional<Time> get_next_timer_expiration();
@@ -106,4 +115,9 @@ private:
NonnullOwnPtr<Private> m_private;
};
+inline void deferred_invoke(Function<void()> invokee)
+{
+ EventLoop::current().deferred_invoke(move(invokee));
+}
+
}
diff --git a/Userland/Libraries/LibCore/Forward.h b/Userland/Libraries/LibCore/Forward.h
index 5bd7941640..71381bc79d 100644
--- a/Userland/Libraries/LibCore/Forward.h
+++ b/Userland/Libraries/LibCore/Forward.h
@@ -15,6 +15,7 @@ class ConfigFile;
class CustomEvent;
class DateTime;
class DirIterator;
+class DeferredInvocationContext;
class ElapsedTimer;
class Event;
class EventLoop;
diff --git a/Userland/Libraries/LibCore/Object.cpp b/Userland/Libraries/LibCore/Object.cpp
index 9009d4c850..cc5be65d54 100644
--- a/Userland/Libraries/LibCore/Object.cpp
+++ b/Userland/Libraries/LibCore/Object.cpp
@@ -164,7 +164,12 @@ void Object::dump_tree(int indent)
void Object::deferred_invoke(Function<void(Object&)> invokee)
{
- Core::EventLoop::current().post_event(*this, make<Core::DeferredInvocationEvent>(move(invokee)));
+ deferred_invoke([invokee = move(invokee), this] { invokee(*this); });
+}
+
+void Object::deferred_invoke(Function<void()> invokee)
+{
+ Core::deferred_invoke([invokee = move(invokee), strong_this = NonnullRefPtr(*this)] { invokee(); });
}
void Object::save_to(JsonObject& json)
diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h
index fee533ed84..606d0ebeb1 100644
--- a/Userland/Libraries/LibCore/Object.h
+++ b/Userland/Libraries/LibCore/Object.h
@@ -130,6 +130,7 @@ public:
void dump_tree(int indent = 0);
void deferred_invoke(Function<void(Object&)>);
+ void deferred_invoke(Function<void()>);
void save_to(JsonObject&);