Sha256: 371bb781539b9a6d7fcd98d16f4cc6aa15a331deacec45c49ecbb5225f6c1d8e

Contents?: true

Size: 1.43 KB

Versions: 20

Compression:

Stored size: 1.43 KB

Contents

module CypressRails
  class Server
    class Middleware
      class Counter
        def initialize
          @value = []
          @mutex = Mutex.new
        end

        def increment(uri)
          @mutex.synchronize { @value.push(uri) }
        end

        def decrement(uri)
          @mutex.synchronize { @value.delete_at(@value.index(uri) || @value.length) }
        end

        def positive?
          @mutex.synchronize { @value.length.positive? }
        end

        def value
          @mutex.synchronize { @value.dup }
        end
      end

      attr_reader :error

      def initialize(app, server_errors, extra_middleware = [])
        @app = app
        @extended_app = extra_middleware.inject(@app) { |ex_app, klass|
          klass.new(ex_app)
        }
        @counter = Counter.new
        @server_errors = server_errors
      end

      def pending_requests
        @counter.value
      end

      def pending_requests?
        @counter.positive?
      end

      def clear_error
        @error = nil
      end

      def call(env)
        if env["PATH_INFO"] == "/__identify__"
          [200, {}, [@app.object_id.to_s]]
        else
          @counter.increment(env["REQUEST_URI"])
          begin
            @extended_app.call(env)
          rescue *@server_errors => e
            @error ||= e
            raise e
          ensure
            @counter.decrement(env["REQUEST_URI"])
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
cypress-rails-0.8.0.rc1 lib/cypress-rails/server/middleware.rb
cypress-rails-0.7.1 lib/cypress-rails/server/middleware.rb
cypress-rails-0.7.0 lib/cypress-rails/server/middleware.rb
cypress-rails-0.6.1 lib/cypress-rails/server/middleware.rb
cypress-rails-0.6.0 lib/cypress-rails/server/middleware.rb
cypress-rails-0.5.5 lib/cypress-rails/server/middleware.rb
cypress-rails-0.5.4 lib/cypress-rails/server/middleware.rb
cypress-rails-0.5.3 lib/cypress-rails/server/middleware.rb
cypress-rails-0.5.2 lib/cypress-rails/server/middleware.rb
cypress-rails-0.5.1 lib/cypress-rails/server/middleware.rb
cypress-rails-0.5.0 lib/cypress-rails/server/middleware.rb
cypress-rails-0.4.2 lib/cypress-rails/server/middleware.rb
cypress-rails-0.4.1 lib/cypress-rails/server/middleware.rb
cypress-rails-0.4.0 lib/cypress-rails/server/middleware.rb
cypress-rails-0.3.0 lib/cypress-rails/server/middleware.rb
cypress-rails-0.2.0 lib/cypress-rails/server/middleware.rb
cypress-rails-0.1.5 lib/cypress-rails/server/middleware.rb
cypress-rails-0.1.4 lib/cypress-rails/server/middleware.rb
cypress-rails-0.1.3 lib/cypress-rails/server/middleware.rb
cypress-rails-0.1.2 lib/cypress-rails/server/middleware.rb