Sha256: 42253fdde710ed9e28830a686810b0e8b48d40567a40fbdbee316049ed24ad98

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

module Mutant
  # A minimal actor implementation
  module Actor

    # Error raised when actor signalling protocol is violated
    class ProtocolError < RuntimeError
    end # ProtocolError

    # Undefined message payload
    Undefined = Class.new do
      INSPECT = 'Mutant::Actor::Undefined'.freeze

      # Object inspection
      #
      # @return [String]
      #
      # @api private
      def inspect
        INSPECT
      end
    end.new

    # Message being exchanged between actors
    class Message
      include Concord::Public.new(:type, :payload)

      # New message
      #
      # @param [Symbol] type
      # @param [Object] payload
      #
      # @return [Message]
      #
      # @api private
      def self.new(_type, _payload = Undefined)
        super
      end

    end # Message

    # Binding to othersactors sender for simple RPC
    class Binding
      include Concord.new(:mailbox, :other)

      # Send message and wait for reply
      #
      # @param [Symbol] type
      #
      # @return [Object]
      #
      # @api private
      def call(type)
        other.call(Message.new(type, mailbox.sender))
        message = mailbox.receiver.call
        fail ProtocolError, "Expected #{type} but got #{message.type}" unless type.equal?(message.type)
        message.payload
      end

    end # Binding
  end # Actor
end # Mutant

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mutant-0.8.8 lib/mutant/actor.rb
mutant-0.8.7 lib/mutant/actor.rb
mutant-0.8.6 lib/mutant/actor.rb
mutant-0.8.5 lib/mutant/actor.rb
mutant-0.8.4 lib/mutant/actor.rb
mutant-0.8.3 lib/mutant/actor.rb
mutant-0.8.2 lib/mutant/actor.rb
mutant-0.8.1 lib/mutant/actor.rb