Let debouncer delay be configurable
This commit is contained in:
		@@ -7,7 +7,7 @@ type Debouncer struct {
 | 
				
			|||||||
	C     <-chan bool
 | 
						C     <-chan bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func New(ch <-chan bool) *Debouncer {
 | 
					func New(ch <-chan bool, window time.Duration) *Debouncer {
 | 
				
			||||||
	output := make(chan bool)
 | 
						output := make(chan bool)
 | 
				
			||||||
	var currentState bool
 | 
						var currentState bool
 | 
				
			||||||
	var lastState bool
 | 
						var lastState bool
 | 
				
			||||||
@@ -21,11 +21,11 @@ func New(ch <-chan bool) *Debouncer {
 | 
				
			|||||||
					output <- false
 | 
										output <- false
 | 
				
			||||||
					lastState = false
 | 
										lastState = false
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					falseTimer.Reset(200 * time.Millisecond)
 | 
										falseTimer.Reset(window)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			case v, ok := <-ch:
 | 
								case v, ok := <-ch:
 | 
				
			||||||
				if !ok {
 | 
									if !ok {
 | 
				
			||||||
					falseTimer.Reset(200 * time.Millisecond)
 | 
										falseTimer.Reset(window)
 | 
				
			||||||
					<-falseTimer.C
 | 
										<-falseTimer.C
 | 
				
			||||||
					output <- false
 | 
										output <- false
 | 
				
			||||||
					close(output)
 | 
										close(output)
 | 
				
			||||||
@@ -35,12 +35,12 @@ func New(ch <-chan bool) *Debouncer {
 | 
				
			|||||||
					if !lastState {
 | 
										if !lastState {
 | 
				
			||||||
						output <- true
 | 
											output <- true
 | 
				
			||||||
						lastState = true
 | 
											lastState = true
 | 
				
			||||||
						falseTimer.Reset(200 * time.Millisecond)
 | 
											falseTimer.Reset(window)
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					currentState = true
 | 
										currentState = true
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					falseTimer.Reset(200 * time.Millisecond)
 | 
										falseTimer.Reset(window)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					currentState = false
 | 
										currentState = false
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,7 @@ import (
 | 
				
			|||||||
func TestDebouncer(t *testing.T) {
 | 
					func TestDebouncer(t *testing.T) {
 | 
				
			||||||
	t.Run("Simple", func(t *testing.T) {
 | 
						t.Run("Simple", func(t *testing.T) {
 | 
				
			||||||
		ch := make(chan bool)
 | 
							ch := make(chan bool)
 | 
				
			||||||
		d := debouncer.New(ch)
 | 
							d := debouncer.New(ch, 200*time.Millisecond)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		go func() {
 | 
							go func() {
 | 
				
			||||||
			ch <- true
 | 
								ch <- true
 | 
				
			||||||
@@ -23,7 +23,7 @@ func TestDebouncer(t *testing.T) {
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
	t.Run("TrueTrueTrueFalse", func(t *testing.T) {
 | 
						t.Run("TrueTrueTrueFalse", func(t *testing.T) {
 | 
				
			||||||
		ch := make(chan bool)
 | 
							ch := make(chan bool)
 | 
				
			||||||
		d := debouncer.New(ch)
 | 
							d := debouncer.New(ch, 200*time.Millisecond)
 | 
				
			||||||
		go func() {
 | 
							go func() {
 | 
				
			||||||
			ch <- true
 | 
								ch <- true
 | 
				
			||||||
			ch <- true
 | 
								ch <- true
 | 
				
			||||||
@@ -40,7 +40,7 @@ func TestDebouncer(t *testing.T) {
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
	t.Run("Debounce", func(t *testing.T) {
 | 
						t.Run("Debounce", func(t *testing.T) {
 | 
				
			||||||
		ch := make(chan bool)
 | 
							ch := make(chan bool)
 | 
				
			||||||
		d := debouncer.New(ch)
 | 
							d := debouncer.New(ch, 200*time.Millisecond)
 | 
				
			||||||
		go func() {
 | 
							go func() {
 | 
				
			||||||
			ch <- true
 | 
								ch <- true
 | 
				
			||||||
			time.Sleep(10 * time.Millisecond)
 | 
								time.Sleep(10 * time.Millisecond)
 | 
				
			||||||
@@ -65,7 +65,7 @@ func TestDebouncer(t *testing.T) {
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
	t.Run("DebounceDelay", func(t *testing.T) {
 | 
						t.Run("DebounceDelay", func(t *testing.T) {
 | 
				
			||||||
		ch := make(chan bool)
 | 
							ch := make(chan bool)
 | 
				
			||||||
		d := debouncer.New(ch)
 | 
							d := debouncer.New(ch, 200*time.Millisecond)
 | 
				
			||||||
		go func() {
 | 
							go func() {
 | 
				
			||||||
			ch <- true
 | 
								ch <- true
 | 
				
			||||||
			ch <- false
 | 
								ch <- false
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.go
									
									
									
									
									
								
							@@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/signal"
 | 
						"os/signal"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.t-juice.club/torjus/ghettoptt/bus"
 | 
						"git.t-juice.club/torjus/ghettoptt/bus"
 | 
				
			||||||
	"git.t-juice.club/torjus/ghettoptt/debouncer"
 | 
						"git.t-juice.club/torjus/ghettoptt/debouncer"
 | 
				
			||||||
@@ -15,7 +16,7 @@ import (
 | 
				
			|||||||
	"github.com/prometheus/client_golang/prometheus/promhttp"
 | 
						"github.com/prometheus/client_golang/prometheus/promhttp"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Version = "v0.1.3"
 | 
					const Version = "v0.1.4"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	// Setup logger
 | 
						// Setup logger
 | 
				
			||||||
@@ -73,7 +74,7 @@ func main() {
 | 
				
			|||||||
	doneCtx, doneCancel := context.WithCancel(srvCtx)
 | 
						doneCtx, doneCancel := context.WithCancel(srvCtx)
 | 
				
			||||||
	defer doneCancel()
 | 
						defer doneCancel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	debouncer := debouncer.New(eventCh)
 | 
						debouncer := debouncer.New(eventCh, 400*time.Millisecond)
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		var done bool
 | 
							var done bool
 | 
				
			||||||
		for !done {
 | 
							for !done {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user