lib/assets/javascripts/up/proxy.js.coffee in upjs-rails-0.5.0 vs lib/assets/javascripts/up/proxy.js.coffee in upjs-rails-0.6.0

- old
+ new

@@ -24,14 +24,19 @@ cacheSize: 70 cacheExpiry: 1000 * 60 * 5 ###* @method up.proxy.defaults - @param {Number} [options.preloadDelay] - @param {Number} [options.cacheSize] - @param {Number} [options.cacheExpiry] + @param {Number} [options.preloadDelay=75] + The number of milliseconds to wait before [`[up-preload]`](#up-preload) + starts preloading. + @param {Number} [options.cacheSize=70] + The maximum number of responses to cache. + If the size is exceeded, the oldest items will be dropped from the cache. + @param {Number} [options.cacheExpiry=300000] The number of milliseconds until a cache entry expires. + Defaults to 5 minutes. ### defaults = (options) -> u.extend(config, options) cache = {} @@ -89,21 +94,33 @@ @method up.proxy.ajax @param {String} request.url @param {String} [request.method='GET'] @param {String} [request.selector] + @param {Boolean} [request.cache] + Whether to use a cached response, if available. + If set to `false` a network connection will always be attempted. ### - ajax = (request) -> - if !isIdempotent(request) + ajax = (options) -> + forceCache = u.castsToTrue(options.cache) + ignoreCache = u.castsToFalse(options.cache) + + request = u.only(options, 'url', 'method', 'selector') + + # We don't cache non-GET responses unless `options.cache` + # is explicitly set to `true`. + if !isIdempotent(request) && !forceCache clear() - # We don't cache non-GET responses promise = u.ajax(request) - else if promise = get(request) - touch(promise) + # If a cached response is available, we use it unless + # `options.cache` is explicitly set to `false`. + else if promise = get(request) && !ignoreCache + promise else promise = u.ajax(request) set(request, promise) + promise SAFE_HTTP_METHODS = ['GET', 'OPTIONS', 'HEAD'] isIdempotent = (request) -> @@ -114,14 +131,11 @@ isIdempotent(request) or u.error("Won't preload non-GET request %o", request) isFresh = (promise) -> timeSinceTouch = timestamp() - promise.timestamp timeSinceTouch < config.cacheExpiry - - touch = (promise) -> - promise.timestamp = timestamp() - + get = (request) -> key = cacheKey(request) if promise = cache[key] if !isFresh(promise) u.debug("Discarding stale cache entry for %o (%o)", request.url, request) @@ -137,12 +151,12 @@ undefined set = (request, promise) -> trim() key = cacheKey(request) + promise.timestamp = timestamp() cache[key] = promise - touch(promise) promise remove = (request) -> key = cacheKey(request) delete cache[key] @@ -203,8 +217,9 @@ ajax: ajax get: get set: set alias: alias clear: clear + remove: remove defaults: defaults )()