serenity/Userland/Libraries/LibCore/Debounce.h
Sam Atkins a8cf0c9371 LibCore+Userland: Make Core::Timer::create_single_shot() return ErrorOr
clang-format sure has some interesting opinions about where to put a
method call that comes after a lambda. :thonk:
2023-01-12 11:25:51 +01:00

30 lines
682 B
C++

/*
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibCore/Timer.h>
namespace Core {
template<typename TFunction>
auto debounce(TFunction function, int timeout)
{
RefPtr<Core::Timer> timer;
return [=]<typename... T>(T... args) mutable {
auto apply_function = [=] { function(args...); };
if (timer) {
timer->stop();
timer->on_timeout = move(apply_function);
} else {
timer = Core::Timer::create_single_shot(timeout, move(apply_function)).release_value_but_fixme_should_propagate_errors();
}
timer->start();
};
};
}