status-go/protocol/subscription/subscription.go
Pedro Pombeiro ed5a5c154d
Move to monorepo structure (#1684)
Move to a monorepo structure with submodules

- Rename status-protocol-go to status-go/protocol
2019-11-21 17:19:22 +01:00

56 lines
666 B
Go

package subscription
import (
"sync"
)
type Subscription struct {
sync.RWMutex
err error
done chan struct{}
}
func New() *Subscription {
return &Subscription{
done: make(chan struct{}),
}
}
func (s *Subscription) Cancel(err error) {
s.Lock()
defer s.Unlock()
if s.done == nil {
return
}
close(s.done)
s.done = nil
s.err = err
}
func (s *Subscription) Unsubscribe() {
s.Lock()
defer s.Unlock()
if s.done == nil {
return
}
close(s.done)
s.done = nil
}
func (s *Subscription) Err() error {
s.RLock()
defer s.RUnlock()
return s.err
}
func (s *Subscription) Done() <-chan struct{} {
s.RLock()
defer s.RUnlock()
return s.done
}