Sha256: fff2ac7f1b2610420fe67aa95ddcff3cab2a612e77c0b0a40fda0b5e19a03ed9

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

require "dry/effects/provider"

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

2 entries across 2 versions & 1 rubygems

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