Files
nostrlib/subscription.go
fiatjaf 69ccfbaa08 protect against faulty relays that send more than one EOSE halting us
using sync.Once{} to only emit to the EndOfStoredEvents channel once
(it has capacity 1 so anything over that would halt).
2022-11-16 10:07:37 -03:00

41 lines
654 B
Go

package nostr
import "sync"
type Subscription struct {
id string
conn *Connection
filters Filters
Events chan Event
EndOfStoredEvents chan struct{}
stopped bool
emitEose sync.Once
}
type EventMessage struct {
Event Event
Relay string
}
func (sub Subscription) Unsub() {
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
sub.stopped = true
if sub.Events != nil {
close(sub.Events)
}
}
func (sub *Subscription) Sub(filters Filters) {
sub.filters = filters
message := []interface{}{"REQ", sub.id}
for _, filter := range sub.filters {
message = append(message, filter)
}
sub.conn.WriteJSON(message)
}