mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-27 12:35:39 +08:00
34 lines
466 B
Go
34 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
|
|
}
|