Class: MessagePack::RPC::MultiFuture
- Inherits:
-
Object
- Object
- MessagePack::RPC::MultiFuture
- Defined in:
- lib/msgpack/rpc/multi_future.rb
Overview
MultiFuture bunldes up multiple Future objects.
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Gets all registered Future objects.
-
#error ⇒ Object
readonly
Gets Future objects which are joined as error.
-
#joined ⇒ Object
readonly
Gets Future objects which are already joined.
-
#not_joined ⇒ Object
readonly
Gets Future objects which are not joined yet.
-
#success ⇒ Object
readonly
Gets Future objects which are joined as success.
Instance Method Summary collapse
-
#add(future) ⇒ Object
Registeres new Future object.
-
#clear ⇒ Object
Clears all Future objects and all callback methods.
-
#clear_callback ⇒ Object
Clears all callback methods registered by on_xxx methods.
-
#initialize ⇒ MultiFuture
constructor
A new instance of MultiFuture.
-
#join_all ⇒ Object
Waits until all Future objects join.
-
#join_error ⇒ Object
Waits until at least one Future object joins as error.
-
#join_num(n) ⇒ Object
Waits until specified number of Future objects join.
-
#join_success ⇒ Object
Waits until at least one Future object joins as success.
-
#on_all(&block) ⇒ Object
Attaches a callback method that is called when all Future objects are joined.
-
#on_error(&block) ⇒ Object
Attaches a callback method that is called when Future objects are joined as error.
-
#on_num(n, &block) ⇒ Object
Attaches a callback method that is called when specified number of Future objects are joined.
-
#on_success(&block) ⇒ Object
Attaches a callback method that is called when Future objects are joined as success.
Constructor Details
#initialize ⇒ MultiFuture
Returns a new instance of MultiFuture.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/msgpack/rpc/multi_future.rb', line 24 def initialize @all = [] @not_joined = [] @joined = [] @error = [] @success = [] @on_all = nil @on_error = nil @on_success = nil @on_num = {} # {num => callback} end |
Instance Attribute Details
#all ⇒ Object (readonly)
Gets all registered Future objects.
39 40 41 |
# File 'lib/msgpack/rpc/multi_future.rb', line 39 def all @all end |
#error ⇒ Object (readonly)
Gets Future objects which are joined as error.
48 49 50 |
# File 'lib/msgpack/rpc/multi_future.rb', line 48 def error @error end |
#joined ⇒ Object (readonly)
Gets Future objects which are already joined.
45 46 47 |
# File 'lib/msgpack/rpc/multi_future.rb', line 45 def joined @joined end |
#not_joined ⇒ Object (readonly)
Gets Future objects which are not joined yet.
42 43 44 |
# File 'lib/msgpack/rpc/multi_future.rb', line 42 def not_joined @not_joined end |
#success ⇒ Object (readonly)
Gets Future objects which are joined as success.
51 52 53 |
# File 'lib/msgpack/rpc/multi_future.rb', line 51 def success @success end |
Instance Method Details
#add(future) ⇒ Object
Registeres new Future object. Returns self.
76 77 78 79 80 81 |
# File 'lib/msgpack/rpc/multi_future.rb', line 76 def add(future) future.attach_callback(&method(:callback)) @all << future @not_joined << future self end |
#clear ⇒ Object
Clears all Future objects and all callback methods.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/msgpack/rpc/multi_future.rb', line 54 def clear @all = [] @not_joined = [] @joined = [] @error = [] @success = [] clear_callback end |
#clear_callback ⇒ Object
Clears all callback methods registered by on_xxx methods.
66 67 68 69 70 71 72 |
# File 'lib/msgpack/rpc/multi_future.rb', line 66 def clear_callback @on_all = nil @on_error = nil @on_success = nil @on_num = {} self end |
#join_all ⇒ Object
Waits until all Future objects join.
116 117 118 119 120 121 |
# File 'lib/msgpack/rpc/multi_future.rb', line 116 def join_all @not_joined.each {|future| future.join } @all end |
#join_error ⇒ Object
Waits until at least one Future object joins as error. Returns the joined Future object or nil.
137 138 139 140 141 142 143 144 145 |
# File 'lib/msgpack/rpc/multi_future.rb', line 137 def join_error until @not_joined.empty? unless @error.empty? break end @not_joined.first.loop.run_once end @error.last end |
#join_num(n) ⇒ Object
Waits until specified number of Future objects join. Returns the joined Future objects or nil.
149 150 151 152 153 154 155 156 157 |
# File 'lib/msgpack/rpc/multi_future.rb', line 149 def join_num(n) until @not_joined.empty? unless @joined.size >= n return @joined end @not_joined.first.loop.run_once end nil end |
#join_success ⇒ Object
Waits until at least one Future object joins as success. Returns the joined Future object or nil.
125 126 127 128 129 130 131 132 133 |
# File 'lib/msgpack/rpc/multi_future.rb', line 125 def join_success until @not_joined.empty? unless @success.empty? break end @not_joined.first.loop.run_once end @success.last end |
#on_all(&block) ⇒ Object
Attaches a callback method that is called when all Future objects are joined. Returns self.
86 87 88 89 |
# File 'lib/msgpack/rpc/multi_future.rb', line 86 def on_all(&block) @on_all = block self end |
#on_error(&block) ⇒ Object
Attaches a callback method that is called when Future objects are joined as error. Returns self.
102 103 104 105 |
# File 'lib/msgpack/rpc/multi_future.rb', line 102 def on_error(&block) @on_error = block self end |
#on_num(n, &block) ⇒ Object
Attaches a callback method that is called when specified number of Future objects are joined. Returns self.
110 111 112 113 |
# File 'lib/msgpack/rpc/multi_future.rb', line 110 def on_num(n, &block) @on_num[n.to_i] = block self end |
#on_success(&block) ⇒ Object
Attaches a callback method that is called when Future objects are joined as success. Returns self.
94 95 96 97 |
# File 'lib/msgpack/rpc/multi_future.rb', line 94 def on_success(&block) @on_success = block self end |