go-libp2p-pubsub/subscription.go
Steven Allen bd1a4630b9 move to go-libp2p-pubsub
Updates for move from go-floodsub to go-libp2p-pubsub.

fixes https://github.com/libp2p/go-libp2p-pubsub/issues/4
2018-10-18 21:10:16 +01:00

34 lines
494 B
Go

package pubsub
import (
"context"
)
type Subscription struct {
topic string
ch chan *Message
cancelCh chan<- *Subscription
err error
}
func (sub *Subscription) Topic() string {
return sub.topic
}
func (sub *Subscription) Next(ctx context.Context) (*Message, error) {
select {
case msg, ok := <-sub.ch:
if !ok {
return msg, sub.err
}
return msg, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (sub *Subscription) Cancel() {
sub.cancelCh <- sub
}