Expose the relay in the client connection event

This commit is contained in:
Wilson Silva 2024-04-05 15:31:49 +01:00
parent 61eb0459d4
commit a0cf41bfb4
No known key found for this signature in database
GPG Key ID: 65135F94E23F82C8
7 changed files with 30 additions and 21 deletions

View File

@ -9,7 +9,7 @@
# Offense count: 2 # Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes. # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize: Metrics/AbcSize:
Max: 23 Max: 24
# Offense count: 2 # Offense count: 2
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods. # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.

View File

@ -80,7 +80,7 @@ relay = Nostr::Relay.new(url: 'wss://nostr.wine', name: 'Wine')
client.connect(relay) client.connect(relay)
# Listen asynchronously for the connect event # Listen asynchronously for the connect event
client.on :connect do client.on :connect do |relay|
# Send the event to the Relay # Send the event to the Relay
client.publish(text_note_event) client.publish(text_note_event)

View File

@ -28,7 +28,7 @@ The `:connect` event is fired when a connection with a WebSocket is opened. You
client = Nostr::Client.new client = Nostr::Client.new
relay = Nostr::Relay.new(url: 'wss://relay.damus.io', name: 'Damus') relay = Nostr::Relay.new(url: 'wss://relay.damus.io', name: 'Damus')
client.on :connect do client.on :connect do |relay|
# When this block executes, you're connected to the relay # When this block executes, you're connected to the relay
end end

View File

@ -40,11 +40,11 @@ module Nostr
# #
def connect(relay) def connect(relay)
execute_within_an_em_thread do execute_within_an_em_thread do
client = Faye::WebSocket::Client.new(relay.url, [], { tls: { verify_peer: false } }) client = build_websocket_client(relay.url)
parent_to_child_channel.subscribe { |msg| client.send(msg) } parent_to_child_channel.subscribe { |msg| client.send(msg) && emit(:send, msg) }
client.on :open do client.on :open do
child_to_parent_channel.push(type: :open) child_to_parent_channel.push(type: :open, relay:)
end end
client.on :message do |event| client.on :message do |event|
@ -167,11 +167,21 @@ module Nostr
@child_to_parent_channel = EventMachine::Channel.new @child_to_parent_channel = EventMachine::Channel.new
child_to_parent_channel.subscribe do |msg| child_to_parent_channel.subscribe do |msg|
emit :connect if msg[:type] == :open emit :connect, msg[:relay] if msg[:type] == :open
emit :message, msg[:data] if msg[:type] == :message emit :message, msg[:data] if msg[:type] == :message
emit :error, msg[:message] if msg[:type] == :error emit :error, msg[:message] if msg[:type] == :error
emit :close, msg[:code], msg[:reason] if msg[:type] == :close emit :close, msg[:code], msg[:reason] if msg[:type] == :close
end end
end end
# Builds a websocket client
#
# @api private
#
# @return [Faye::WebSocket::Client]
#
def build_websocket_client(relay_url)
Faye::WebSocket::Client.new(relay_url, [], { tls: { verify_peer: false } })
end
end end
end end

View File

@ -16,5 +16,6 @@ module Nostr
def execute_within_an_em_thread: { -> void } -> Thread def execute_within_an_em_thread: { -> void } -> Thread
def initialize_channels: -> void def initialize_channels: -> void
def build_websocket_client: (String relay_name) -> Faye::WebSocket::Client
end end
end end

View File

@ -1,16 +1,14 @@
# Added only to satisfy the Steep requirements. Not 100% reliable. # Added only to satisfy the Steep requirements. Not 100% reliable.
module EventEmitter module EventEmitter
interface _Event def self.included: (Module) -> void
def data: -> String def self.apply: (untyped) -> void
def message: -> String
def code: -> Integer
def reason: -> String
end
def add_listener: (Symbol event_name) { (_Event event) -> void } -> void def __events: () -> Array[untyped]
def add_listener: (Symbol | String type, ?Hash[untyped, untyped] params) { (*untyped) -> void } -> Integer
alias on add_listener alias on add_listener
alias once add_listener
def remove_listener: (untyped id_or_type) -> Array[untyped]? def remove_listener: (Integer | Symbol | String id_or_type) -> void
def emit: (Symbol `type`, *untyped data) -> Array[untyped] def emit: (Symbol | String type, *untyped data) -> void
def once: (Symbol `type`) -> Integer
end end

View File

@ -52,16 +52,16 @@ RSpec.describe Nostr::Client do
describe '#on' do describe '#on' do
context 'when the connection is opened' do context 'when the connection is opened' do
it 'fires the :connect event' do it 'fires the :connect event' do
connect_event_fired = false connected_relay = nil
client.on :connect do client.on :connect do |relay|
connect_event_fired = true connected_relay = relay
end end
client.connect(relay) client.connect(relay)
sleep 0.02 sleep 0.02
expect(connect_event_fired).to be(true) expect(connected_relay).to eq(relay)
end end
end end