Skip to content

Debouncer library that debounces signal to avoid frequently spamming triggered event.

License

Notifications You must be signed in to change notification settings

cloud-ignite/godebouncer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Debouncer

Go Reference build Go Report Card Built with WeBuild License: MIT

Go Debouncer is a Go language library. It makes sure that the pre-defined function is only triggered once per client's signals during a fixed duration.

It allows creating a debouncer that delays invoking a triggered function until after the duration has elapsed since the last time the SendSingal was invoked.

GoDebouncer_drawio

Quickstart

Import library to your project

go get -u github.com/vnteamopen/godebouncer

From your code, you can try to create debouncer.

package main

import (
	"fmt"
	"time"

	"github.com/vnteamopen/godebouncer"
)

func main() {
	debouncer := godebouncer.New(5 * time.Second).WithTriggered(func() {
		fmt.Println("Trigger") // Triggered func will be called after 5 seconds from last SendSignal().
	})

	fmt.Println("Action 1")
	debouncer.SendSignal()

	time.Sleep(1 * time.Second)

	fmt.Println("Action 2")
	debouncer.SendSignal()

	// After 5 seconds, the trigger will be called.
	// Previous `SendSignal()` will be ignored to trigger the triggered function.
	<-debouncer.Done()
}

Anything else?

Do

Allows defining actions before calling SendSignal(). They are synchronous.

debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
	fmt.Println("Trigger") // Triggered func will be called after 10 seconds from last SendSignal().
})

debouncer.Do(func() {
	fmt.Println("Action 1")
})
// Debouncer run the argument function of Do() then SendSignal(). They run sequentially.
// After 10 seconds from finishing Do(), the triggered function will be called.

Cancel

Allows cancelling the timer from the last function SendSignal(). The scheduled triggered function is cancelled and doesn't invoke.

debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
	fmt.Println("Trigger") // Triggered func will be called after 10 seconds from last SendSignal().
})

debouncer.SendSignal()
debouncer.Cancel() // No triggered function is called

Update triggered function

Allows replacing triggered function.

debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
	fmt.Println("Trigger 1") // Triggered func will be called after 10 seconds from last SendSignal().
})

debouncer.SendSignal()
debouncer.UpdateTriggeredFunc(func() {
	fmt.Println("Trigger 2")
})

// Output: "Trigger 2" after 10 seconds

Update waiting time duration

Allows replacing the waiting time duration. You need to call a SendSignal() again to trigger a new timer with a new waiting time duration.

debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
	fmt.Println("Trigger") // Triggered func will be called after 10 seconds from last SendSignal().
})

debouncer.UpdateTimeDuration(20 * time.Millisecond)
debouncer.SendSignal()
// Output: "Trigger" after 20 seconds

Let the caller knows when the triggered function has been invoked

Allows the caller of godebouncer knows when the triggered function is done invoking to synchronize execution across goroutines.

debouncer := godebouncer.New(1 * time.Second).WithTriggered(func() {
	fmt.Println("Fetching...")
	time.Sleep(2 * time.Second)
	fmt.Println("Done")
})

debouncer.SendSignal()

<-debouncer.Done() // The current goroutine will wait until the triggered func finish its execution.

fmt.Println("After done")

Pass any to your function

package main

import (
	"fmt"
	"time"

	"github.com/DanielRenne/GoCore/core/debouncer"
)

func main() {
	type myStruct struct {
		Boolean bool
		Integer int
		String string
	}
	d := debouncer.New(5 * time.Second).WithAny(func(myData any) {
		fmt.Printf("%#v", myData) // Triggered func will be called after 5 seconds from last SendSignal().
	})

	fmt.Println("Action 1")
	d.SendSignalWithData(&myStruct{
		Boolean: false,
		Integer: 5,
		String: "Will not show",
	})

	time.Sleep(1 * time.Second)

	fmt.Println("Action 2")
	d.SendSignalWithData(&myStruct{
		Boolean: true,
		Integer: 5,
		String: "Will show because last one in wins!",
	})

	// After 5 seconds, the trigger will be called.
	// Previous `SendSignal()` will be ignored to trigger the triggered function.
	<-d.Done()
}

License

MIT

Contribution

All your contributions to project and make it better, they are welcome. Feel free to start an issue.

Core contributors:

Thanks! 🙌

Stargazers repo roster for @vnteamopen/godebouncer

About

Debouncer library that debounces signal to avoid frequently spamming triggered event.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%