Class: Celluloid::Receivers

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

Overview

Allow methods to directly interact with the actor protocol

Instance Method Summary collapse

Constructor Details

#initializeReceivers

Returns a new instance of Receivers.



6
7
8
9
# File 'lib/celluloid/receivers.rb', line 6

def initialize
  @receivers = Set.new
  @timers = Timers.new
end

Instance Method Details

#fire_timersObject

Fire any pending timers



36
37
38
# File 'lib/celluloid/receivers.rb', line 36

def fire_timers
  @timers.fire
end

#handle_message(message) ⇒ Object

Handle incoming messages



41
42
43
44
45
46
47
48
# File 'lib/celluloid/receivers.rb', line 41

def handle_message(message)
  receiver = @receivers.find { |r| r.match(message) }
  return unless receiver

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

#receive(timeout = nil, &block) ⇒ Object

Receive an asynchronous message



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/celluloid/receivers.rb', line 12

def receive(timeout = nil, &block)
  if Celluloid.exclusive?
    Thread.mailbox.receive(timeout, &block)
  else
    receiver = Receiver.new block

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

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

#wait_intervalObject

How long to wait until the next timer fires



31
32
33
# File 'lib/celluloid/receivers.rb', line 31

def wait_interval
  @timers.wait_interval
end