2017-12-27 20:33:42 +00:00
package signal
2018-06-26 13:04:47 +00:00
import "sync"
2018-04-02 07:52:16 +00:00
// Notifier is a utility for notifying changes. The change producer may notify changes multiple time, and the consumer may get notified asynchronously.
2017-12-27 20:33:42 +00:00
type Notifier struct {
2018-06-26 13:04:47 +00:00
sync . Mutex
2018-06-26 13:28:59 +00:00
waiters [ ] chan struct { }
notCosumed bool
2017-12-27 20:33:42 +00:00
}
2018-02-11 22:28:42 +00:00
// NewNotifier creates a new Notifier.
2017-12-27 20:33:42 +00:00
func NewNotifier ( ) * Notifier {
2018-06-26 13:04:47 +00:00
return & Notifier { }
2017-12-27 20:33:42 +00:00
}
2018-02-11 22:28:42 +00:00
// Signal signals a change, usually by producer. This method never blocks.
2017-12-27 20:33:42 +00:00
func ( n * Notifier ) Signal ( ) {
2018-06-26 13:04:47 +00:00
n . Lock ( )
2018-06-26 13:28:59 +00:00
defer n . Unlock ( )
if len ( n . waiters ) == 0 {
n . notCosumed = true
return
}
2018-06-26 13:04:47 +00:00
for _ , w := range n . waiters {
close ( w )
2017-12-27 20:33:42 +00:00
}
2018-06-26 13:04:47 +00:00
n . waiters = make ( [ ] chan struct { } , 0 , 8 )
2017-12-27 20:33:42 +00:00
}
2018-06-26 13:28:59 +00:00
// Wait returns a channel for waiting for changes.
2018-02-08 14:39:46 +00:00
func ( n * Notifier ) Wait ( ) <- chan struct { } {
2018-06-26 13:04:47 +00:00
n . Lock ( )
defer n . Unlock ( )
w := make ( chan struct { } )
2018-06-26 13:28:59 +00:00
if n . notCosumed {
n . notCosumed = false
close ( w )
return w
}
2018-06-26 13:04:47 +00:00
n . waiters = append ( n . waiters , w )
return w
2017-12-27 20:33:42 +00:00
}