2017-12-27 15:33:42 -05:00
package signal
2018-02-11 17:28:42 -05:00
// Notifier is an utility for notifying changes. The change producer may notify changes multiple time, and the consumer may get notified asychronously.
2017-12-27 15:33:42 -05:00
type Notifier struct {
2018-02-08 09:39:46 -05:00
c chan struct { }
2017-12-27 15:33:42 -05:00
}
2018-02-11 17:28:42 -05:00
// NewNotifier creates a new Notifier.
2017-12-27 15:33:42 -05:00
func NewNotifier ( ) * Notifier {
return & Notifier {
2018-02-08 09:39:46 -05:00
c : make ( chan struct { } , 1 ) ,
2017-12-27 15:33:42 -05:00
}
}
2018-02-11 17:28:42 -05:00
// Signal signals a change, usually by producer. This method never blocks.
2017-12-27 15:33:42 -05:00
func ( n * Notifier ) Signal ( ) {
select {
2018-02-08 09:39:46 -05:00
case n . c <- struct { } { } :
2017-12-27 15:33:42 -05:00
default :
}
}
2018-02-11 17:28:42 -05:00
// Wait returns a channel for waiting for changes. The returned channel never gets closed.
2018-02-08 09:39:46 -05:00
func ( n * Notifier ) Wait ( ) <- chan struct { } {
2017-12-27 15:33:42 -05:00
return n . c
}