Coder Social home page Coder Social logo

romdo / go-debounce Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 26 KB

Go package that provides a number of debounce patterns as a set of easy to use functions.

Home Page: https://pkg.go.dev/github.com/romdo/go-debounce

License: MIT License

Makefile 9.17% Go 90.83%
debounce debounced debouncer go golang package

go-debounce's Introduction

debounce

Go package that provides a number of debounce patterns as a set of easy to use functions.

Go Reference GitHub tag (latest SemVer) Actions Status Coverage GitHub issues GitHub pull requests License Status

Provides functions to debounce function calls, i.e., to ensure that a function is only executed after a certain amount of time has passed since the last call.

Debouncing can be useful in scenarios where function calls may be triggered rapidly, such as in response to user input, but the underlying operation is expensive and only needs to be performed once per batch of calls.

This package provides several debouncing functions, each with different characteristics:

  • New: creates a new debounced function that will wait a fixed duration before calling the original function, regardless of how many times the debounced function is called in the meantime.
  • NewMutable: creates a new debounced function that will wait a fixed duration before calling the last function that was passed to the debounced function, regardless of how many times the debounced function is called in the meantime. This allows the function to be changed dynamically.
  • NewWithMaxWait: creates a new debounced function that will wait a fixed duration before calling the original function, but will also enforce a maximum wait time, after which the function will be called regardless of whether new calls have been made. This ensures that the function is not delayed indefinitely if calls keep coming in.
  • NewMutableWithMaxWait: creates a new debounced function that combines the characteristics of NewMutable and NewWithMaxWait, i.e., it will wait a fixed duration before calling the last function that was passed to the debounced function, but will also enforce a maximum wait time. All debouncing functions are safe for concurrent use in goroutines and can be called multiple times.

Import

import "github.com/romdo/go-debounce"

Usage

func main() {
	// Create a new debouncer that will wait 100 milliseconds since the last
	// call before calling the callback function.
	debounced, _ := debounce.New(100*time.Millisecond, func() {
		fmt.Println("Hello, world!")
	})

	debounced()
	time.Sleep(75 * time.Millisecond) // +75ms = 75ms
	debounced()
	time.Sleep(75 * time.Millisecond) // +75ms = 150ms
	debounced()
	time.Sleep(150 * time.Millisecond) // +150ms = 300ms, wait expired at 250ms

	// Output:
	// Hello, world!
}

Documentation

Please see the Go Reference.

Contributing

Contributions to this package are welcome! Please open an issue or a pull request on GitHub.

License

MIT

go-debounce's People

Contributors

github-actions[bot] avatar jimeh avatar

Watchers

 avatar

go-debounce's Issues

README.md links to "debouce" instead of "debounce" (missing "n")

## Documentation

Please see the
[Go Reference](https://pkg.go.dev/github.com/romdo/go-debouce#section-documentation).

should be

## Documentation

Please see the
[Go Reference](https://pkg.go.dev/github.com/romdo/go-debounce#section-documentation).

The current typo leads to a page saying:

Oops! We couldn't find “github.com/romdo/go-debouce”.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.