Sha256: 40f2bda32bacb3385dd2a6961a9d2b098e05b2dba1a033247ba955c5d20345c7

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-effects-0.5.0 lib/dry/effects/providers/timeout.rb