Class: Cadence::Workflow::Future
- Inherits:
-
Object
- Object
- Cadence::Workflow::Future
- Defined in:
- lib/cadence/workflow/future.rb
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #cancel ⇒ Object
- #done(&block) ⇒ Object
- #fail(reason, details) ⇒ Object
- #failed? ⇒ Boolean
- #finished? ⇒ Boolean
- #get ⇒ Object
-
#initialize(target, context, cancelation_id: nil) ⇒ Future
constructor
A new instance of Future.
- #ready? ⇒ Boolean
- #set(result) ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(target, context, cancelation_id: nil) ⇒ Future
Returns a new instance of Future.
8 9 10 11 12 13 14 15 16 |
# File 'lib/cadence/workflow/future.rb', line 8 def initialize(target, context, cancelation_id: nil) @target = target @context = context @cancelation_id = cancelation_id @callbacks = [] @ready = false @result = nil @failure = nil end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
6 7 8 |
# File 'lib/cadence/workflow/future.rb', line 6 def callbacks @callbacks end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'lib/cadence/workflow/future.rb', line 6 def target @target end |
Instance Method Details
#cancel ⇒ Object
64 65 66 67 68 |
# File 'lib/cadence/workflow/future.rb', line 64 def cancel return false if finished? context.cancel(target, cancelation_id) end |
#done(&block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cadence/workflow/future.rb', line 53 def done(&block) # do nothing return if failed? if ready? block.call(result) else callbacks << block end end |
#fail(reason, details) ⇒ Object
47 48 49 50 51 |
# File 'lib/cadence/workflow/future.rb', line 47 def fail(reason, details) raise 'can not fail a fulfilled future' if ready? @failure = [reason, details] end |
#failed? ⇒ Boolean
26 27 28 |
# File 'lib/cadence/workflow/future.rb', line 26 def failed? !!@failure end |
#finished? ⇒ Boolean
18 19 20 |
# File 'lib/cadence/workflow/future.rb', line 18 def finished? ready? || failed? end |
#get ⇒ Object
35 36 37 38 |
# File 'lib/cadence/workflow/future.rb', line 35 def get wait failure || result end |
#ready? ⇒ Boolean
22 23 24 |
# File 'lib/cadence/workflow/future.rb', line 22 def ready? @ready end |
#set(result) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/cadence/workflow/future.rb', line 40 def set(result) raise 'can not fulfil a failed future' if failed? @result = result @ready = true end |
#wait ⇒ Object
30 31 32 33 |
# File 'lib/cadence/workflow/future.rb', line 30 def wait return if finished? context.wait_for(self) end |