Class: Airbrake::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake-ruby/promise.rb

Overview

Represents a simplified promise object (similar to promises found in JavaScript), which allows chaining callbacks that are executed when the promise is either resolved or rejected.

Instance Method Summary collapse

Constructor Details

#initializePromise

Returns a new instance of Promise.

Since:

  • v1.7.0



10
11
12
13
14
15
# File 'lib/airbrake-ruby/promise.rb', line 10

def initialize
  @on_resolved = []
  @on_rejected = []
  @value = {}
  @mutex = Mutex.new
end

Instance Method Details

#reject(reason = 'rejected') ⇒ self

Examples:

Airbrake::Promise.new.reject('Something went wrong')

Parameters:

  • reason (String) (defaults to: 'rejected')

Returns:

  • (self)

Since:

  • v1.7.0



80
81
82
83
84
85
86
87
# File 'lib/airbrake-ruby/promise.rb', line 80

def reject(reason = 'rejected')
  @mutex.synchronize do
    @value['error'] = reason
    @on_rejected.each { |callback| callback.call(reason) }
  end

  self
end

#rejected?Boolean

Returns:

  • (Boolean)

Since:

  • v1.7.0



90
91
92
# File 'lib/airbrake-ruby/promise.rb', line 90

def rejected?
  @value.key?('error')
end

#rescue {|error| ... } ⇒ self

Attaches a callback to be executed when the promise is rejected.

Examples:

Airbrake::Promise.new.rescue { |error| raise error }

Yields:

  • (error)

    The error message from the API

Yield Parameters:

  • error (String)

Returns:

  • (self)

Since:

  • v1.7.0



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/airbrake-ruby/promise.rb', line 48

def rescue(&block)
  @mutex.synchronize do
    if @value.key?('error')
      yield(@value['error'])
      return self
    end

    @on_rejected << block
  end

  self
end

#resolve(reason = 'resolved') ⇒ self

Examples:

Airbrake::Promise.new.resolve('id' => '123')

Parameters:

  • reason (Object) (defaults to: 'resolved')

Returns:

  • (self)

Since:

  • v1.7.0



66
67
68
69
70
71
72
73
# File 'lib/airbrake-ruby/promise.rb', line 66

def resolve(reason = 'resolved')
  @mutex.synchronize do
    @value['ok'] = reason
    @on_resolved.each { |callback| callback.call(reason) }
  end

  self
end

#resolved?Boolean

Returns:

  • (Boolean)

Since:

  • v1.7.0



95
96
97
# File 'lib/airbrake-ruby/promise.rb', line 95

def resolved?
  @value.key?('ok')
end

#then {|response| ... } ⇒ self

Attaches a callback to be executed when the promise is resolved.

Examples:

Airbrake::Promise.new.then { |response| puts response }
#=> {"id"=>"00054415-8201-e9c6-65d6-fc4d231d2871",
#    "url"=>"http://localhost/locate/00054415-8201-e9c6-65d6-fc4d231d2871"}

Yields:

  • (response)

Yield Parameters:

  • response (Hash<String,String>)

    Contains the ‘id` & `url` keys

Returns:

  • (self)

Since:

  • v1.7.0



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/airbrake-ruby/promise.rb', line 27

def then(&block)
  @mutex.synchronize do
    if @value.key?('ok')
      yield(@value['ok'])
      return self
    end

    @on_resolved << block
  end

  self
end

#valueHash<String,String>

TODO:

Get rid of this method and use an accessor. The resolved guard is needed for compatibility but it shouldn’t exist in the future

Note:

This is a non-blocking call!

Returns either successful response containing the id key or unsuccessful response containing the error key.

Returns:

  • (Hash<String,String>)

    either successful response containing the id key or unsuccessful response containing the error key

Since:

  • v1.7.0



104
105
106
107
108
# File 'lib/airbrake-ruby/promise.rb', line 104

def value
  return @value['ok'] if resolved?

  @value
end