mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-14 05:11:17 +08:00
34 lines
465 B
Go
34 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
|
|
}
|