mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-09-06 09:02:32 +00:00
33 lines
466 B
Go
33 lines
466 B
Go
package observable
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type Subscription <-chan interface{}
|
|
|
|
type Subscriber struct {
|
|
buffer chan interface{}
|
|
once sync.Once
|
|
}
|
|
|
|
func (s *Subscriber) Emit(item interface{}) {
|
|
s.buffer <- item
|
|
}
|
|
|
|
func (s *Subscriber) Out() Subscription {
|
|
return s.buffer
|
|
}
|
|
|
|
func (s *Subscriber) Close() {
|
|
s.once.Do(func() {
|
|
close(s.buffer)
|
|
})
|
|
}
|
|
|
|
func newSubscriber() *Subscriber {
|
|
sub := &Subscriber{
|
|
buffer: make(chan interface{}, 200),
|
|
}
|
|
return sub
|
|
}
|