Skip to content

Commit d2fa096

Browse files
committed
Promise extends IVar
1 parent 635c748 commit d2fa096

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/concurrent/promise.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'thread'
22

3+
require 'concurrent/ivar'
34
require 'concurrent/obligation'
45
require 'concurrent/executor/executor_options'
56

@@ -181,8 +182,7 @@ module Concurrent
181182
# - `on_success { |result| ... }` is the same as `then {|result| ... }`
182183
# - `rescue { |reason| ... }` is the same as `then(Proc.new { |reason| ... } )`
183184
# - `rescue` is aliased by `catch` and `on_error`
184-
class Promise
185-
include Obligation
185+
class Promise < IVar
186186
include ExecutorOptions
187187

188188
# Initialize a new Promise with the provided options.
@@ -203,6 +203,7 @@ class Promise
203203
# @see http://promises-aplus.github.io/promises-spec/
204204
def initialize(opts = {}, &block)
205205
opts.delete_if { |k, v| v.nil? }
206+
super(IVar::NO_VALUE, opts)
206207

207208
@executor = get_executor_from(opts) || Concurrent.global_io_executor
208209
@args = get_arguments_from(opts)
@@ -214,17 +215,13 @@ def initialize(opts = {}, &block)
214215
@promise_body = block || Proc.new { |result| result }
215216
@state = :unscheduled
216217
@children = []
217-
218-
init_obligation
219-
set_deref_options(opts)
220218
end
221219

222220
# @return [Promise]
223221
def self.fulfill(value, opts = {})
224222
Promise.new(opts).tap { |p| p.send(:synchronized_set_state!, true, value, nil) }
225223
end
226224

227-
228225
# @return [Promise]
229226
def self.reject(reason, opts = {})
230227
Promise.new(opts).tap { |p| p.send(:synchronized_set_state!, false, nil, reason) }
@@ -444,17 +441,21 @@ def notify_child(child)
444441
if_state(:rejected) { child.on_reject(@reason) }
445442
end
446443

444+
# @!visibility private
445+
def complete(success, value, reason)
446+
children_to_notify = mutex.synchronize do
447+
set_state!(success, value, reason)
448+
@children.dup
449+
end
450+
451+
children_to_notify.each { |child| notify_child(child) }
452+
end
453+
447454
# @!visibility private
448455
def realize(task)
449456
@executor.post do
450457
success, value, reason = SafeTaskExecutor.new(task).execute(*@args)
451-
452-
children_to_notify = mutex.synchronize do
453-
set_state!(success, value, reason)
454-
@children.dup
455-
end
456-
457-
children_to_notify.each { |child| notify_child(child) }
458+
complete(success, value, reason)
458459
end
459460
end
460461

0 commit comments

Comments
 (0)