aboutsummaryrefslogtreecommitdiffstats
path: root/src/service/xhr.cache.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/xhr.cache.js')
-rw-r--r--src/service/xhr.cache.js148
1 files changed, 76 insertions, 72 deletions
diff --git a/src/service/xhr.cache.js b/src/service/xhr.cache.js
index 335c481d..a448bfe0 100644
--- a/src/service/xhr.cache.js
+++ b/src/service/xhr.cache.js
@@ -28,87 +28,91 @@
* cached entry. The `success` function will be called when the response is received.
* @param {boolean=} [sync=false] in case of cache hit execute `success` synchronously.
*/
-angularServiceInject('$xhr.cache', function($xhr, $defer, $error, $log) {
- var inflight = {};
- function cache(method, url, post, success, error, verifyCache, sync) {
- if (isFunction(post)) {
- if (!isFunction(success)) {
- verifyCache = success;
- sync = error;
- error = null;
- } else {
+function $XhrCacheProvider() {
+ this.$get = ['$xhr.bulk', '$defer', '$xhr.error', '$log',
+ function($xhr, $defer, $error, $log) {
+ var inflight = {};
+ function cache(method, url, post, success, error, verifyCache, sync) {
+ if (isFunction(post)) {
+ if (!isFunction(success)) {
+ verifyCache = success;
+ sync = error;
+ error = null;
+ } else {
+ sync = verifyCache;
+ verifyCache = error;
+ error = success;
+ }
+ success = post;
+ post = null;
+ } else if (!isFunction(error)) {
sync = verifyCache;
verifyCache = error;
- error = success;
+ error = null;
}
- success = post;
- post = null;
- } else if (!isFunction(error)) {
- sync = verifyCache;
- verifyCache = error;
- error = null;
- }
- if (method == 'GET') {
- var data, dataCached;
- if ((dataCached = cache.data[url])) {
+ if (method == 'GET') {
+ var data, dataCached;
+ if ((dataCached = cache.data[url])) {
- if (sync) {
- success(200, copy(dataCached.value));
- } else {
- $defer(function() { success(200, copy(dataCached.value)); });
- }
+ if (sync) {
+ success(200, copy(dataCached.value));
+ } else {
+ $defer(function() { success(200, copy(dataCached.value)); });
+ }
- if (!verifyCache)
- return;
- }
+ if (!verifyCache)
+ return;
+ }
- if ((data = inflight[url])) {
- data.successes.push(success);
- data.errors.push(error);
- } else {
- inflight[url] = {successes: [success], errors: [error]};
- cache.delegate(method, url, post,
- function(status, response) {
- if (status == 200)
- cache.data[url] = {value: response};
- var successes = inflight[url].successes;
- delete inflight[url];
- forEach(successes, function(success) {
- try {
- (success||noop)(status, copy(response));
- } catch(e) {
- $log.error(e);
- }
- });
- },
- function(status, response) {
- var errors = inflight[url].errors,
- successes = inflight[url].successes;
- delete inflight[url];
+ if ((data = inflight[url])) {
+ data.successes.push(success);
+ data.errors.push(error);
+ } else {
+ inflight[url] = {successes: [success], errors: [error]};
+ cache.delegate(method, url, post,
+ function(status, response) {
+ if (status == 200)
+ cache.data[url] = {value: response};
+ var successes = inflight[url].successes;
+ delete inflight[url];
+ forEach(successes, function(success) {
+ try {
+ (success||noop)(status, copy(response));
+ } catch(e) {
+ $log.error(e);
+ }
+ });
+ },
+ function(status, response) {
+ var errors = inflight[url].errors,
+ successes = inflight[url].successes;
+ delete inflight[url];
- forEach(errors, function(error, i) {
- try {
- if (isFunction(error)) {
- error(status, copy(response));
- } else {
- $error(
- {method: method, url: url, data: post, success: successes[i]},
- {status: status, body: response});
+ forEach(errors, function(error, i) {
+ try {
+ if (isFunction(error)) {
+ error(status, copy(response));
+ } else {
+ $error(
+ {method: method, url: url, data: post, success: successes[i]},
+ {status: status, body: response});
+ }
+ } catch(e) {
+ $log.error(e);
}
- } catch(e) {
- $log.error(e);
- }
+ });
});
- });
- }
+ }
- } else {
- cache.data = {};
- cache.delegate(method, url, post, success, error);
+ } else {
+ cache.data = {};
+ cache.delegate(method, url, post, success, error);
+ }
}
- }
- cache.data = {};
- cache.delegate = $xhr;
- return cache;
-}, ['$xhr.bulk', '$defer', '$xhr.error', '$log']);
+ cache.data = {};
+ cache.delegate = $xhr;
+ return cache;
+ }];
+
+}