lib/assets/javascripts/up/proxy.js.coffee in upjs-rails-0.18.0 vs lib/assets/javascripts/up/proxy.js.coffee in upjs-rails-0.18.1

- old
+ new

@@ -120,11 +120,11 @@ cache = u.cache size: -> config.cacheSize expiry: -> config.cacheExpiry key: cacheKey - log: 'up.proxy' + # log: 'up.proxy' ###* Returns a cached response for the given request. Returns `undefined` if the given request is not currently cached. @@ -252,10 +252,11 @@ forceCache = (options.cache == true) ignoreCache = (options.cache == false) request = u.only(options, 'url', 'method', 'data', 'target', 'headers', '_normalized') + request = normalizeRequest(request) pending = true # Non-GET requests always touch the network # unless `options.cache` is explicitly set to `true`. @@ -265,10 +266,11 @@ promise = loadOrQueue(request) # If we have an existing promise matching this new request, # we use it unless `options.cache` is explicitly set to `false`. # The promise might still be pending. else if (promise = get(request)) && !ignoreCache + up.puts 'Re-using cached response for %s %s', request.method, request.url pending = (promise.state() == 'pending') # If no existing promise is available, we make a network request. else promise = loadOrQueue(request) set(request, promise) @@ -287,10 +289,12 @@ # - The request finishes. # This triggers `up:proxy:idle`. loadStarted() promise.always(loadEnded) + console.groupEnd() + promise ###* Returns `true` if the proxy is not currently waiting for a request to finish. Returns `false` otherwise. @@ -327,11 +331,11 @@ if wasIdle # Since the emission of up:proxy:busy might be delayed by config.busyDelay, # we wrap the mission in a function for scheduling below. emission = -> if busy() # a fast response might have beaten the delay - up.emit('up:proxy:busy') + up.emit('up:proxy:busy', message: 'Proxy is busy') busyEventEmitted = true if config.busyDelay > 0 busyDelayTimer = setTimeout(emission, config.busyDelay) else emission() @@ -356,11 +360,11 @@ ### loadEnded = -> pendingCount -= 1 if idle() && busyEventEmitted - up.emit('up:proxy:idle') + up.emit('up:proxy:idle', message: 'Proxy is idle') busyEventEmitted = false ###* This event is [emitted]/(up.emit) when [AJAX requests](/up.proxy.ajax) have [taken long to finish](/up:proxy:busy), but have finished now. @@ -374,21 +378,20 @@ load(request) else queue(request) queue = (request) -> - u.debug('Queuing URL %o', request.url) + up.puts('Queuing request for %s %s', request.method, request.url) deferred = $.Deferred() entry = deferred: deferred request: request queuedRequests.push(entry) deferred.promise() load = (request) -> - u.debug('Fetching %o via %o', request.url, request.method) - up.emit('up:proxy:load', request) + up.emit('up:proxy:load', u.merge(request, message: ['Loading %s %s', request.method, request.url])) # We will modify the request below for features like method wrapping. # Let's not change the original request which would confuse API clients # and cache key logic. request = u.copy(request) @@ -402,15 +405,18 @@ name: config.wrapMethodParam value: request.method request.method = 'POST' promise = $.ajax(request) - promise.always -> - up.emit('up:proxy:received', request) - pokeQueue() + promise.done (data, textStatus, xhr) -> responseReceived(request, xhr) + promise.fail (xhr, textStatus, errorThrown) -> responseReceived(request, xhr) promise + responseReceived = (request, xhr) -> + up.emit('up:proxy:received', u.merge(request, message: ['Server responded with %s %s (%d bytes)', xhr.status, xhr.statusText, xhr.responseText?.length])) + pokeQueue() + pokeQueue = -> if entry = queuedRequests.shift() promise = load(entry.request) promise.done (args...) -> entry.deferred.resolve(args...) promise.fail (args...) -> entry.deferred.reject(args...) @@ -466,14 +472,14 @@ $link = $(linkOrSelector) options = u.options(options) method = up.link.followMethod($link, options) if isIdempotent(method: method) - u.debug("Preloading %o", $link) - options.preload = true - up.follow($link, options) + up.log.group "Preloading link %o", $link, -> + options.preload = true + up.follow($link, options) else - u.debug("Won't preload %o due to unsafe method %o", $link, method) + up.puts("Won't preload %o due to unsafe method %s", $link, method) u.resolvedPromise() ###* Links with an `up-preload` attribute will silently fetch their target when the user hovers over the click area, or when the user puts her