1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:57:35 +00:00

LibCore: Add Core::Timer::create_single_shot()

This is just a convenience function for creating single-shot timers.
This commit is contained in:
Andreas Kling 2020-04-07 22:15:22 +02:00
parent f813041f67
commit a53cf81374
4 changed files with 33 additions and 35 deletions

View file

@ -32,8 +32,16 @@
namespace Core { namespace Core {
class Timer final : public Object { class Timer final : public Object {
C_OBJECT(Timer) C_OBJECT(Timer);
public: public:
static NonnullRefPtr<Timer> create_single_shot(int interval, Function<void()>&& timeout_handler, Object* parent = nullptr)
{
auto timer = adopt(*new Timer(interval, move(timeout_handler), parent));
timer->set_single_shot(true);
return timer;
}
virtual ~Timer() override; virtual ~Timer() override;
void start(); void start();

View file

@ -62,12 +62,9 @@ Document::Document()
, m_style_resolver(make<StyleResolver>(*this)) , m_style_resolver(make<StyleResolver>(*this))
, m_window(Window::create_with_document(*this)) , m_window(Window::create_with_document(*this))
{ {
m_style_update_timer = Core::Timer::construct(); m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
m_style_update_timer->set_single_shot(true);
m_style_update_timer->set_interval(0);
m_style_update_timer->on_timeout = [this] {
update_style(); update_style();
}; });
} }
Document::~Document() Document::~Document()

View file

@ -165,14 +165,14 @@ void DHCPv4Client::handle_ack(const DHCPv4Packet& packet, const ParsedDHCPv4Opti
auto new_ip = packet.yiaddr(); auto new_ip = packet.yiaddr();
auto lease_time = convert_between_host_and_network(options.get<u32>(DHCPOption::IPAddressLeaseTime).value_or(transaction->offered_lease_time)); auto lease_time = convert_between_host_and_network(options.get<u32>(DHCPOption::IPAddressLeaseTime).value_or(transaction->offered_lease_time));
// set a timer for the duration of the lease, we shall renew if needed // set a timer for the duration of the lease, we shall renew if needed
Core::Timer::construct( Core::Timer::create_single_shot(
lease_time * 1000, [this, transaction, interface = InterfaceDescriptor { interface }, new_ip] { lease_time * 1000,
[this, transaction, interface = InterfaceDescriptor { interface }, new_ip] {
transaction->accepted_offer = false; transaction->accepted_offer = false;
transaction->has_ip = false; transaction->has_ip = false;
dhcp_discover(interface, new_ip); dhcp_discover(interface, new_ip);
}, },
this) this);
->set_single_shot(true);
set_params(transaction->interface, new_ip, options.get<IPv4Address>(DHCPOption::SubnetMask).value(), options.get_many<IPv4Address>(DHCPOption::Router, 1).first()); set_params(transaction->interface, new_ip, options.get<IPv4Address>(DHCPOption::SubnetMask).value(), options.get_many<IPv4Address>(DHCPOption::Router, 1).first());
} }
@ -189,12 +189,12 @@ void DHCPv4Client::handle_nak(const DHCPv4Packet& packet, const ParsedDHCPv4Opti
transaction->accepted_offer = false; transaction->accepted_offer = false;
transaction->has_ip = false; transaction->has_ip = false;
auto& iface = transaction->interface; auto& iface = transaction->interface;
Core::Timer::construct( Core::Timer::create_single_shot(
10000, [this, iface = InterfaceDescriptor { iface }] { 10000,
[this, iface = InterfaceDescriptor { iface }] {
dhcp_discover(iface); dhcp_discover(iface);
}, },
this) this);
->set_single_shot(true);
} }
void DHCPv4Client::process_incoming(const DHCPv4Packet& packet) void DHCPv4Client::process_incoming(const DHCPv4Packet& packet)

View file

@ -62,30 +62,23 @@ WallpaperMode mode_to_enum(const String& name)
Compositor::Compositor() Compositor::Compositor()
{ {
m_compose_timer = add<Core::Timer>(); m_compose_timer = Core::Timer::create_single_shot(
m_immediate_compose_timer = add<Core::Timer>(); 1000 / 60,
[this] {
notify_display_links();
compose();
},
this);
m_immediate_compose_timer = Core::Timer::create_single_shot(
0,
[this] {
compose();
},
this);
m_screen_can_set_buffer = Screen::the().can_set_buffer(); m_screen_can_set_buffer = Screen::the().can_set_buffer();
init_bitmaps(); init_bitmaps();
m_compose_timer->on_timeout = [&]() {
notify_display_links();
#if defined(COMPOSITOR_DEBUG)
dbgprintf("Compositor: delayed frame callback: %d rects\n", m_dirty_rects.size());
#endif
compose();
};
m_compose_timer->set_single_shot(true);
m_compose_timer->set_interval(1000 / 60);
m_immediate_compose_timer->on_timeout = [this]() {
#if defined(COMPOSITOR_DEBUG)
dbgprintf("Compositor: immediate frame callback: %d rects\n", m_dirty_rects.size());
#endif
compose();
};
m_immediate_compose_timer->set_single_shot(true);
m_immediate_compose_timer->set_interval(0);
} }
void Compositor::init_bitmaps() void Compositor::init_bitmaps()