# frozen_string_literal: true module Wayfarer module Middleware class Dedup extend Base def call(task) # Was task routed by a previous controller already? return yield if task.metadata.action return if task.barrier.seen?(task.url) begin yield if block_given? rescue StandardError => e task.barrier.unsee(task.url) raise e end staged_urls = task.metadata.staged_urls return if staged_urls.none? inclusion = task.barrier.peek(staged_urls.to_a) unseen = staged_urls.zip(inclusion).reject { |_, seen| seen }.map(&:first) task.metadata.staged_urls = SortedSet.new(unseen) end end end end