Sha256: 18a0a5a99f2b396ec60bc03bd0b8c0cc66d11961d10ebd53d72349b9f40d1eeb

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 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|
        @scanners.map.with_index { |scanner, i|
          Thread.start { results[i] = scanner.keys }
        }.each(&:join)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i18n-tasks-0.9.6 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.5 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.4 lib/i18n/tasks/scanners/scanner_multiplexer.rb
i18n-tasks-0.9.3 lib/i18n/tasks/scanners/scanner_multiplexer.rb