Skip to content

Instantly share code, notes, and snippets.

@AndreKR
Created Jun 4, 2017
Embed
What would you like to do?
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