debouncer

package module
v0.0.0-...-a499989 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 3 Imported by: 0

README ¶

GoCore Debouncer

Go Reference build Go Report Card Built with WeBuild

Originally forked from vnteamopen/godebouncer but added the ability to pass interface/any to the functions which is very useful.

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/DanielRenne/GoCore/core/debouncer

From your code, you can try to create debouncer.

package main

import (
	"fmt"
	"time"

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

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

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

	time.Sleep(1 * time.Second)

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

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

Anything else?

Do

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

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

d.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.

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

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

Update triggered function

Allows replacing triggered function.

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

d.SendSignal()
d.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.

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

d.UpdateTimeDuration(20 * time.Millisecond)
d.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.

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

d.SendSignal()

<-d.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! 🙌

Documentation ¶

Overview ¶

Package debouncer provides feature to make 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.*/

Example ¶
package main

import (
	"fmt"
	"time"

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

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

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

	time.Sleep(1 * time.Second)

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

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

Index ¶

Examples ¶

Constants ¶

View Source
const (
	// ErrorTypeIncorrectSendSignal if you call SendSignal on something you configured WithAny
	ErrorTypeIncorrectSendSignal = "you are using sendsignal with something setup withany"
	// ErrorTypeIncorrectSendSignalWithAny  if you call SendSignalWithAny on something you configured WithTriggered
	ErrorTypeIncorrectSendSignalWithAny = "you are using sendsignalwithany with something setup withtriggered"
)

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Debouncer ¶

type Debouncer struct {
	// contains filtered or unexported fields
}

Debouncer main struct for debouncer package

func New ¶

func New(duration time.Duration) *Debouncer

New creates a new instance of debouncer. Each instance of debouncer works independent, concurrency with different wait duration.

func (*Debouncer) Cancel ¶

func (d *Debouncer) Cancel()

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

func (*Debouncer) Do ¶

func (d *Debouncer) Do(signalFunc func())

Do run the signalFunc() and call SendSignal() after all. The signalFunc() and SendSignal() function run sequentially.

func (*Debouncer) DoAny ¶

func (d *Debouncer) DoAny(signalFunc func(any), anyVar any)

DoAny run the signalFunc(any) and call SendSignalWithData(any) after all. The signalFunc() and SendSignal() function run sequentially.

func (*Debouncer) Done ¶

func (d *Debouncer) Done() <-chan struct{}

Done returns a receive-only channel to notify the caller when the triggered func has been executed.

func (*Debouncer) SendSignal ¶

func (d *Debouncer) SendSignal() (err error)

SendSignal makes an action that notifies to invoke the triggered function after a wait duration.

func (*Debouncer) SendSignalWithData ¶

func (d *Debouncer) SendSignalWithData(anyVar any) (err error)

SendSignalWithData makes an action that notifies to invoke the triggered function after a wait duration.

func (*Debouncer) UpdateAnyFunc ¶

func (d *Debouncer) UpdateAnyFunc(newTriggeredFunc func(any))

UpdateAnyFunc replaces triggered function.

func (*Debouncer) UpdateTimeDuration ¶

func (d *Debouncer) UpdateTimeDuration(newTimeDuration time.Duration)

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

func (*Debouncer) UpdateTriggeredFunc ¶

func (d *Debouncer) UpdateTriggeredFunc(newTriggeredFunc func())

UpdateTriggeredFunc replaces triggered function.

func (*Debouncer) WithAny ¶

func (d *Debouncer) WithAny(triggeredFunc func(any)) *Debouncer

WithAny attached a triggered function to debouncer instance and return the same instance of debouncer to use.

func (*Debouncer) WithTriggered ¶

func (d *Debouncer) WithTriggered(triggeredFunc func()) *Debouncer

WithTriggered attached a triggered function to debouncer instance and return the same instance of debouncer to use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL