Class: Keeper::Keeper
- Inherits:
-
Object
- Object
- Keeper::Keeper
- Defined in:
- lib/keeper.rb
Overview
A thread-safe blocking event pattern for your pleasure.
Instance Method Summary collapse
-
#fire(event) ⇒ Keeper
Fires the given event, waking up the waiting threads.
-
#initialize ⇒ Keeper
constructor
Create a new Keeper instance.
-
#wait_for(event) ⇒ Keeper
Waits for the given event to fire.
-
#waiting ⇒ Array<Symbol>
A list of all events currently being waited for.
Constructor Details
#initialize ⇒ Keeper
Create a new Keeper::Keeper instance.
34 35 36 37 |
# File 'lib/keeper.rb', line 34 def initialize @waiting = {} @mootex = Mutex.new end |
Instance Method Details
#fire(event) ⇒ Keeper
Fires the given event, waking up the waiting threads.
43 44 45 46 47 48 49 |
# File 'lib/keeper.rb', line 43 def fire(event) @mootex.synchronize do condition = @waiting.delete(event) condition.broadcast unless condition.nil? end self end |
#wait_for(event) ⇒ Keeper
Waits for the given event to fire.
55 56 57 58 59 60 |
# File 'lib/keeper.rb', line 55 def wait_for(event) @mootex.synchronize do (@waiting[event] ||= ConditionVariable.new).wait(@mootex) end self end |
#waiting ⇒ Array<Symbol>
A list of all events currently being waited for.
65 66 67 |
# File 'lib/keeper.rb', line 65 def waiting @mootex.synchronize { @waiting.keys } end |