From 4e09037e9f8667e1434ca1a42428da2eb8e1b271 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 17 Jun 2014 01:28:43 -0700 Subject: [PATCH] . --- blocks/account.go | 1 + blocks/adjustment.go | 1 + blocks/tx.go | 1 + common/debounce.go | 39 +++++++++++++++++++++++++++++++ blocks/util.go => common/panic.go | 2 +- 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 common/debounce.go rename blocks/util.go => common/panic.go (87%) diff --git a/blocks/account.go b/blocks/account.go index 8f1250849..0758d7b1d 100644 --- a/blocks/account.go +++ b/blocks/account.go @@ -1,6 +1,7 @@ package blocks import ( + . "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/binary" "io" ) diff --git a/blocks/adjustment.go b/blocks/adjustment.go index 070cf63be..548ee60c6 100644 --- a/blocks/adjustment.go +++ b/blocks/adjustment.go @@ -1,6 +1,7 @@ package blocks import ( + . "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/binary" "io" ) diff --git a/blocks/tx.go b/blocks/tx.go index ce940b9f3..4a982618a 100644 --- a/blocks/tx.go +++ b/blocks/tx.go @@ -1,6 +1,7 @@ package blocks import ( + . "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/binary" "io" ) diff --git a/common/debounce.go b/common/debounce.go new file mode 100644 index 000000000..6f5755243 --- /dev/null +++ b/common/debounce.go @@ -0,0 +1,39 @@ +package common + +import ( + "time" +) + +/* Debouncer */ +type Debouncer struct { + Ch chan struct{} + quit chan struct{} + dur time.Duration + timer *time.Timer +} + +func NewDebouncer(dur time.Duration) *Debouncer { + var timer *time.Timer + var ch = make(chan struct{}) + var quit = make(chan struct{}) + fire := func() { + go func() { + select { + case ch <- struct{}{}: + case <-quit: + } + }() + timer.Reset(dur) + } + timer = time.AfterFunc(dur, fire) + return &Debouncer{Ch:ch, dur:dur, quit:quit, timer:timer} +} + +func (d *Debouncer) Reset() { + d.timer.Reset(d.dur) +} + +func (d *Debouncer) Stop() bool { + close(d.quit) + return d.timer.Stop() +} diff --git a/blocks/util.go b/common/panic.go similarity index 87% rename from blocks/util.go rename to common/panic.go index 03fbd6287..b6cf4f328 100644 --- a/blocks/util.go +++ b/common/panic.go @@ -1,4 +1,4 @@ -package blocks +package common import ( "fmt"