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