1 | import IterResult from './iterresult';
|
2 | import dateutil from './dateutil';
|
3 | import { isArray } from './helpers';
|
4 | var Cache = (function () {
|
5 | function Cache() {
|
6 | this.all = false;
|
7 | this.before = [];
|
8 | this.after = [];
|
9 | this.between = [];
|
10 | }
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | Cache.prototype._cacheAdd = function (what, value, args) {
|
17 | if (value) {
|
18 | value =
|
19 | value instanceof Date
|
20 | ? dateutil.clone(value)
|
21 | : dateutil.cloneDates(value);
|
22 | }
|
23 | if (what === 'all') {
|
24 | this.all = value;
|
25 | }
|
26 | else {
|
27 | args._value = value;
|
28 | this[what].push(args);
|
29 | }
|
30 | };
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | Cache.prototype._cacheGet = function (what, args) {
|
39 | var cached = false;
|
40 | var argsKeys = args ? Object.keys(args) : [];
|
41 | var findCacheDiff = function (item) {
|
42 | for (var i = 0; i < argsKeys.length; i++) {
|
43 | var key = argsKeys[i];
|
44 | if (String(args[key]) !== String(item[key])) {
|
45 | return true;
|
46 | }
|
47 | }
|
48 | return false;
|
49 | };
|
50 | var cachedObject = this[what];
|
51 | if (what === 'all') {
|
52 | cached = this.all;
|
53 | }
|
54 | else if (isArray(cachedObject)) {
|
55 |
|
56 |
|
57 | for (var i = 0; i < cachedObject.length; i++) {
|
58 | var item = cachedObject[i];
|
59 | if (argsKeys.length && findCacheDiff(item))
|
60 | continue;
|
61 | cached = item._value;
|
62 | break;
|
63 | }
|
64 | }
|
65 | if (!cached && this.all) {
|
66 |
|
67 |
|
68 | var iterResult = new IterResult(what, args);
|
69 | for (var i = 0; i < this.all.length; i++) {
|
70 | if (!iterResult.accept(this.all[i]))
|
71 | break;
|
72 | }
|
73 | cached = iterResult.getValue();
|
74 | this._cacheAdd(what, cached, args);
|
75 | }
|
76 | return isArray(cached)
|
77 | ? dateutil.cloneDates(cached)
|
78 | : cached instanceof Date
|
79 | ? dateutil.clone(cached)
|
80 | : cached;
|
81 | };
|
82 | return Cache;
|
83 | }());
|
84 | export { Cache };
|
85 |
|
\ | No newline at end of file |