2022-07-18 20:00:03 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibCore/Timer.h>
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
template<typename TFunction>
|
2023-06-14 14:54:46 +01:00
|
|
|
auto debounce(int timeout, TFunction function)
|
2022-07-18 20:00:03 +01:00
|
|
|
{
|
|
|
|
|
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 {
|
2023-01-11 16:31:10 +00:00
|
|
|
timer = Core::Timer::create_single_shot(timeout, move(apply_function)).release_value_but_fixme_should_propagate_errors();
|
2022-07-18 20:00:03 +01:00
|
|
|
}
|
|
|
|
|
timer->start();
|
|
|
|
|
};
|
2023-07-07 22:48:11 -04:00
|
|
|
}
|
2022-07-18 20:00:03 +01:00
|
|
|
|
|
|
|
|
}
|