notifier.go 640 B

1234567891011121314151617181920212223242526
  1. package signal
  2. // Notifier is a utility for notifying changes. The change producer may notify changes multiple time, and the consumer may get notified asynchronously.
  3. type Notifier struct {
  4. c chan struct{}
  5. }
  6. // NewNotifier creates a new Notifier.
  7. func NewNotifier() *Notifier {
  8. return &Notifier{
  9. c: make(chan struct{}, 1),
  10. }
  11. }
  12. // Signal signals a change, usually by producer. This method never blocks.
  13. func (n *Notifier) Signal() {
  14. select {
  15. case n.c <- struct{}{}:
  16. default:
  17. }
  18. }
  19. // Wait returns a channel for waiting for changes. The returned channel never gets closed.
  20. func (n *Notifier) Wait() <-chan struct{} {
  21. return n.c
  22. }