mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
problem: can't unsub
This commit is contained in:
parent
72ef03f238
commit
6f1c60f66c
@ -24,7 +24,7 @@ for notice := range pool.Notices {
|
||||
### Listening for events
|
||||
|
||||
```go
|
||||
subId, events := pool.Sub(nostr.Filters{
|
||||
subId, events, unsub := pool.Sub(nostr.Filters{
|
||||
{
|
||||
Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"},
|
||||
Kinds: []int{nostr.KindTextNote}, // or {1}
|
||||
@ -38,7 +38,7 @@ go func() {
|
||||
}()
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
sub.Unsub()
|
||||
close(unsub)
|
||||
```
|
||||
|
||||
### Publishing an event
|
||||
@ -84,4 +84,4 @@ fmt.Println("pk:", nostr.GetPublicKey(sk))
|
||||
|
||||
```
|
||||
go run example/example.go
|
||||
```
|
||||
```
|
12
relaypool.go
12
relaypool.go
@ -124,7 +124,7 @@ func (r *RelayPool) Remove(url string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage) {
|
||||
func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, chan struct{}) {
|
||||
random := make([]byte, 7)
|
||||
rand.Read(random)
|
||||
id := hex.EncodeToString(random)
|
||||
@ -132,6 +132,7 @@ func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage) {
|
||||
r.subscriptions.Store(id, filters)
|
||||
eventStream := make(chan EventMessage)
|
||||
r.eventStreams.Store(id, eventStream)
|
||||
unsub := make(chan struct{})
|
||||
|
||||
r.Relays.Range(func(_ string, relay *Relay) bool {
|
||||
sub := relay.prepareSubscription(id)
|
||||
@ -143,10 +144,17 @@ func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage) {
|
||||
}
|
||||
}(sub)
|
||||
|
||||
go func() {
|
||||
select {
|
||||
case <-unsub:
|
||||
sub.Unsub()
|
||||
}
|
||||
}()
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
return id, eventStream
|
||||
return id, eventStream, unsub
|
||||
}
|
||||
|
||||
func Unique(all chan EventMessage) chan Event {
|
||||
|
Loading…
x
Reference in New Issue
Block a user