Class: EventMachine::Completion
- Inherits:
-
Object
- Object
- EventMachine::Completion
- Includes:
- Deferrable
- Defined in:
- lib/em/completion.rb
Constant Summary
Constants included from Deferrable
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#callback(*a, &b) ⇒ Object
Callbacks are called when you enter (or are in) a :succeeded state.
-
#cancel_callback(*a, &b) ⇒ Object
Remove a callback.
-
#cancel_errback(*a, &b) ⇒ Object
Remove an errback.
-
#cancel_timeout ⇒ Object
Disable the timeout.
-
#change_state(state, *args) ⇒ Object
(also: #set_deferred_status)
Enter a new state, setting the result value if given.
-
#completed? ⇒ Boolean
Indicates that we’ve reached some kind of completion state, by default this is :succeeded or :failed.
-
#completion(*a, &b) ⇒ Object
Completions are called when you enter (or are in) either a :failed or a :succeeded state.
-
#completion_states ⇒ Object
Completion states simply returns a list of completion states, by default this is :succeeded and :failed.
-
#errback(*a, &b) ⇒ Object
Errbacks are called when you enter (or are in) a :failed state.
-
#fail(*args) ⇒ Object
(also: #set_deferred_failure)
Enter the :failed state, setting the result value if given.
-
#initialize ⇒ Completion
constructor
A new instance of Completion.
-
#stateback(state, *a, &b) ⇒ Object
Statebacks are called when you enter (or are in) the named state.
-
#succeed(*args) ⇒ Object
(also: #set_deferred_success)
Enter the :succeeded state, setting the result value if given.
-
#timeout(time, *args) ⇒ Object
Schedule a time which if passes before we enter a completion state, this deferrable will be failed with the given arguments.
Methods included from Deferrable
Constructor Details
#initialize ⇒ Completion
Returns a new instance of Completion.
171 172 173 174 175 176 |
# File 'lib/em/completion.rb', line 171 def initialize @state = :unknown @callbacks = Hash.new { |h,k| h[k] = [] } @value = [] @timeout_timer = nil end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
169 170 171 |
# File 'lib/em/completion.rb', line 169 def state @state end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
169 170 171 |
# File 'lib/em/completion.rb', line 169 def value @value end |
Instance Method Details
#callback(*a, &b) ⇒ Object
Callbacks are called when you enter (or are in) a :succeeded state.
206 207 208 |
# File 'lib/em/completion.rb', line 206 def callback(*a, &b) stateback(:succeeded, *a, &b) end |
#cancel_callback(*a, &b) ⇒ Object
Remove a callback. N.B. Some callbacks cannot be deleted. Usage is NOT recommended, this is an anti-pattern.
272 273 274 |
# File 'lib/em/completion.rb', line 272 def cancel_callback(*a, &b) @callbacks[:succeeded].delete(EM::Callback(*a, &b)) end |
#cancel_errback(*a, &b) ⇒ Object
Remove an errback. N.B. Some errbacks cannot be deleted. Usage is NOT recommended, this is an anti-pattern.
266 267 268 |
# File 'lib/em/completion.rb', line 266 def cancel_errback(*a, &b) @callbacks[:failed].delete(EM::Callback(*a, &b)) end |
#cancel_timeout ⇒ Object
Disable the timeout
257 258 259 260 261 262 |
# File 'lib/em/completion.rb', line 257 def cancel_timeout if @timeout_timer @timeout_timer.cancel @timeout_timer = nil end end |
#change_state(state, *args) ⇒ Object Also known as: set_deferred_status
Enter a new state, setting the result value if given. If the state is one of :succeeded or :failed, then :completed callbacks will also be called.
224 225 226 227 228 229 |
# File 'lib/em/completion.rb', line 224 def change_state(state, *args) @value = args @state = state EM.schedule { execute_callbacks } end |
#completed? ⇒ Boolean
Indicates that we’ve reached some kind of completion state, by default this is :succeeded or :failed. Due to these semantics, the :completed state is reserved for internal use.
237 238 239 |
# File 'lib/em/completion.rb', line 237 def completed? completion_states.any? { |s| state == s } end |
#completion(*a, &b) ⇒ Object
Completions are called when you enter (or are in) either a :failed or a :succeeded state. They are stored as a special (reserved) state called :completed.
218 219 220 |
# File 'lib/em/completion.rb', line 218 def completion(*a, &b) stateback(:completed, *a, &b) end |
#completion_states ⇒ Object
Completion states simply returns a list of completion states, by default this is :succeeded and :failed.
243 244 245 |
# File 'lib/em/completion.rb', line 243 def completion_states [:succeeded, :failed] end |
#errback(*a, &b) ⇒ Object
Errbacks are called when you enter (or are in) a :failed state.
211 212 213 |
# File 'lib/em/completion.rb', line 211 def errback(*a, &b) stateback(:failed, *a, &b) end |
#fail(*args) ⇒ Object Also known as: set_deferred_failure
Enter the :failed state, setting the result value if given.
186 187 188 |
# File 'lib/em/completion.rb', line 186 def fail(*args) change_state(:failed, *args) end |
#stateback(state, *a, &b) ⇒ Object
Statebacks are called when you enter (or are in) the named state.
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/em/completion.rb', line 193 def stateback(state, *a, &b) # The following is quite unfortunate special casing for :completed # statebacks, but it's a necessary evil for latent completion # definitions. if :completed == state || !completed? || @state == state @callbacks[state] << EM::Callback(*a, &b) end execute_callbacks self end |
#succeed(*args) ⇒ Object Also known as: set_deferred_success
Enter the :succeeded state, setting the result value if given.
179 180 181 |
# File 'lib/em/completion.rb', line 179 def succeed(*args) change_state(:succeeded, *args) end |
#timeout(time, *args) ⇒ Object
Schedule a time which if passes before we enter a completion state, this deferrable will be failed with the given arguments.
249 250 251 252 253 254 |
# File 'lib/em/completion.rb', line 249 def timeout(time, *args) cancel_timeout @timeout_timer = EM::Timer.new(time) do fail(*args) unless completed? end end |