Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send RelayEvent Containing NostrEvent and Subscription ID #86

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Send RelayEvent Containing NostrEvent and Subscription ID
  • Loading branch information
joelklabo committed Aug 25, 2023
commit a28ce665e87de2666ba949641c57ca8be1a0e2b6
19 changes: 13 additions & 6 deletions Sources/NostrSDK/Relay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public enum RelayRequestError: Error, CustomStringConvertible {
/// An optional interface for receiving state updates and events
public protocol RelayDelegate: AnyObject {
func relayStateDidChange(_ relay: Relay, state: Relay.State)
func relay(_ relay: Relay, didReceive event: NostrEvent)
func relay(_ relay: Relay, didReceive event: RelayEvent)
}

/// A struct containing a nostr event and the subscription ID
public struct RelayEvent {
let event: NostrEvent
let subscriptionId: String
}

/// An object that communicates with a relay.
Expand Down Expand Up @@ -89,8 +95,8 @@ public final class Relay: ObservableObject, EventVerifying {
private var socketSubscription: AnyCancellable?

/// A Publisher that publishes all events the relay receives.
public private(set) var events = PassthroughSubject<NostrEvent, Never>()
public private(set) var events = PassthroughSubject<RelayEvent, Never>()

/// An optional delegate interface for receiving state updates and events
public weak var delegate: RelayDelegate?

Expand Down Expand Up @@ -133,9 +139,10 @@ public final class Relay: ObservableObject, EventVerifying {
func handle(messageData: Data) {
let response = RelayResponse.decode(data: messageData)
switch response {
case .event(_, let event):
events.send(event)
delegate?.relay(self, didReceive: event)
case .event(let subscriptionId, let event):
let relayEvent = RelayEvent(event: event, subscriptionId: subscriptionId)
events.send(relayEvent)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the publisher case, maybe a tuple would be better...

delegate?.relay(self, didReceive: relayEvent)
default:
break
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/NostrSDKTests/RelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension RelayTests: RelayDelegate {
}
}

func relay(_ relay: Relay, didReceive event: NostrEvent) {
func relay(_ relay: Relay, didReceive event: RelayEvent) {
receiveExpectation?.fulfill()
}
}
Loading