logos-messaging-go/waku/v2/node/subscription.go

32 lines
581 B
Go
Raw Normal View History

2021-04-14 22:17:12 -04:00
package node
import (
"sync"
2021-04-21 20:09:37 -04:00
"github.com/status-im/go-waku/waku/v2/protocol"
2021-04-14 22:17:12 -04:00
)
2021-04-22 14:49:52 -04:00
// Subscription to a pubsub topic
2021-04-14 22:17:12 -04:00
type Subscription struct {
2021-04-22 14:49:52 -04:00
// Channel for receiving messages
C chan *protocol.Envelope
2021-04-14 22:17:12 -04:00
closed bool
mutex sync.Mutex
quit chan struct{}
}
2021-04-22 14:49:52 -04:00
// Unsubscribe from a pubsub topic. Will close the message channel
2021-04-14 22:17:12 -04:00
func (subs *Subscription) Unsubscribe() {
if !subs.closed {
close(subs.quit)
}
}
2021-04-22 14:49:52 -04:00
// Determine whether a Subscription is open or not
2021-04-14 22:17:12 -04:00
func (subs *Subscription) IsClosed() bool {
subs.mutex.Lock()
defer subs.mutex.Unlock()
return subs.closed
}