go-libp2p-pubsub/subscription.go

34 lines
496 B
Go
Raw Normal View History

2016-10-19 23:01:06 +00:00
package floodsub
2016-11-17 10:27:57 +00:00
import (
"context"
)
2016-10-19 23:01:06 +00:00
type Subscription struct {
topic string
ch chan *Message
cancelCh chan<- *Subscription
err error
}
func (sub *Subscription) Topic() string {
return sub.topic
}
2016-11-17 10:27:57 +00:00
func (sub *Subscription) Next(ctx context.Context) (*Message, error) {
select {
case msg, ok := <-sub.ch:
if !ok {
return msg, sub.err
}
2016-10-19 23:01:06 +00:00
2016-11-17 10:27:57 +00:00
return msg, nil
case <-ctx.Done():
return nil, ctx.Err()
2016-10-19 23:01:06 +00:00
}
}
func (sub *Subscription) Cancel() {
sub.cancelCh <- sub
}