Class: Celluloid::Internals::Receivers

Inherits:
Object
  • Object
show all
Defined in:
lib/celluloid/internals/receivers.rb

Overview

Allow methods to directly interact with the actor protocol

Instance Method Summary collapse

Constructor Details

#initialize(timers) ⇒ Receivers

Returns a new instance of Receivers.



8
9
10
11
# File 'lib/celluloid/internals/receivers.rb', line 8

def initialize(timers)
  @receivers = Set.new
  @timers = timers
end

Instance Method Details

#handle_message(message) ⇒ Object

Handle incoming messages



33
34
35
36
37
38
39
40
41
# File 'lib/celluloid/internals/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) ⇒ Object

Receive an asynchronous message



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/celluloid/internals/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