Преглед изворни кода

LibCore: Add Core::EventLoop::spin_until(Function<bool()>)

This function spins the event loop until a goal condition is met.
Andreas Kling пре 3 година
родитељ
комит
f2b9ec9f8a
2 измењених фајлова са 9 додато и 0 уклоњено
  1. 7 0
      Userland/Libraries/LibCore/EventLoop.cpp
  2. 2 0
      Userland/Libraries/LibCore/EventLoop.h

+ 7 - 0
Userland/Libraries/LibCore/EventLoop.cpp

@@ -366,6 +366,13 @@ int EventLoop::exec()
     VERIFY_NOT_REACHED();
 }
 
+void EventLoop::spin_until(Function<bool()> goal_condition)
+{
+    EventLoopPusher pusher(*this);
+    while (!goal_condition())
+        pump();
+}
+
 void EventLoop::pump(WaitMode mode)
 {
     wait_for_event(mode);

+ 2 - 0
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<bool()>);
+
     void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
 
     static EventLoop& main();