aboutsummaryrefslogtreecommitdiffstats
path: root/src/service/xhr.cache.js
blob: a448bfe092d2ba78030d9d92ab87b7d266878bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';

/**
 * @ngdoc service
 * @name angular.service.$xhr.cache
 * @function
 *
 * @requires $xhr.bulk
 * @requires $defer
 * @requires $xhr.error
 * @requires $log
 *
 * @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))} success Response success callback.
 * @param {function(number, (string|Object))=} error Response error 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 `success` function will be called when the response is received.
 * @param {boolean=} [sync=false] in case of cache hit execute `success` synchronously.
 */
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 = null;
      }

      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 (!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];

              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);
                }
              });
            });
        }

      } else {
        cache.data = {};
        cache.delegate(method, url, post, success, error);
      }
    }
    cache.data = {};
    cache.delegate = $xhr;
    return cache;
  }];

}