Sha256: 8fff25c86ca4896000d7faf6705dda60637fff469133ceb04607f999f5e8ad5a

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 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

      # Return 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)

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

    end # Message

    # Bindin to others actors sender for simple RPC
    class Binding
      include Concord.new(:actor, :other)

      # Send message and wait for reply
      #
      # @param [Symbol] type
      #
      # @return [Object]
      #
      # @api private
      #
      def call(type)
        other.call(Message.new(type, actor.sender))
        message = actor.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

5 entries across 5 versions & 1 rubygems

Version Path
mutant-0.7.5 lib/mutant/actor.rb
mutant-0.7.4 lib/mutant/actor.rb
mutant-0.7.3 lib/mutant/actor.rb
mutant-0.7.2 lib/mutant/actor.rb
mutant-0.7.1 lib/mutant/actor.rb