Class: Tribe::Mailbox

Inherits:
Object
  • Object
show all
Defined in:
lib/tribe/mailbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mailbox

Returns a new instance of Mailbox.



3
4
5
6
# File 'lib/tribe/mailbox.rb', line 3

def initialize(options = {})
  @messages = []
  @mutex = Mutex.new
end

Instance Method Details

#push(event, &block) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/tribe/mailbox.rb', line 8

def push(event, &block)
  @mutex.synchronize do
    @messages.push(event)
    block.call unless @current_thread
  end

  return nil
end

#release(&block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/tribe/mailbox.rb', line 27

def release(&block)
  @mutex.synchronize do
    @current_thread = nil
    block.call if block && @messages.length > 0
  end

  return nil
end

#shiftObject



17
18
19
20
21
22
23
24
25
# File 'lib/tribe/mailbox.rb', line 17

def shift
  @mutex.synchronize do
    return nil if @current_thread && @current_thread != Thread.current

    @current_thread = Thread.current unless @current_thread

    return @messages.shift
  end
end

#synchronize(&block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/tribe/mailbox.rb', line 36

def synchronize(&block)
  @mutex.synchronize do
    block.call
  end

  return nil
end