LibWeb: Implement an ad-hoc version of EventLoop::spin_until(condition)

This doesn't follow the exact spec steps but instead simply makes a
nested Core::EventLoop and spins it while a periodic timer tests the
goal condition.
This commit is contained in:
Andreas Kling 2021-09-20 16:53:01 +02:00
parent 60d0f041b7
commit 398692722b
Notes: sideshowbarker 2024-07-18 03:36:36 +09:00

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/EventLoop.h>
#include <LibCore/Timer.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/MainThreadVM.h>
@ -46,6 +47,19 @@ EventLoop& main_thread_event_loop()
// https://html.spec.whatwg.org/multipage/webappapis.html#spin-the-event-loop
void EventLoop::spin_until([[maybe_unused]] Function<bool()> goal_condition)
{
// FIXME: This is an ad-hoc hack until we implement the proper mechanism.
if (goal_condition())
return;
Core::EventLoop loop;
auto timer = Core::Timer::create_repeating(16, [&] {
if (goal_condition())
loop.quit(0);
});
timer->start();
loop.exec();
// Real spec steps:
// FIXME: 1. Let task be the event loop's currently running task.
// FIXME: 2. Let task source be task's source.