Class: Dry::Effects::Frame Private

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/dry/effects/frame.rb

Overview

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

Stack frame

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializer

extended

Instance Attribute Details

#providerProvider

Returns Effects provider.

Returns:



70
# File 'lib/dry/effects/frame.rb', line 70

param :provider

Class Method Details

.spawn_fiber(stack) ⇒ 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.

Spawn a new fiber with a stack of effect handlers

Parameters:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dry/effects/frame.rb', line 31

def spawn_fiber(stack)
  fiber = ::Fiber.new do
    self.stack = stack
    yield
  end
  result = fiber.resume

  loop do
    error = false
    break result unless fiber.alive?

    provided = stack.(result) do
      ::Dry::Effects.yield(result) do |_, e|
        error = true
        e
      end
    end

    result =
      if error
        raise_in_fiber(fiber, provided)
      else
        fiber.resume(provided)
      end
  end
end

.stackStack

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.

Accessing current stack of effect handlers. It is inherently thread/fiber-local.

Returns:



17
18
19
# File 'lib/dry/effects/frame.rb', line 17

def stack
  ::Thread.current[:dry_effects_stack] ||= Stack.new
end

.stack=(stack) ⇒ 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.

Parameters:



23
24
25
# File 'lib/dry/effects/frame.rb', line 23

def stack=(stack)
  ::Thread.current[:dry_effects_stack] = stack
end

Instance Method Details

#call(*args, &block) ⇒ 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.

Add new handler to the current stack and run the given block

Parameters:

  • args (Array<Object>)

    Handler arguments

  • block (Proc)

    Program to run



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dry/effects/frame.rb', line 78

def call(*args, &block)
  stack = Frame.stack
  prov = provider.dup
  was_empty = stack.empty?

  prov.(*args) do
    if was_empty
      stack.push(prov) do
        Frame.spawn_fiber(stack, &block)
      end
    else
      stack.push(prov, &block)
    end
  end
end