1 |
|
2 | var DEFAULT_ACTIONS, buildCachedResourceClass, readArrayCache, readCache, writeCache;
|
3 |
|
4 | DEFAULT_ACTIONS = {
|
5 | get: {
|
6 | method: 'GET'
|
7 | },
|
8 | query: {
|
9 | method: 'GET',
|
10 | isArray: true
|
11 | },
|
12 | save: {
|
13 | method: 'POST'
|
14 | },
|
15 | remove: {
|
16 | method: 'DELETE'
|
17 | },
|
18 | "delete": {
|
19 | method: 'DELETE'
|
20 | }
|
21 | };
|
22 |
|
23 | readArrayCache = require('./read_array_cache');
|
24 |
|
25 | readCache = require('./read_cache');
|
26 |
|
27 | writeCache = require('./write_cache');
|
28 |
|
29 | module.exports = buildCachedResourceClass = function($resource, $timeout, $q, providerParams, args) {
|
30 | var $key, $log, Cache, CachedResource, Resource, ResourceCacheArrayEntry, ResourceCacheEntry, ResourceWriteQueue, actionConfig, actionName, actions, arg, boundParams, handler, isPermissibleBoundValue, method, param, paramDefault, paramDefaults, url;
|
31 | $log = providerParams.$log;
|
32 | ResourceCacheEntry = require('./resource_cache_entry')(providerParams);
|
33 | ResourceCacheArrayEntry = require('./resource_cache_array_entry')(providerParams);
|
34 | ResourceWriteQueue = require('./resource_write_queue')(providerParams, $q);
|
35 | Cache = require('./cache')(providerParams);
|
36 | $key = args.shift();
|
37 | url = args.shift();
|
38 | while (args.length) {
|
39 | arg = args.pop();
|
40 | if (angular.isObject(arg[Object.keys(arg)[0]])) {
|
41 | actions = arg;
|
42 | } else {
|
43 | paramDefaults = arg;
|
44 | }
|
45 | }
|
46 | actions = angular.extend({}, DEFAULT_ACTIONS, actions);
|
47 | if (paramDefaults == null) {
|
48 | paramDefaults = {};
|
49 | }
|
50 | boundParams = {};
|
51 | for (param in paramDefaults) {
|
52 | paramDefault = paramDefaults[param];
|
53 | if (paramDefault[0] === '@') {
|
54 | boundParams[paramDefault.substr(1)] = param;
|
55 | }
|
56 | }
|
57 | Resource = $resource.call(null, url, paramDefaults, actions);
|
58 | isPermissibleBoundValue = function(value) {
|
59 | return angular.isDate(value) || angular.isNumber(value) || angular.isString(value);
|
60 | };
|
61 | CachedResource = (function() {
|
62 | CachedResource.prototype.$cache = true;
|
63 |
|
64 | function CachedResource(attrs) {
|
65 | angular.extend(this, attrs);
|
66 | }
|
67 |
|
68 | CachedResource.prototype.$params = function() {
|
69 | var attribute, params;
|
70 | params = {};
|
71 | for (attribute in boundParams) {
|
72 | param = boundParams[attribute];
|
73 | if (isPermissibleBoundValue(this[attribute])) {
|
74 | params[param] = this[attribute];
|
75 | }
|
76 | }
|
77 | return params;
|
78 | };
|
79 |
|
80 | CachedResource.prototype.$$addToCache = function(dirty) {
|
81 | var entry;
|
82 | if (dirty == null) {
|
83 | dirty = false;
|
84 | }
|
85 | entry = new ResourceCacheEntry($key, this.$params());
|
86 | entry.set(this, dirty);
|
87 | return this;
|
88 | };
|
89 |
|
90 | CachedResource.$clearCache = function(_arg) {
|
91 | var cacheArrayEntry, cacheKeys, clearChildren, clearPendingWrites, entries, exceptFor, isArray, key, params, queue, translateEntriesToCacheKeys, translateParamsArrayToCacheKeys, translateParamsArrayToEntries, where, _ref, _ref1;
|
92 | _ref = _arg != null ? _arg : {}, where = _ref.where, exceptFor = _ref.exceptFor, clearPendingWrites = _ref.clearPendingWrites, isArray = _ref.isArray, clearChildren = _ref.clearChildren;
|
93 | if (where == null) {
|
94 | where = null;
|
95 | }
|
96 | if (exceptFor == null) {
|
97 | exceptFor = null;
|
98 | }
|
99 | if (clearPendingWrites == null) {
|
100 | clearPendingWrites = false;
|
101 | }
|
102 | if (isArray == null) {
|
103 | isArray = false;
|
104 | }
|
105 | if (clearChildren == null) {
|
106 | clearChildren = false;
|
107 | }
|
108 | if (where && exceptFor) {
|
109 | return $log.error("Using where and exceptFor arguments at once in $clearCache() method is forbidden!");
|
110 | }
|
111 | cacheKeys = [];
|
112 | translateParamsArrayToEntries = function(entries) {
|
113 | entries || (entries = []);
|
114 | if (!angular.isArray(entries)) {
|
115 | entries = [entries];
|
116 | }
|
117 | return entries.map(function(entry) {
|
118 | return new CachedResource(entry).$params();
|
119 | });
|
120 | };
|
121 | translateEntriesToCacheKeys = function(params_objects) {
|
122 | return params_objects.map(function(params) {
|
123 | return new ResourceCacheEntry($key, params).fullCacheKey();
|
124 | });
|
125 | };
|
126 | translateParamsArrayToCacheKeys = function(entries) {
|
127 | return translateEntriesToCacheKeys(translateParamsArrayToEntries(entries));
|
128 | };
|
129 | if (exceptFor || where) {
|
130 | if (isArray) {
|
131 | cacheArrayEntry = new ResourceCacheArrayEntry($key, exceptFor || where).load();
|
132 | cacheKeys.push(cacheArrayEntry.fullCacheKey());
|
133 | if (cacheArrayEntry.value && ((exceptFor && !clearChildren) || (where && clearChildren))) {
|
134 | entries = (function() {
|
135 | var _i, _len, _ref1, _results;
|
136 | _ref1 = cacheArrayEntry.value;
|
137 | _results = [];
|
138 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
139 | params = _ref1[_i];
|
140 | _results.push(params);
|
141 | }
|
142 | return _results;
|
143 | })();
|
144 | if (entries) {
|
145 | cacheKeys = cacheKeys.concat(translateEntriesToCacheKeys(entries));
|
146 | }
|
147 | }
|
148 | } else {
|
149 | cacheKeys = translateParamsArrayToCacheKeys(where || exceptFor);
|
150 | }
|
151 | }
|
152 | if (!clearPendingWrites && !where) {
|
153 | _ref1 = CachedResource.$writes, queue = _ref1.queue, key = _ref1.key;
|
154 | cacheKeys.push(key);
|
155 | entries = queue.map(function(resource) {
|
156 | return resource.resourceParams;
|
157 | });
|
158 | cacheKeys = cacheKeys.concat(translateEntriesToCacheKeys(entries));
|
159 | } else if (clearPendingWrites && where) {
|
160 | $log.debug("TODO if clearPendingWrites && where");
|
161 | }
|
162 | if (where) {
|
163 | return Cache.clear({
|
164 | key: $key,
|
165 | where: cacheKeys
|
166 | });
|
167 | } else {
|
168 | return Cache.clear({
|
169 | key: $key,
|
170 | exceptFor: cacheKeys
|
171 | });
|
172 | }
|
173 | };
|
174 |
|
175 | CachedResource.$addToCache = function(attrs, dirty) {
|
176 | return new CachedResource(attrs).$$addToCache(dirty);
|
177 | };
|
178 |
|
179 | CachedResource.$addArrayToCache = function(attrs, instances, dirty) {
|
180 | if (dirty == null) {
|
181 | dirty = false;
|
182 | }
|
183 | instances = instances.map(function(instance) {
|
184 | return new CachedResource(instance);
|
185 | });
|
186 | return new ResourceCacheArrayEntry($key, attrs).addInstances(instances, dirty);
|
187 | };
|
188 |
|
189 | CachedResource.$resource = Resource;
|
190 |
|
191 | CachedResource.$key = $key;
|
192 |
|
193 | return CachedResource;
|
194 |
|
195 | })();
|
196 | CachedResource.$writes = new ResourceWriteQueue(CachedResource, $timeout);
|
197 | for (actionName in actions) {
|
198 | actionConfig = actions[actionName];
|
199 | method = actionConfig.method.toUpperCase();
|
200 | if (actionConfig.cache !== false) {
|
201 | handler = method === 'GET' && actionConfig.isArray ? readArrayCache($q, providerParams, actionName, CachedResource, actionConfig) : method === 'GET' ? readCache($q, providerParams, actionName, CachedResource, actionConfig) : method === 'POST' || method === 'PUT' || method === 'DELETE' || method === 'PATCH' ? writeCache($q, providerParams, actionName, CachedResource, actionConfig) : void 0;
|
202 | CachedResource[actionName] = handler;
|
203 | if (method !== 'GET') {
|
204 | CachedResource.prototype["$" + actionName] = handler;
|
205 | }
|
206 | } else {
|
207 | CachedResource[actionName] = Resource[actionName];
|
208 | CachedResource.prototype["$" + actionName] = Resource.prototype["$" + actionName];
|
209 | }
|
210 | }
|
211 | return CachedResource;
|
212 | };
|