# frozen_string_literal: true

module Ramverk
  module Middleware # :nodoc:
    # Middleware wrapper for the Zeitwerk constant loader. This will reload all
    # autoloaded constants before each request.
    class Reloader
      # Initialize the middleware.
      #
      # @param app [#call]
      #   Next middleware.
      # @param loader [Zeitwerk::Loader]
      #   Loader object.
      #
      # @return [void]
      def initialize(app, loader)
        @app = app
        @loader = loader
      end

      # Execute the middleware.
      #
      # @param env [Hash]
      #   Rack's environment hash.
      #
      # @return [Array]
      def call(env)
        @loader.reload

        @app.call(env)
      end
    end
  end
end
