1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:17:34 +00:00

LibWeb: Implement the infrastructure necessary for requestIdleCallback

This includes a bug fix for the event loop processing steps which has
not been merged yet: https://github.com/whatwg/html/pull/7768
This commit is contained in:
Simon Wanner 2022-03-31 21:55:01 +02:00 committed by Linus Groh
parent 73da139cd7
commit 836d2ff259
6 changed files with 209 additions and 31 deletions

View file

@ -1,24 +1,43 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/RequestIdleCallback/IdleDeadline.h>
namespace Web::RequestIdleCallback {
NonnullRefPtr<IdleDeadline> IdleDeadline::create(double time_remaining, bool did_timeout)
NonnullRefPtr<IdleDeadline> IdleDeadline::create(bool did_timeout)
{
return adopt_ref(*new IdleDeadline(time_remaining, did_timeout));
return adopt_ref(*new IdleDeadline(did_timeout));
}
IdleDeadline::IdleDeadline(double time_remaining, bool did_timeout)
: m_time_remaining(time_remaining)
, m_did_timeout(did_timeout)
IdleDeadline::IdleDeadline(bool did_timeout)
: m_did_timeout(did_timeout)
{
}
IdleDeadline::~IdleDeadline() = default;
// https://w3c.github.io/requestidlecallback/#dom-idledeadline-timeremaining
double IdleDeadline::time_remaining() const
{
auto const& event_loop = HTML::main_thread_event_loop();
// 1. Let now be a DOMHighResTimeStamp representing current high resolution time in milliseconds.
auto now = event_loop.unsafe_shared_current_time();
// 2. Let deadline be the result of calling IdleDeadline's get deadline time algorithm.
auto deadline = event_loop.compute_deadline();
// 3. Let timeRemaining be deadline - now.
auto time_remaining = deadline - now;
// 4. If timeRemaining is negative, set it to 0.
if (time_remaining < 0)
time_remaining = 0;
// 5. Return timeRemaining.
// NOTE: coarsening to milliseconds
return ceil(time_remaining);
}
}