textual icon indicating copy to clipboard operation
textual copied to clipboard

Adds the debounce decorator

Open lllama opened this issue 2 years ago • 1 comments

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.

lllama avatar Oct 27 '22 09:10 lllama

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)

lllama avatar Oct 27 '22 09:10 lllama

@willmcgugan is there any blocker on this?

tusharsadhwani avatar Jan 07 '23 14:01 tusharsadhwani

@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 avatar Apr 02 '23 07:04 lllama

@lllama or debounce can be implemented with work, using exclusive and adding delay code on top

tusharsadhwani avatar Apr 02 '23 08:04 tusharsadhwani

Yeah. @willmcgugan is a crafty one. Just stick an await asyncio.sleep in your function and set exclusive.

lllama avatar Apr 02 '23 08:04 lllama

Some potential ideas for other options to go with exclusive: http://ember-concurrency.com/docs/task-concurrency

Maybe also some docs inspiration.

lllama avatar Apr 02 '23 08:04 lllama

@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?

ethael avatar Apr 20 '23 13:04 ethael

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.

lllama avatar Apr 20 '23 13:04 lllama

It works! You're the man @lllama! Thank you very much.

ethael avatar Apr 20 '23 14:04 ethael