Skip to content

Jerakin/debounce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

debounce

Debounced function delays invoking the function until after wait seconds have elapsed since the last time the debounced function was invoked. The debounced function comes with a cancel method to cancel delayed function invocations and a flush method to immediately invoke them.

Additional arguments

  • leading=True - If you want it to call your function immediately on invocation.
  • max_wait=x - The maximum time the decorated function is allowed to be delayed before it's invoked.

Installation

pip install py-debounce

Usage

from debounce import debounce

@debounce(0.1)
def handle_event(e):
    """Computationally heavy methods that potentially gets polled a lot."""

Cancel

from debounce import debounce

@debounce(0.1)
def handle_event(e):
    """Computationally heavy methods that potentially gets polled a lot."""


# Imagine that the event has been called
handle_event(...)

# You can then cancel it by calling. The function will now never be called.
handle_event.cancel()

Flush

from debounce import debounce

@debounce(0.1)
def handle_event(e):
    """Computationally heavy methods that potentially gets polled a lot."""


# Imagine that the event has been called
handle_event(...)

# You can flush the call to immediately call it. 
handle_event.flush()

Asyncio

It also handles async functions.

from debounce import debounce

@debounce(0.1)
async def event(e):
    """Computationally heavy methods that potentially gets polled a lot."""

About

Python implementation of debounce functionality.

Topics

Resources

License

Stars

Watchers

Forks

Languages