Sha256: fdd94a96802f16fe8b2ea10be54f22ee1cd25b27b4daa313f38b8434b606ff60

Contents?: true

Size: 937 Bytes

Versions: 2

Compression:

Stored size: 937 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(_stack)
          [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.1.1 lib/dry/effects/providers/interrupt.rb
dry-effects-0.1.0 lib/dry/effects/providers/interrupt.rb