forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscription.go
More file actions
49 lines (39 loc) · 822 Bytes
/
subscription.go
File metadata and controls
49 lines (39 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package nostr
import (
"sync"
)
type Subscription struct {
id string
conn *Connection
mutex sync.Mutex
Relay *Relay
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.mutex.Lock()
defer sub.mutex.Unlock()
sub.conn.WriteJSON([]interface{}{"CLOSE", sub.id})
if sub.stopped == false && sub.Events != nil {
close(sub.Events)
}
sub.stopped = true
}
func (sub *Subscription) Sub(filters Filters) {
sub.Filters = filters
sub.Fire()
}
func (sub *Subscription) Fire() {
message := []interface{}{"REQ", sub.id}
for _, filter := range sub.Filters {
message = append(message, filter)
}
sub.conn.WriteJSON(message)
}