Sha256: b964b4c2dd9d4aeb8744d3a9305605a463a3b38969165d58f43011d4f29b9cc7

Contents?: true

Size: 933 Bytes

Versions: 3

Compression:

Stored size: 933 Bytes

Contents

# frozen_string_literal: true

module Dry
  module Effects
    module Providers
      class Timeout < Provider[:timeout]
        def self.handle_method(_scope, as: Undefined, **)
          Undefined.default(as) { :with_timeout }
        end

        param :scope

        def timeout
          left = @time_out_at - read_clock

          if left <= 0
            0.0
          else
            left
          end
        end

        # Yield the block with the handler installed
        #
        # @api private
        def call(timeout)
          @time_out_at = read_clock + timeout

          yield
        end

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

        def read_clock
          ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-effects-0.4.1 lib/dry/effects/providers/timeout.rb
dry-effects-0.4.0 lib/dry/effects/providers/timeout.rb
dry-effects-0.3.1 lib/dry/effects/providers/timeout.rb