Created
June 4, 2017 18:12
-
-
Save AndreKR/d6370d3fc8dcfd41ad58dcd655cad533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Trigger ensures a worker runs once after it has been triggered, no matter how many trigger events happened during | |
// its last run. | |
// | |
// Usage: | |
// trigger := NewTrigger() | |
// go func() { | |
// for _ = range trigger { | |
// do_work() | |
// } | |
// } | |
// trigger.Fire() | |
type Trigger chan struct{} | |
func (t Trigger) Fire() { | |
// non-blocking channel write | |
select { | |
case t <- struct{}{}: | |
default: | |
} | |
} | |
func NewTrigger() Trigger { | |
var t Trigger | |
t = make(chan struct{}, 1) | |
return t | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment