LibWeb: Make requestAnimationFrame() callback IDs sequential

This is required by the spec, so let's stop returning random IDs in
favor of a simple sequential integer sequence.
This commit is contained in:
Andreas Kling 2024-08-04 16:36:50 +02:00 committed by Andreas Kling
commit 0e1256e5a4
Notes: github-actions[bot] 2024-08-05 07:13:03 +00:00
6 changed files with 32 additions and 14 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,9 +9,9 @@
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/IDAllocator.h>
#include <LibCore/Timer.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::HTML {
@ -24,22 +25,21 @@ struct AnimationFrameCallbackDriver {
});
}
i32 add(Callback handler)
[[nodiscard]] WebIDL::UnsignedLong add(Callback handler)
{
auto id = m_id_allocator.allocate();
auto id = ++m_animation_frame_callback_identifier;
m_callbacks.set(id, move(handler));
if (!m_timer->is_active())
m_timer->start();
return id;
}
bool remove(i32 id)
bool remove(WebIDL::UnsignedLong id)
{
auto it = m_callbacks.find(id);
if (it == m_callbacks.end())
return false;
m_callbacks.remove(it);
m_id_allocator.deallocate(id);
return true;
}
@ -56,8 +56,10 @@ struct AnimationFrameCallbackDriver {
}
private:
OrderedHashMap<i32, Callback> m_callbacks;
IDAllocator m_id_allocator;
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#animation-frame-callback-identifier
WebIDL::UnsignedLong m_animation_frame_callback_identifier { 0 };
OrderedHashMap<WebIDL::UnsignedLong, Callback> m_callbacks;
RefPtr<Core::Timer> m_timer;
};