mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:58:11 +00:00
LibWeb: Stop inactive requestAnimationFrame() callbacks from running
Previously requestAnimationFrame() callbacks were registered with a static global RequestAnimationFrameDriver shared between all windows. This led to callbacks still running after navigating away from a page (This could be seen with the WASM GoL demo). This commit moves the RequestAnimationFrameDriver (now AnimationFrameCallbackDriver) to be a member of the HTML::Window object, then uses the 'active document' parameter of run_animation_frame_callbacks() to run only the active callbacks.
This commit is contained in:
parent
18cad73b01
commit
3cfa9b63b5
3 changed files with 72 additions and 83 deletions
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/IDAllocator.h>
|
||||
#include <LibCore/Timer.h>
|
||||
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
struct AnimationFrameCallbackDriver {
|
||||
using Callback = Function<void(i32)>;
|
||||
|
||||
AnimationFrameCallbackDriver()
|
||||
{
|
||||
m_timer = Core::Timer::create_single_shot(16, [] {
|
||||
HTML::main_thread_event_loop().schedule();
|
||||
});
|
||||
}
|
||||
|
||||
i32 add(Callback handler)
|
||||
{
|
||||
auto id = m_id_allocator.allocate();
|
||||
m_callbacks.set(id, move(handler));
|
||||
if (!m_timer->is_active())
|
||||
m_timer->start();
|
||||
return id;
|
||||
}
|
||||
|
||||
bool remove(i32 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;
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
auto taken_callbacks = move(m_callbacks);
|
||||
for (auto& [id, callback] : taken_callbacks)
|
||||
callback(id);
|
||||
}
|
||||
|
||||
bool has_callbacks() const
|
||||
{
|
||||
return !m_callbacks.is_empty();
|
||||
}
|
||||
|
||||
private:
|
||||
HashMap<i32, Callback> m_callbacks;
|
||||
IDAllocator m_id_allocator;
|
||||
RefPtr<Core::Timer> m_timer;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue