wait.go 507 B

123456789101112131415161718192021222324252627282930313233343536
  1. package wait
  2. import (
  3. "sync"
  4. )
  5. type List struct {
  6. l sync.Mutex
  7. m map[int64]chan interface{}
  8. }
  9. func New() *List {
  10. return &List{m: make(map[int64]chan interface{})}
  11. }
  12. func (w *List) Register(id int64) <-chan interface{} {
  13. w.l.Lock()
  14. defer w.l.Unlock()
  15. ch := w.m[id]
  16. if ch == nil {
  17. ch = make(chan interface{}, 1)
  18. w.m[id] = ch
  19. }
  20. return ch
  21. }
  22. func (w *List) Trigger(id int64, x interface{}) {
  23. w.l.Lock()
  24. ch := w.m[id]
  25. delete(w.m, id)
  26. w.l.Unlock()
  27. if ch != nil {
  28. ch <- x
  29. close(ch)
  30. }
  31. }