Files
qb/inbox/inbox.go

45 lines
702 B
Go
Raw Normal View History

2026-06-13 22:43:17 +00:00
package inbox
2026-06-14 23:12:03 +03:00
import (
"sync"
)
2026-06-13 22:43:17 +00:00
type Inbox struct {
mutex *sync.Mutex
signalCh chan struct{}
items []any
}
func New() *Inbox {
return &Inbox{
mutex: new(sync.Mutex),
signalCh: make(chan struct{}, 1),
}
}
func (s *Inbox) Ready() chan struct{} {
return s.signalCh
}
func (s *Inbox) Push(x any) {
2026-06-14 23:12:03 +03:00
//fmt.Printf("inbox.Push: %#v\n", x)
2026-06-13 22:43:17 +00:00
s.mutex.Lock()
s.items = append(s.items, x)
s.mutex.Unlock()
2026-06-14 23:12:03 +03:00
//fmt.Printf("inbox.Pushed\n")
2026-06-13 22:43:17 +00:00
select {
case s.signalCh <- struct{}{}:
default:
}
}
func (s *Inbox) Drain() []any {
2026-06-14 23:12:03 +03:00
//fmt.Printf("inbox.Drain\n")
2026-06-13 22:43:17 +00:00
s.mutex.Lock()
items := s.items
s.items = nil
s.mutex.Unlock()
2026-06-14 23:12:03 +03:00
//fmt.Printf("inbox.Drained: %#v\n", items)
2026-06-13 22:43:17 +00:00
return items
}