Class: Farcall::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/farcall/promise.rb

Overview

Promise let set multiple callbacks on different completion events: success, fail and completion. Promisee guarantte that corresponing callbacks will be called and only once. Callbacks added after completion are being called immediately.

Promise state can be changed only once by calling either #set_success( or #set_fail(). Its subsequent invocations raise errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePromise

Returns a new instance of Promise.



17
18
19
20
# File 'lib/farcall/promise.rb', line 17

def initialize
  @state                   = nil
  @success, @fail, @always = [], [], []
end

Instance Attribute Details

#dataObject (readonly)

returns data passed to the call to #set_success(data), or nil



12
13
14
# File 'lib/farcall/promise.rb', line 12

def data
  @data
end

#errorObject (readonly)

returns data passed to the call to #set_success(error), or nil



15
16
17
# File 'lib/farcall/promise.rb', line 15

def error
  @error
end

Instance Method Details

#always(&block) ⇒ Proomise Also known as: finished

Add handler to the completion event that will receive promise instance as the parameter (thus able to check state and ready #data or #error value).

Note that ‘always` handlers are called after `success` or `fail` ones.

Returns:

  • (Proomise)

    self



71
72
73
74
75
76
77
78
# File 'lib/farcall/promise.rb', line 71

def always &block
  if !completed?
    @always << block
  else
    block.call(self)
  end
  self
end

#completed?Boolean

the promise is completed after one of #set_success(data) and #set_fail(error) is called.

Returns:

  • (Boolean)


31
32
33
# File 'lib/farcall/promise.rb', line 31

def completed?
  @state == :completed
end

#fail(&block) ⇒ Proomise

Add handler for the fail event. If the promise is already #fail? then the block is called immediately, otherwise when and if the promise reach this state.

Block receives error parameter passed to the #set_fail(data)

Returns:

  • (Proomise)

    self



56
57
58
59
60
61
62
63
# File 'lib/farcall/promise.rb', line 56

def fail &block
  if !completed?
    @fail << block
  elsif fail?
    block.call(@error)
  end
  self
end

#fail?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/farcall/promise.rb', line 26

def fail?
  @state == :fail
end

#set_fail(error) ⇒ Object

Set the promise as #completed? with #fail? state and invoke proper handlers. passing ‘error` which is also available with #error property.

If invoked when already #completed?, raises error.



98
99
100
101
102
103
# File 'lib/farcall/promise.rb', line 98

def set_fail error
  raise "state is already set" if @state
  @state = :fail
  @error = error
  invoke @fail, error
end

#set_success(data) ⇒ Object

Set the promise as #completed? with #success? state and invoke proper handlers passing ‘data` which is also available with #data property.

If invoked when already #completed?, raises error.



87
88
89
90
91
92
# File 'lib/farcall/promise.rb', line 87

def set_success data
  raise "state is already set" if @state
  @state = :success
  @data  = data
  invoke @success, data
end

#success(&block) ⇒ Proomise

Add handler for the success event. If the promise is already #success? then the block is called immediately, otherwise when and if the promise reach this state.

block receives data parameter passed to #set_success(data)

Returns:

  • (Proomise)

    self



41
42
43
44
45
46
47
48
# File 'lib/farcall/promise.rb', line 41

def success &block
  if !completed?
    @success << block
  elsif succsess?
    block.call(@data)
  end
  self
end

#succsess?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/farcall/promise.rb', line 22

def succsess?
  @state == :success
end