Skip to content

Commit

Permalink
Add scheduleWithContext to event dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Apr 9, 2024
1 parent 4e102d6 commit 8447b4a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/fcitx-utils/eventdispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <functional>
#include <memory>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/trackableobject.h>
#include "fcitxutils_export.h"

namespace fcitx {
Expand Down Expand Up @@ -43,6 +44,7 @@ class FCITXUTILS_EXPORT EventDispatcher {
* thread from event loop.
*/
void detach();

/**
* A thread-safe function to schedule a functor to be call from event loop.
*
Expand All @@ -54,6 +56,33 @@ class FCITXUTILS_EXPORT EventDispatcher {
*/
void schedule(std::function<void()> functor);

/**
* A helper function that allows to only invoke certain function if the
* reference is still valid.
*
* If context object is not valid when calling scheduleWithContext, it won't
* be scheduled at all.
*
* @param context the context object.
* @param functor function to be scheduled
*
* @since 5.1.8
*/
template <typename T>
void scheduleWithContext(TrackableObjectReference<T> context,
std::function<void()> functor) {
if (!context.isValid()) {
return;
}

schedule(
[context = std::move(context), functor = std::move(functor)]() {
if (context.isValid()) {
functor();
}
});
}

/**
* Return the currently attached event loop
*
Expand Down
45 changes: 45 additions & 0 deletions test/testeventdispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <unistd.h>
#include <atomic>
#include <thread>
#include <vector>
#include "fcitx-utils/event.h"
#include "fcitx-utils/eventdispatcher.h"
#include "fcitx-utils/log.h"
#include "fcitx-utils/trackableobject.h"

using namespace fcitx;

Expand Down Expand Up @@ -38,6 +40,22 @@ void basicTest() {
thread.join();
}

void testOrder() {
EventLoop loop;
EventDispatcher dispatcher;
dispatcher.attach(&loop);
std::vector<int> value;
for (int i = 0; i < 100; i++) {
dispatcher.schedule([i, &value]() { value.push_back(i); });
}
dispatcher.schedule([&loop]() { loop.exit(); });
loop.exec();
FCITX_ASSERT(value.size() == 100);
for (int i = 0; i < 100; i++) {
FCITX_ASSERT(i == value[i]) << i << " " << value[i];
}
}

void recursiveSchedule() {
EventDispatcher dispatcher;
EventLoop loop;
Expand All @@ -59,8 +77,35 @@ void recursiveSchedule() {
FCITX_ASSERT(counter == 100);
}

class TestObject : public TrackableObject<TestObject> {};

void withContext() {
EventDispatcher dispatcher;
EventLoop loop;
dispatcher.attach(&loop);
bool called = false;
bool invalidCalled = false;
TestObject validObject;
{
TestObject invalidObject;
dispatcher.scheduleWithContext(validObject.watch(),
[&called]() { called = true; });
dispatcher.scheduleWithContext(
invalidObject.watch(),
[&invalidCalled]() { invalidCalled = true; });
}

dispatcher.schedule([&loop]() { loop.exit(); });
loop.exec();

FCITX_ASSERT(called);
FCITX_ASSERT(!invalidCalled);
}

int main() {
basicTest();
testOrder();
recursiveSchedule();
withContext();
return 0;
}

0 comments on commit 8447b4a

Please sign in to comment.