Sha256: 6257149ef8bb551c0d91d7826f331d651a50321993ac3573c5e8ce0c29891afb

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require "fiber"

module ActiveSupport
  module IsolatedExecutionState # :nodoc:
    @isolation_level = :thread

    Thread.attr_accessor :active_support_execution_state
    Fiber.attr_accessor :active_support_execution_state

    class << self
      attr_reader :isolation_level

      def isolation_level=(level)
        unless %i(thread fiber).include?(level)
          raise ArgumentError, "isolation_level must be `:thread` or `:fiber`, got: `#{level.inspect}`"
        end

        if level != isolation_level
          clear
          singleton_class.alias_method(:current, "current_#{level}")
          singleton_class.send(:private, :current)
          @isolation_level = level
        end
      end

      def unique_id
        self[:__id__] ||= Object.new
      end

      def [](key)
        current[key]
      end

      def []=(key, value)
        current[key] = value
      end

      def key?(key)
        current.key?(key)
      end

      def delete(key)
        current.delete(key)
      end

      def clear
        current.clear
      end

      private
        def current_thread
          Thread.current.active_support_execution_state ||= {}
        end

        def current_fiber
          Fiber.current.active_support_execution_state ||= {}
        end

        alias_method :current, :current_thread
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/isolated_execution_state.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/isolated_execution_state.rb
activesupport-7.0.2.4 lib/active_support/isolated_execution_state.rb
activesupport-7.0.2.3 lib/active_support/isolated_execution_state.rb
activesupport-7.0.2.2 lib/active_support/isolated_execution_state.rb