mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-12-09 12:21:04 +01:00
support handling CLOSED messages from relay client.
This commit is contained in:
@@ -26,11 +26,15 @@ type Subscription struct {
|
||||
// the EndOfStoredEvents channel gets closed when an EOSE comes for that subscription
|
||||
EndOfStoredEvents chan struct{}
|
||||
|
||||
// the ClosedReason channel emits the reason when a CLOSED message is received
|
||||
ClosedReason chan string
|
||||
|
||||
// Context will be .Done() when the subscription ends
|
||||
Context context.Context
|
||||
|
||||
live atomic.Bool
|
||||
eosed atomic.Bool
|
||||
closed atomic.Bool
|
||||
cancel context.CancelFunc
|
||||
|
||||
// this keeps track of the events we've received before the EOSE that we must dispatch before
|
||||
@@ -107,6 +111,13 @@ func (sub *Subscription) dispatchEose() {
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *Subscription) dispatchClosed(reason string) {
|
||||
if sub.closed.CompareAndSwap(false, true) {
|
||||
sub.ClosedReason <- reason
|
||||
close(sub.ClosedReason)
|
||||
}
|
||||
}
|
||||
|
||||
// Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01.
|
||||
// Unsub() also closes the channel sub.Events and makes a new one.
|
||||
func (sub *Subscription) Unsub() {
|
||||
|
||||
Reference in New Issue
Block a user