class Celluloid::Receivers - celluloid-0.16.0 Documentation

# class Celluloid::Receivers

Allow methods to directly interact with the actor protocol

### Public Class Methods

new(timers) click to toggle source

    # File lib/celluloid/receivers.rb, line 8
    def initialize(timers)
      @receivers = Set.new
      @timers = timers
    end

### Public Instance Methods

handle_message(message) click to toggle source

Handle incoming messages

    # File lib/celluloid/receivers.rb, line 33
    def handle_message(message)
      receiver = @receivers.find { |r| r.match(message) }
      return unless receiver

      @receivers.delete receiver
      receiver.timer.cancel if receiver.timer
      receiver.resume message
      message
    end

receive(timeout = nil, &block) click to toggle source

Receive an asynchronous message

    # File lib/celluloid/receivers.rb, line 14
    def receive(timeout = nil, &block)
      if Celluloid.exclusive?
        Celluloid.mailbox.receive(timeout, &block)
      else
        receiver = Receiver.new block

        if timeout
          receiver.timer = @timers.after(timeout) do
            @receivers.delete receiver
            receiver.resume
          end
        end

        @receivers << receiver
        Task.suspend :receiving
      end
    end