Class: UU::OS::Lang::Future

Inherits:
Object
  • Object
show all
Defined in:
uu_os_commons-1.5.0/lib/uu/os/lang/future.rb

Overview

Object which is returned as result of all asynchronous tasks.

Instance Method Summary (collapse)

Constructor Details

- (Future) initialize(actor)

Creates new instance of future object.

Parameters:

  • actor (#get)

    Object responsible for obtaining actual result. Accepted is any object with declared #get method.



13
14
15
16
17
18
19
20
21
22
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/future.rb', line 13

def initialize(actor)
  if actor.respond_to?(:get)
    @actor = actor
  elsif actor.nil?
    raise ArgumentError, 'cannot get result of asynchronous task without actor.'
    @actor = nil
  else
    raise TypeError, "cannot handle actor of type #{actor.class}"
  end
end

Instance Method Details

- (Object) get(timeout = 30)

Returns result of asynchronous task. This call is blocking (it waits until result is provided or timeout exceeded).

Parameters:

  • timeout (Fixnum) (defaults to: 30)

    Allows to configure maximum wait time within which result must be returned (defaults to 30 seconds, which is also highest possible timeout).

Returns:

  • (Object)

    Asynchronous task result. Actual type of returned value depends on particular actor given in constructor.

Raises:

  • (Timeout::Error)

    In case of timeout expiration. Not that raised exception does not mean that asynchronous task was cancelled. It may or may not finish based on asynchronous task type (actual behavior depends on task implementation and cannot be affected).



34
35
36
37
38
39
40
41
# File 'uu_os_commons-1.5.0/lib/uu/os/lang/future.rb', line 34

def get(timeout = 30)
  return nil if @actor.nil?
  timeout = 30 if timeout > 30
  timeout = 0.1 if timeout <= 0
  Timeout::timeout(timeout) do
    @actor.get
  end
end