Clash.Meta/common/observable/subscriber.go

34 lines
466 B
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package observable
import (
"sync"
)
type Subscription <-chan interface{}
type Subscriber struct {
2020-10-20 17:44:39 +08:00
buffer chan interface{}
2018-06-10 22:50:03 +08:00
once sync.Once
}
func (s *Subscriber) Emit(item interface{}) {
2020-10-20 17:44:39 +08:00
s.buffer <- item
2018-06-10 22:50:03 +08:00
}
func (s *Subscriber) Out() Subscription {
2020-10-20 17:44:39 +08:00
return s.buffer
2018-06-10 22:50:03 +08:00
}
func (s *Subscriber) Close() {
s.once.Do(func() {
2020-10-20 17:44:39 +08:00
close(s.buffer)
2018-06-10 22:50:03 +08:00
})
}
func newSubscriber() *Subscriber {
sub := &Subscriber{
2020-10-20 17:44:39 +08:00
buffer: make(chan interface{}, 200),
2018-06-10 22:50:03 +08:00
}
return sub
}