aboutsummaryrefslogtreecommitdiffstats
path: root/src/service/xhr.cache.js
diff options
context:
space:
mode:
authorIgor Minar2011-02-15 01:12:45 -0500
committerIgor Minar2011-02-15 11:01:53 -0500
commit1777110958f76ee4be5760e36c96702223385918 (patch)
tree5aa03b246507e66877e5eac69e58e004e244d7a5 /src/service/xhr.cache.js
parentd2089a16335276eecb8d81eb17332c2dff2cf1a2 (diff)
downloadangular.js-1777110958f76ee4be5760e36c96702223385918.tar.bz2
split up services into individual files
- split up services into files under src/service - split up specs into files under test/service - rewrite all specs so that they don't depend on one global forEach - get rid of obsolete code and tests in ng:switch - rename mock $log spec from "$log" to "$log mock"
Diffstat (limited to 'src/service/xhr.cache.js')
-rw-r--r--src/service/xhr.cache.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/service/xhr.cache.js b/src/service/xhr.cache.js
new file mode 100644
index 00000000..e87b127b
--- /dev/null
+++ b/src/service/xhr.cache.js
@@ -0,0 +1,66 @@
+/**
+ * @workInProgress
+ * @ngdoc service
+ * @name angular.service.$xhr.cache
+ * @function
+ * @requires $xhr
+ *
+ * @description
+ * Acts just like the {@link angular.service.$xhr $xhr} service but caches responses for `GET`
+ * requests. All cache misses are delegated to the $xhr service.
+ *
+ * @property {function()} delegate Function to delegate all the cache misses to. Defaults to
+ * the {@link angular.service.$xhr $xhr} service.
+ * @property {object} data The hashmap where all cached entries are stored.
+ *
+ * @param {string} method HTTP method.
+ * @param {string} url Destination URL.
+ * @param {(string|Object)=} post Request body.
+ * @param {function(number, (string|Object))} callback Response callback.
+ * @param {boolean=} [verifyCache=false] If `true` then a result is immediately returned from cache
+ * (if present) while a request is sent to the server for a fresh response that will update the
+ * cached entry. The `callback` function will be called when the response is received.
+ */
+angularServiceInject('$xhr.cache', function($xhr, $defer, $log){
+ var inflight = {}, self = this;
+ function cache(method, url, post, callback, verifyCache){
+ if (isFunction(post)) {
+ callback = post;
+ post = _null;
+ }
+ if (method == 'GET') {
+ var data, dataCached;
+ if (dataCached = cache.data[url]) {
+ $defer(function() { callback(200, copy(dataCached.value)); });
+ if (!verifyCache)
+ return;
+ }
+
+ if (data = inflight[url]) {
+ data.callbacks.push(callback);
+ } else {
+ inflight[url] = {callbacks: [callback]};
+ cache.delegate(method, url, post, function(status, response){
+ if (status == 200)
+ cache.data[url] = { value: response };
+ var callbacks = inflight[url].callbacks;
+ delete inflight[url];
+ forEach(callbacks, function(callback){
+ try {
+ (callback||noop)(status, copy(response));
+ } catch(e) {
+ $log.error(e);
+ }
+ });
+ });
+ }
+
+ } else {
+ cache.data = {};
+ cache.delegate(method, url, post, callback);
+ }
+ }
+ cache.data = {};
+ cache.delegate = $xhr;
+ return cache;
+}, ['$xhr.bulk', '$defer', '$log']);