Class: Cadence::Workflow::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/cadence/workflow/future.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#callbacksObject (readonly)

Returns the value of attribute callbacks.



6
7
8
# File 'lib/cadence/workflow/future.rb', line 6

def callbacks
  @callbacks
end

#targetObject (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

#cancelObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/cadence/workflow/future.rb', line 26

def failed?
  !!@failure
end

#finished?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cadence/workflow/future.rb', line 18

def finished?
  ready? || failed?
end

#getObject



35
36
37
38
# File 'lib/cadence/workflow/future.rb', line 35

def get
  wait
  failure || result
end

#ready?Boolean

Returns:

  • (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

#waitObject



30
31
32
33
# File 'lib/cadence/workflow/future.rb', line 30

def wait
  return if finished?
  context.wait_for(self)
end