go-waku/waku/v2/protocol/relay/subscription.go

33 lines
832 B
Go
Raw Normal View History

2021-11-01 10:42:55 -04:00
package relay
2021-04-14 22:17:12 -04:00
2023-05-05 15:19:15 +05:30
import "github.com/waku-org/go-waku/waku/v2/protocol"
2021-04-14 22:17:12 -04:00
// Subscription handles the details of a particular Topic subscription. There may be many subscriptions for a given topic.
2021-04-14 22:17:12 -04:00
type Subscription struct {
2023-05-05 15:19:15 +05:30
Unsubscribe func()
Ch <-chan *protocol.Envelope
2021-04-14 22:17:12 -04:00
}
// NoopSubscription creates a noop subscription that will not receive any envelope
2023-05-05 15:19:15 +05:30
func NoopSubscription() Subscription {
ch := make(chan *protocol.Envelope)
close(ch)
return Subscription{
Unsubscribe: func() {},
Ch: ch,
}
2021-04-14 22:17:12 -04:00
}
// ArraySubscription creates a subscription for a list of envelopes
2023-05-05 15:19:15 +05:30
func ArraySubscription(msgs []*protocol.Envelope) Subscription {
ch := make(chan *protocol.Envelope, len(msgs))
2023-05-05 17:33:44 +05:30
for _, msg := range msgs {
ch <- msg
}
2023-05-05 15:19:15 +05:30
close(ch)
return Subscription{
Unsubscribe: func() {},
Ch: ch,
}
2021-04-14 22:17:12 -04:00
}