UNPKG

3.45 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.8.0
2var modifyObjectInPlace, processReadArgs, readArrayCache,
3 __slice = [].slice;
4
5processReadArgs = require('./process_read_args');
6
7modifyObjectInPlace = require('./modify_object_in_place');
8
9module.exports = readArrayCache = function($q, providerParams, name, CachedResource, actionConfig) {
10 var ResourceCacheArrayEntry, ResourceCacheEntry, first;
11 ResourceCacheEntry = require('./resource_cache_entry')(providerParams);
12 ResourceCacheArrayEntry = require('./resource_cache_array_entry')(providerParams);
13 first = function(array, params) {
14 var found, item, itemParams, _i, _len;
15 found = null;
16 for (_i = 0, _len = array.length; _i < _len; _i++) {
17 item = array[_i];
18 itemParams = item.$params();
19 if (Object.keys(params).every(function(key) {
20 return itemParams[key] === params[key];
21 })) {
22 found = item;
23 break;
24 }
25 }
26 return found;
27 };
28 return function() {
29 var arrayInstance, cacheArrayEntry, cacheDeferred, cacheInstanceEntry, cacheInstanceParams, httpDeferred, params, readHttp, _i, _len, _ref, _ref1;
30 _ref = processReadArgs($q, arguments), params = _ref.params, cacheDeferred = _ref.deferred;
31 httpDeferred = $q.defer();
32 arrayInstance = new Array();
33 arrayInstance.$promise = cacheDeferred.promise;
34 arrayInstance.$httpPromise = httpDeferred.promise;
35 cacheArrayEntry = new ResourceCacheArrayEntry(CachedResource.$key, params).load();
36 arrayInstance.$httpPromise.then(function(instances) {
37 return cacheArrayEntry.addInstances(instances, false);
38 });
39 readHttp = function() {
40 var resource;
41 resource = CachedResource.$resource[name](params);
42 resource.$promise.then(function(response) {
43 var newArrayInstance;
44 newArrayInstance = new Array();
45 response.map(function(resourceInstance) {
46 var existingInstance;
47 resourceInstance = new CachedResource(resourceInstance);
48 existingInstance = first(arrayInstance, resourceInstance.$params());
49 if (existingInstance) {
50 modifyObjectInPlace(existingInstance, resourceInstance);
51 return newArrayInstance.push(existingInstance);
52 } else {
53 return newArrayInstance.push(resourceInstance);
54 }
55 });
56 arrayInstance.splice.apply(arrayInstance, [0, arrayInstance.length].concat(__slice.call(newArrayInstance)));
57 if (!cacheArrayEntry.value) {
58 cacheDeferred.resolve(arrayInstance);
59 }
60 return httpDeferred.resolve(arrayInstance);
61 });
62 return resource.$promise["catch"](function(error) {
63 if (!cacheArrayEntry.value) {
64 cacheDeferred.reject(error);
65 }
66 return httpDeferred.reject(error);
67 });
68 };
69 if (!actionConfig.cacheOnly) {
70 CachedResource.$writes.flush(readHttp);
71 }
72 if (cacheArrayEntry.value) {
73 _ref1 = cacheArrayEntry.value;
74 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
75 cacheInstanceParams = _ref1[_i];
76 cacheInstanceEntry = new ResourceCacheEntry(CachedResource.$key, cacheInstanceParams).load();
77 arrayInstance.push(new CachedResource(cacheInstanceEntry.value));
78 }
79 cacheDeferred.resolve(arrayInstance);
80 } else if (actionConfig.cacheOnly) {
81 cacheDeferred.reject(new Error("Cache value does not exist for params", params));
82 }
83 return arrayInstance;
84 };
85};