mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 18:12:43 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			29 lines
		
	
	
	
		
			681 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			29 lines
		
	
	
	
		
			681 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(int timeout, TFunction function)
 | |
| {
 | |
|     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();
 | |
|     };
 | |
| }
 | |
| 
 | |
| }
 | 
