Class: APromise

Inherits:
Async::Condition
  • Object
show all
Defined in:
lib/apromise.rb

Constant Summary collapse

VERSION =

Constants ============================================================

File.readlines(File.expand_path('../VERSION', __dir__)).first.chomp.freeze
NOT_SPECIFIED =
Object.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value: NOT_SPECIFIED) ⇒ APromise

Instance Methods =====================================================



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/apromise.rb', line 22

def initialize(value: NOT_SPECIFIED)
  if (block_given?)
    begin
      @value = yield
    rescue Exception => e
      @value = e
    end
  elsif (value === NOT_SPECIFIED)
    # Do nothing
  else
    @value = value
  end

  super()
end

Class Method Details

.versionObject

Class Methods ========================================================



16
17
18
# File 'lib/apromise.rb', line 16

def self.version
  VERSION
end

Instance Method Details

#resolve(value: nil, task: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/apromise.rb', line 46

def resolve(value: nil, task: nil)
  @value = value

  if (block_given?)
    begin
      @value = yield
    rescue Exception => e
      @value = e
    end
  end

  reactor = (task ||= Async::Task.current).reactor

  reactor << Async::Notification::Signal.new(@waiting, @value)
  reactor.yield

  @waiting = [ ]

  nil
end

#resolved?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/apromise.rb', line 42

def resolved?
  defined?(@value)
end

#waitObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/apromise.rb', line 67

def wait
  if (defined?(@value))
    case (@value)
    when Exception
      raise @value
    else
      return @value
    end
  end

  super
end

#waiting?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/apromise.rb', line 38

def waiting?
  @waiting.any?
end