File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed
Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -16,21 +16,24 @@ def debounce(interval_s, keys=None):
1616 """Debounce calls to this function until interval_s seconds have passed."""
1717 def wrapper (func ):
1818 arg_cache = {}
19+ lock = threading .Lock ()
1920
2021 @functools .wraps (func )
2122 def cleanup (* args , ** kwargs ):
2223 input_hash = _hash_input (keys , * args , ** kwargs )
23- del arg_cache [input_hash ]
24+ with lock :
25+ del arg_cache [input_hash ]
2426 return func (* args , ** kwargs )
2527
2628 @functools .wraps (func )
2729 def debounced (* args , ** kwargs ):
2830 input_hash = _hash_input (keys , * args , ** kwargs )
29- if input_hash in arg_cache :
30- arg_cache [input_hash ].cancel ()
31- timer = threading .Timer (interval_s , cleanup , args , kwargs )
32- arg_cache [input_hash ] = timer
33- timer .start ()
31+ with lock :
32+ if input_hash in arg_cache :
33+ arg_cache [input_hash ].cancel ()
34+ timer = threading .Timer (interval_s , cleanup , args , kwargs )
35+ arg_cache [input_hash ] = timer
36+ timer .start ()
3437 return debounced
3538 return wrapper
3639
You can’t perform that action at this time.
0 commit comments