1
0
mirror of https://github.com/Elegycloud/clash-backup.git synced 2024-12-23 01:27:24 +08:00
clash/observable/subscriber.go

36 lines
522 B
Go
Raw Permalink Normal View History

package observable
import (
"sync"
"gopkg.in/eapache/channels.v1"
)
type Subscription <-chan interface{}
type Subscriber struct {
buffer *channels.InfiniteChannel
once sync.Once
}
func (s *Subscriber) Emit(item interface{}) {
s.buffer.In() <- item
}
func (s *Subscriber) Out() Subscription {
return s.buffer.Out()
}
func (s *Subscriber) Close() {
s.once.Do(func() {
s.buffer.Close()
})
}
func newSubscriber() *Subscriber {
sub := &Subscriber{
buffer: channels.NewInfiniteChannel(),
}
return sub
}