Class: Dry::Effects::Providers::Defer

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/effects/providers/defer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#later_callsObject (readonly)

Returns the value of attribute later_calls.



13
14
15
# File 'lib/dry/effects/providers/defer.rb', line 13

def later_calls
  @later_calls
end

#stackObject (readonly)

Returns the value of attribute stack.



15
16
17
# File 'lib/dry/effects/providers/defer.rb', line 15

def stack
  @stack
end

Instance Method Details

#call(options = {executor: Undefined}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yield the block with the handler installed

API:

  • private



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dry/effects/providers/defer.rb', line 52

def call(options = {executor: Undefined})
  unless Undefined.equal?(options[:executor])
    @executor = options[:executor]
  end

  @stack = Frame.stack
  @later_calls = []
  yield
ensure
  later_calls.each(&:execute)
end

#defer(block, executor) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/dry/effects/providers/defer.rb', line 17

def defer(block, executor)
  stack = self.stack.dup
  at = Undefined.default(executor, self.executor)
  ::Concurrent::Promise.execute(executor: at) do
    Frame.spawn_fiber(stack, &block)
  end
end

#dupObject



64
65
66
67
68
69
70
# File 'lib/dry/effects/providers/defer.rb', line 64

def dup
  if defined? @later_calls
    super.tap { _1.instance_variable_set(:@later_calls, EMPTY_ARRAY) }
  else
    super
  end
end

#later(block, executor) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dry/effects/providers/defer.rb', line 25

def later(block, executor)
  if @later_calls.frozen?
    Instructions.Raise(Errors::EffectRejectedError.new("      .later calls are not allowed, they would be processed\n      by another stack. Add another defer handler to the current stack\n    MSG\n  else\n    at = Undefined.default(executor, self.executor)\n    stack = self.stack.dup\n    @later_calls << ::Concurrent::Promise.new(executor: at) do\n      Frame.spawn_fiber(stack, &block)\n    end\n    nil\n  end\nend\n"))

#representString

Returns:

API:

  • public



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dry/effects/providers/defer.rb', line 74

def represent
  info = []
  info << executor.to_s if executor.is_a?(::Symbol)
  info << "call_later=#{later_calls.size}" if later_calls.any?

  if info.empty?
    "defer"
  else
    "defer[#{info.join(" ")}]"
  end
end

#wait(promises) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/dry/effects/providers/defer.rb', line 41

def wait(promises)
  if promises.is_a?(::Array)
    ::Concurrent::Promise.zip(*promises).value!
  else
    promises.value!
  end
end