Sha256: cea1a0d5d66224a5cf59a6487b641526333a6becb3620910a8b208a6ad62203c

Contents?: true

Size: 1.21 KB

Versions: 21

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'i18n/tasks/scanners/scanner'

module I18n::Tasks::Scanners
  # Run multiple {Scanner Scanners} and merge their results.
  # @note The scanners are run concurrently. A thread is spawned per each scanner.
  # @since 0.9.0
  class ScannerMultiplexer < Scanner
    # @param scanners [Array<Scanner>]
    def initialize(scanners:)
      @scanners = scanners
    end

    # Collect the results of all the scanners. Occurrences of a key from multiple scanners are merged.
    #
    # @note The scanners are run concurrently. A thread is spawned per each scanner.
    # @return (see Scanner#keys)
    def keys
      Results::KeyOccurrences.merge_keys collect_results.flatten(1)
    end

    private

    # @return [Array<Array<Results::KeyOccurrences>>]
    def collect_results
      return [@scanners[0].keys] if @scanners.length == 1
      Array.new(@scanners.length).tap do |results|
        results_mutex = Mutex.new
        @scanners.map.with_index do |scanner, i|
          Thread.start do
            scanner_results = scanner.keys
            results_mutex.synchronize do
              results[i] = scanner_results
            end
          end
        end.each(&:join)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
i18n-tasks-0.9.33 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.32 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.31 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.30 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.29 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.28 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.27 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.26 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.25 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.24 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.23 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.22 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.21 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.20 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.19 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.18 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.17 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.16 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.15 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.14 lib/i18n/tasks/scanners/scanner_multiplexer.rb