Skip to content

Instantly share code, notes, and snippets.

@AndreKR
Created June 4, 2017 18:12
Show Gist options
  • Save AndreKR/d6370d3fc8dcfd41ad58dcd655cad533 to your computer and use it in GitHub Desktop.
Save AndreKR/d6370d3fc8dcfd41ad58dcd655cad533 to your computer and use it in GitHub Desktop.
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