github.com/metacubex/mihomo@v1.18.5/common/observable/subscriber.go (about)

     1  package observable
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  type Subscription[T any] <-chan T
     8  
     9  type Subscriber[T any] struct {
    10  	buffer chan T
    11  	once   sync.Once
    12  }
    13  
    14  func (s *Subscriber[T]) Emit(item T) {
    15  	s.buffer <- item
    16  }
    17  
    18  func (s *Subscriber[T]) Out() Subscription[T] {
    19  	return s.buffer
    20  }
    21  
    22  func (s *Subscriber[T]) Close() {
    23  	s.once.Do(func() {
    24  		close(s.buffer)
    25  	})
    26  }
    27  
    28  func newSubscriber[T any]() *Subscriber[T] {
    29  	sub := &Subscriber[T]{
    30  		buffer: make(chan T, 200),
    31  	}
    32  	return sub
    33  }