From a0cf41bfb44dea536ca657757b9d6d7ed80cdb26 Mon Sep 17 00:00:00 2001 From: Wilson Silva Date: Fri, 5 Apr 2024 15:31:49 +0100 Subject: [PATCH] Expose the relay in the client connection event --- .rubocop_todo.yml | 2 +- README.md | 2 +- docs/core/client.md | 2 +- lib/nostr/client.rb | 18 ++++++++++++++---- sig/nostr/client.rbs | 1 + sig/vendor/event_emitter.rbs | 18 ++++++++---------- spec/nostr/client_spec.rb | 8 ++++---- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f9d5a9f..92cd352 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -9,7 +9,7 @@ # Offense count: 2 # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes. Metrics/AbcSize: - Max: 23 + Max: 24 # Offense count: 2 # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods. diff --git a/README.md b/README.md index 1519e0e..71a2233 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ relay = Nostr::Relay.new(url: 'wss://nostr.wine', name: 'Wine') client.connect(relay) # Listen asynchronously for the connect event -client.on :connect do +client.on :connect do |relay| # Send the event to the Relay client.publish(text_note_event) diff --git a/docs/core/client.md b/docs/core/client.md index dacfb91..8228f1e 100644 --- a/docs/core/client.md +++ b/docs/core/client.md @@ -28,7 +28,7 @@ The `:connect` event is fired when a connection with a WebSocket is opened. You client = Nostr::Client.new 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 end diff --git a/lib/nostr/client.rb b/lib/nostr/client.rb index d5aaa3e..5b5bafb 100644 --- a/lib/nostr/client.rb +++ b/lib/nostr/client.rb @@ -40,11 +40,11 @@ module Nostr # def connect(relay) execute_within_an_em_thread do - client = Faye::WebSocket::Client.new(relay.url, [], { tls: { verify_peer: false } }) - parent_to_child_channel.subscribe { |msg| client.send(msg) } + client = build_websocket_client(relay.url) + parent_to_child_channel.subscribe { |msg| client.send(msg) && emit(:send, msg) } client.on :open do - child_to_parent_channel.push(type: :open) + child_to_parent_channel.push(type: :open, relay:) end client.on :message do |event| @@ -167,11 +167,21 @@ module Nostr @child_to_parent_channel = EventMachine::Channel.new 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 :error, msg[:message] if msg[:type] == :error emit :close, msg[:code], msg[:reason] if msg[:type] == :close 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 diff --git a/sig/nostr/client.rbs b/sig/nostr/client.rbs index fdf1bb2..c2dc656 100644 --- a/sig/nostr/client.rbs +++ b/sig/nostr/client.rbs @@ -16,5 +16,6 @@ module Nostr def execute_within_an_em_thread: { -> void } -> Thread def initialize_channels: -> void + def build_websocket_client: (String relay_name) -> Faye::WebSocket::Client end end diff --git a/sig/vendor/event_emitter.rbs b/sig/vendor/event_emitter.rbs index a7ca4b9..7b6667f 100644 --- a/sig/vendor/event_emitter.rbs +++ b/sig/vendor/event_emitter.rbs @@ -1,16 +1,14 @@ # Added only to satisfy the Steep requirements. Not 100% reliable. module EventEmitter - interface _Event - def data: -> String - def message: -> String - def code: -> Integer - def reason: -> String - end + def self.included: (Module) -> void + def self.apply: (untyped) -> void - 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 once add_listener - def remove_listener: (untyped id_or_type) -> Array[untyped]? - def emit: (Symbol `type`, *untyped data) -> Array[untyped] - def once: (Symbol `type`) -> Integer + def remove_listener: (Integer | Symbol | String id_or_type) -> void + def emit: (Symbol | String type, *untyped data) -> void end diff --git a/spec/nostr/client_spec.rb b/spec/nostr/client_spec.rb index be41798..d5f64aa 100644 --- a/spec/nostr/client_spec.rb +++ b/spec/nostr/client_spec.rb @@ -52,16 +52,16 @@ RSpec.describe Nostr::Client do describe '#on' do context 'when the connection is opened' do it 'fires the :connect event' do - connect_event_fired = false + connected_relay = nil - client.on :connect do - connect_event_fired = true + client.on :connect do |relay| + connected_relay = relay end client.connect(relay) sleep 0.02 - expect(connect_event_fired).to be(true) + expect(connected_relay).to eq(relay) end end