Module: Cql::FutureCallbacks

Included in:
Future
Defined in:
lib/cql/future.rb

Instance Method Summary collapse

Instance Method Details

#on_complete {|future| ... } ⇒ Object

Registers a listener that will be called when this future completes, i.e. resolves or fails. The listener will be called with the future as solve argument

Yield Parameters:



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cql/future.rb', line 248

def on_complete(&listener)
  run_immediately = false
  @lock.synchronize do
    if @resolved || @failed
      run_immediately = true
    else
      @complete_listeners << listener
    end
  end
  if run_immediately
    listener.call(self) rescue nil
  end
  nil
end

#on_failure {|error| ... } ⇒ Object

Registers a listener that will be called when this future fails. The lisener will be called with the error that failed the future as sole argument.

Yield Parameters:

  • error (Error)

    the error that failed the future



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/cql/future.rb', line 288

def on_failure(&listener)
  run_immediately = false
  @lock.synchronize do
    if @failed
      run_immediately = true
    elsif !@resolved
      @failure_listeners << listener
    end
  end
  if run_immediately
    listener.call(@error) rescue nil
  end
  nil
end

#on_value {|value| ... } ⇒ Object

Registers a listener that will be called when this future becomes resolved. The listener will be called with the value of the future as sole argument.

Yield Parameters:

  • value (Object)

    the value of the resolved future



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/cql/future.rb', line 268

def on_value(&listener)
  run_immediately = false
  @lock.synchronize do
    if @resolved
      run_immediately = true
    elsif !@failed
      @value_listeners << listener
    end
  end
  if run_immediately
    listener.call(value) rescue nil
  end
  nil
end