textual
textual copied to clipboard
Adds the debounce decorator
We add the debounce decorator. It can be used to ensure an event handler does not get overwhelmed with events, or that expensive operations (e.g. network requests) do not happen too frequently.
PR against main
as requested. Let me know what your thoughts are. (I've removed the edit to the dictionary example but let me know if you think that's worthwhile to add back in)
@willmcgugan is there any blocker on this?
@willmcgugan if the new work
decorator grew a delay
parameter then delay
+ exclusive
would equal debounce
...
One thought though: does delay
make sense without exclusive
? So could exclusive
take an int/float as a delay?
@lllama or debounce can be implemented with work, using exclusive and adding delay code on top
Yeah. @willmcgugan is a crafty one. Just stick an await asyncio.sleep
in your function and set exclusive
.
Some potential ideas for other options to go with exclusive
: http://ember-concurrency.com/docs/task-concurrency
Maybe also some docs inspiration.
@lllama @willmcgugan I was following this topic, because I would happily use this feature, but I am not sure what is the result now. Is this going to land in near future?
So if you have a look here: https://textual.textualize.io/guide/workers/#work-decorator
So previously with the debounce
decorator, your task would be created in a Timer
. If a new event came in, then the task was cancelled and a new one created. A similar thing now happens with the @work
decorator when you set the exclusive=True
parameter.
So to achieve a debounce, you'll create a task using @work(exclusive=True)
and then the first thing in your code will be an asyncio.sleep(...)
for whatever your debounce value is. If a new task comes in before the sleep starts, then the task will be cancelled and you'll get the debounce behaviour.
It works! You're the man @lllama! Thank you very much.