Sha256: 0fcc279e55fedb239905b4e59c19fe1d29aefa7abb43571769526b98be3d3f4a

Contents?: true

Size: 929 Bytes

Versions: 2

Compression:

Stored size: 929 Bytes

Contents

# frozen_string_literal: true

require "dry/effects/provider"
require "dry/effects/instructions/raise"
require "dry/effects/halt"

module Dry
  module Effects
    module Providers
      class Interrupt < Provider[:interrupt]
        param :scope, default: -> { :default }

        def interrupt(*payload)
          Instructions.Raise(halt.new(payload))
        end

        # Yield the block with the handler installed
        #
        # @api private
        def call
          [false, yield]
        rescue halt => e
          [true, e.payload[0]]
        end

        def halt
          Halt[scope]
        end

        # @return [String]
        # @api public
        def represent
          "interrupt[#{scope}]"
        end

        # @param [Effect] effect
        # @return [Boolean]
        # @api public
        def provide?(effect)
          super && scope.equal?(effect.scope)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-effects-0.3.0 lib/dry/effects/providers/interrupt.rb
dry-effects-0.2.0 lib/dry/effects/providers/interrupt.rb