Sha256: e9a8477a0bf431077cf6abaa54a343e49be3f9c048f64f4ef21528f95282c879

Contents?: true

Size: 937 Bytes

Versions: 5

Compression:

Stored size: 937 Bytes

Contents

# frozen_string_literal: true

module Bitcoin
  module Wallet
    class Publisher < Concurrent::Actor::RestartingContext
      attr_reader :receivers

      def initialize
        @receivers = {}
      end

      def on_message(message)
        case message
        when :unsubscribe
          receivers.each { |receiver| receiver.delete(envelope.sender) }
        when Array
          if message[0] == :subscribe
            if envelope.sender.is_a? Concurrent::Actor::Reference
              receivers[message[1].name] ||= []
              receivers[message[1].name] << envelope.sender
            end
          elsif message[0] == :subscribe?
            receivers[message[1].name]&.include?(envelope.sender)
          elsif message[0] == :unsubscribe
            receivers.delete(message[1].name)
          end
        else
          receivers[message&.class&.name]&.each { |r| r << message }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bitcoinrb-grpc-0.1.9 lib/bitcoin/wallet/publisher.rb
bitcoinrb-grpc-0.1.8 lib/bitcoin/wallet/publisher.rb
bitcoinrb-grpc-0.1.7 lib/bitcoin/wallet/publisher.rb
bitcoinrb-grpc-0.1.6 lib/bitcoin/wallet/publisher.rb
bitcoinrb-grpc-0.1.5 lib/bitcoin/wallet/publisher.rb