debounce based off of inputs#288
Conversation
| lock = threading.Lock() | ||
|
|
||
| @functools.wraps(func) | ||
| def cleanup(*args, **kwargs): |
| def debounce(interval_s, keys=None): | ||
| """Debounce calls to this function until interval_s seconds have passed.""" | ||
| def wrapper(func): | ||
| arg_cache = {} |
| del arg_cache[input_hash] | ||
| return func(*args, **kwargs) | ||
|
|
||
| @functools.wraps(func) |
There was a problem hiding this comment.
Remove the wraps decoration
There was a problem hiding this comment.
Actually, this is wrong. We should leave it on debounced and remove it from cleanup. The decorator basically copies the docstrings / other properties over. No point having it on cleanup. It's debounced that we return
|
|
||
| @functools.wraps(func) | ||
| def cleanup(*args, **kwargs): | ||
| input_hash = _hash_input(keys, *args, **kwargs) |
There was a problem hiding this comment.
Hoist computing the input_hash up to where you create the lock
There was a problem hiding this comment.
you can't do that! the input_hash depends on the input and has to be recomputed each time the function is called. meanwhile the lock is just a member of the debounce closure.
There was a problem hiding this comment.
Oh yup, you're right. You could pass the key to cleanup as an argument. But not sure you gain much
| return wrapper | ||
|
|
||
|
|
||
| def _hash_input(keys, *args, **kwargs): |
There was a problem hiding this comment.
Get rid of this and just use kwargs[key] - can make it more complicate if/when you need it
key the debouncing off of the value of the inputs. Closes #285