From f2b9ec9f8a1f1a4cc503fb4fddf97d7c27bdee79 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 25 Sep 2021 19:32:14 +0200 Subject: [PATCH] LibCore: Add Core::EventLoop::spin_until(Function) This function spins the event loop until a goal condition is met. --- Userland/Libraries/LibCore/EventLoop.cpp | 7 +++++++ Userland/Libraries/LibCore/EventLoop.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 081c34acdb..fafd7aaae3 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -366,6 +366,13 @@ int EventLoop::exec() VERIFY_NOT_REACHED(); } +void EventLoop::spin_until(Function goal_condition) +{ + EventLoopPusher pusher(*this); + while (!goal_condition()) + pump(); +} + void EventLoop::pump(WaitMode mode) { wait_for_event(mode); diff --git a/Userland/Libraries/LibCore/EventLoop.h b/Userland/Libraries/LibCore/EventLoop.h index 047de38234..9b1a660e80 100644 --- a/Userland/Libraries/LibCore/EventLoop.h +++ b/Userland/Libraries/LibCore/EventLoop.h @@ -44,6 +44,8 @@ public: // this should really only be used for integrating with other event loops void pump(WaitMode = WaitMode::WaitForEvents); + void spin_until(Function); + void post_event(Object& receiver, NonnullOwnPtr&&); static EventLoop& main();