package pubsub

import (
	
	
)

// Subscription handles the details of a particular Topic subscription.
// There may be many subscriptions for a given Topic.
type Subscription struct {
	topic    string
	ch       chan *Message
	cancelCh chan<- *Subscription
	ctx      context.Context
	err      error
	once     sync.Once
}

// Topic returns the topic string associated with the Subscription
func ( *Subscription) () string {
	return .topic
}

// Next returns the next message in our subscription
func ( *Subscription) ( context.Context) (*Message, error) {
	select {
	case ,  := <-.ch:
		if ! {
			return , .err
		}

		return , nil
	case <-.Done():
		return nil, .Err()
	}
}

// Cancel closes the subscription. If this is the last active subscription then pubsub will send an unsubscribe
// announcement to the network.
func ( *Subscription) () {
	select {
	case .cancelCh <- :
	case <-.ctx.Done():
	}
}

func ( *Subscription) () {
	.once.Do(func() {
		close(.ch)
	})
}