diff --git a/Userland/Libraries/LibCore/Debounce.h b/Userland/Libraries/LibCore/Debounce.h new file mode 100644 index 0000000000..ba40a145bf --- /dev/null +++ b/Userland/Libraries/LibCore/Debounce.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022, MacDue + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Core { + +template +auto debounce(TFunction function, int timeout) +{ + RefPtr timer; + return [=](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)); + } + timer->start(); + }; +}; + +}