Class: OsCtld::Promise

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

Defined Under Namespace

Classes: Token

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Promise

Returns a new instance of Promise.



23
24
25
26
27
# File 'lib/osctld/promise.rb', line 23

def initialize
  @mutex = Mutex.new
  @tokens = []
  @fulfilled = false
end

Instance Method Details

#add ⇒ Token

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/osctld/promise.rb', line 30

def add
  t = Token.new

  @mutex.synchronize do
    if @fulfilled
      t.fulfil
    else
      @tokens << t
    end
  end

  t
end

#fulfil ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/osctld/promise.rb', line 44

def fulfil
  @mutex.synchronize do
    return if @fulfilled

    @fulfilled = true
    @tokens.each(&:fulfil)
    @tokens.clear
  end

  nil
end