init and close quibbles.

This commit is contained in:
fiatjaf 2021-02-20 18:26:26 -03:00
parent b5e7e446fb
commit 9736da1a8c
2 changed files with 10 additions and 26 deletions

View File

@ -40,33 +40,12 @@ type NoticeMessage struct {
Relay string
}
func (nm *NoticeMessage) UnmarshalJSON(b []byte) error {
var temp []json.RawMessage
if err := json.Unmarshal(b, &temp); err != nil {
return err
}
if len(temp) < 2 {
return errors.New("message is not an array of 2 or more")
}
var tag string
if err := json.Unmarshal(temp[0], &tag); err != nil {
return err
}
if tag != "notice" {
return errors.New("tag is not 'notice'")
}
if err := json.Unmarshal(temp[1], &nm.Message); err != nil {
return err
}
return nil
}
// New creates a new RelayPool with no relays in it
func New() *RelayPool {
return &RelayPool{
Relays: make(map[string]Policy),
websockets: make(map[string]*websocket.Conn),
Relays: make(map[string]Policy),
websockets: make(map[string]*websocket.Conn),
subscriptions: make(map[string]*Subscription),
Notices: make(chan *NoticeMessage),
}

View File

@ -30,7 +30,12 @@ func (subscription Subscription) Unsub() {
})
}
close(subscription.Events)
if subscription.Events != nil {
close(subscription.Events)
}
if subscription.UniqueEvents != nil {
close(subscription.UniqueEvents)
}
}
func (subscription Subscription) Sub(filter *filter.EventFilter) {
@ -46,7 +51,7 @@ func (subscription Subscription) Sub(filter *filter.EventFilter) {
})
}
if subscription.started {
if !subscription.started {
go subscription.startHandlingUnique()
}
}