// Subscribe returns a channel that is written to when an object is set to a new value.
Subscribe returns a channel that is written to when an object is set to a new value.
// Subscribe returns a channel that is written to when an object is set to a new value .
func (o *Object[T]) Subscribe(ctx context.Context, options ...Option) <-chan T {
	o.lock.Lock()
	defer o.lock.Unlock()
	p := params{}
	for _, opt := range options {
		p = opt(p)
	}
	s := o.state
	ch := make(chan T, p.bufferSize)
	go func() {
		defer close(ch)
		if p.withCurrent {
			select {
			case <-ctx.Done():
				return
			case ch <- s.value:
			}
		}
		for {
			select {
			case <-ctx.Done():
				return
			case <-s.done:
				s = s.next
			}
			select {
			case <-ctx.Done():
				return
			case ch <- s.value:
			}
		}
	}()
	return ch
}
func ( o * Object [ T ] ) Subscribe ( ctx context . Context , options ... Option ) <- chan T { o . lock . Lock ( ) defer o . lock . Unlock ( ) p := params { } for _ , opt := range options { p = opt ( p ) } s := o . state ch := make ( chan T , p . bufferSize ) go func ( ) { defer close ( ch ) if p . withCurrent { select { case <- ctx . Done ( ) : return case ch <- s . value : } } for { select { case <- ctx . Done ( ) : return case <- s . done : s = s . next } select { case <- ctx . Done ( ) : return case ch <- s . value : } } } ( ) return ch }
Subscribe returns a channel that is written to when an object is set to a new value.
Subscribe returns a channel that is written to when an object is set to a new value .