-
Notifications
You must be signed in to change notification settings - Fork 83
/
Cache.js
217 lines (209 loc) · 7.21 KB
/
Cache.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
define([
'dojo/_base/array',
'dojo/when',
'dojo/_base/declare',
'dojo/_base/lang',
'./Store',
'./Memory',
'./QueryResults'
], function (arrayUtil, when, declare, lang, Store, Memory, QueryResults) {
// module:
// dstore/Cache
function cachingQuery(type) {
// ensure querying creates a parallel caching store query
return function () {
var subCollection = this.inherited(arguments);
var cachingCollection = this.cachingCollection || this.cachingStore;
subCollection.cachingCollection = cachingCollection[type].apply(cachingCollection, arguments);
subCollection.isValidFetchCache = this.canCacheQuery === true || this.canCacheQuery(type, arguments);
return subCollection;
};
}
function init (store) {
if (!store.cachingStore) {
store.cachingStore = new Memory();
}
store.cachingStore.Model = store.Model;
store.cachingStore.idProperty = store.idProperty;
}
var CachePrototype = {
cachingStore: null,
constructor: function () {
init(this);
},
canCacheQuery: function (method, args) {
// summary:
// Indicates if a queried (filter, sort, etc.) collection should using caching
return false;
},
isAvailableInCache: function () {
// summary:
// Indicates if the collection's cachingCollection is a viable source
// for a fetch
return (this.isValidFetchCache && (this.allLoaded || this.fetchRequest)) ||
this._parent && this._parent.isAvailableInCache();
},
fetch: function () {
return this._fetch(arguments);
},
fetchRange: function () {
return this._fetch(arguments, true);
},
_fetch: function (args, isRange) {
// if the data is available in the cache (via any parent), we use fetch from the caching store
var cachingStore = this.cachingStore;
var cachingCollection = this.cachingCollection || cachingStore;
var store = this;
var available = this.isAvailableInCache();
if (available) {
return new QueryResults(when(available, function () {
// need to double check to make sure the flag hasn't been cleared
// and we really have all data loaded
if (store.isAvailableInCache()) {
return isRange ?
cachingCollection.fetchRange(args[0]) :
cachingCollection.fetch();
} else {
return store.inherited(args);
}
}));
}
var results = this.fetchRequest = this.inherited(args);
when(results, function (results) {
var allLoaded = !isRange;
store.fetchRequest = null;
// store each object before calling the callback
arrayUtil.forEach(results, function (object) {
// store each object before calling the callback
if (!store.isLoaded || store.isLoaded(object)) {
if (cachingStore.putSync) {
// if putSync is available (from the memory store),
// use that as it is about 40x faster
cachingStore.putSync(object);
} else {
cachingStore.put(object);
}
} else {
// if anything is not loaded, we can't consider them all loaded
allLoaded = false;
}
});
if (allLoaded) {
store.allLoaded = true;
}
return results;
},function(err) {
store.fetchRequest=null; //reset requesting flag
if(err) {
throw err;
}
throw new Error("Error fetching data from master-store");
});
return results;
},
// TODO: for now, all forEach() calls delegate to fetch(), but that may be different
// with IndexedDB, so we may need to intercept forEach as well (and hopefully not
// double load elements.
// isValidFetchCache: boolean
// This flag indicates if a previous fetch can be used as a cache for subsequent
// fetches (in this collection, or downstream).
isValidFetchCache: false,
get: function (id, directives) {
var cachingStore = this.cachingStore;
var masterGet = this.getInherited(arguments);
var masterStore = this;
// if everything is being loaded, we always wait for that to finish
return when(this.fetchRequest, function () {
return when(cachingStore.get(id), function (result) {
if (result !== undefined) {
return result;
} else if (masterGet) {
return when(masterGet.call(masterStore, id, directives), function (result) {
if (result) {
cachingStore.put(result, {id: id});
}
return result;
});
}
});
});
},
add: function (object, directives) {
var cachingStore = this.cachingStore;
return when(this.inherited(arguments), function (result) {
// now put result in cache (note we don't do add, because add may have
// called put() and already added it)
var cachedPutResult =
cachingStore.put(result && typeof result === 'object' ? result : object, directives);
// the result from the add should be dictated by the master store and be unaffected by the cachingStore,
// unless the master store doesn't implement add
return result || cachedPutResult;
});
},
put: function (object, directives) {
// first update the cache, we are going to assume the update is valid while we wait to
// hear from the master store
var cachingStore = this.cachingStore;
cachingStore.put(object, directives);
return when(this.inherited(arguments)).then(function (result) {
// now put result in cache
var cachedPutResult =
cachingStore.put(result && typeof result === 'object' ? result : object, directives);
// the result from the put should be dictated by the master store and be unaffected by the cachingStore,
// unless the master store doesn't implement put
return result || cachedPutResult;
}, function (exception) {
// Assuming a rejection of a promise invalidates the local cache
cachingStore.remove((directives && directives.id) || cachingStore.getIdentity(object));
if(exception) {
throw exception;
}
throw new Error("Failed to put() object to master-store");
});
},
remove: function (id, directives) {
var cachingStore = this.cachingStore;
return when(this.inherited(arguments), function (result) {
return when(cachingStore.remove(id, directives), function () {
return result;
});
});
},
evict: function (id) {
// summary:
// Evicts an object from the cache
// any eviction means that we don't have everything loaded anymore
this.allLoaded = false;
return this.cachingStore.remove(id);
},
invalidate: function () {
// summary:
// Invalidates this collection's cache as being a valid source of
// future fetches
this.allLoaded = false;
},
_createSubCollection: function () {
var subCollection = this.inherited(arguments);
subCollection._parent = this;
return subCollection;
},
sort: cachingQuery('sort'),
filter: cachingQuery('filter'),
select: cachingQuery('select'),
_getQuerierFactory: function (type) {
var cachingStore = this.cachingStore;
return this.inherited(arguments) || lang.hitch(cachingStore, cachingStore._getQuerierFactory(type));
}
};
var Cache = declare(null, CachePrototype);
Cache.create = function (target, properties) {
// create a delegate of an existing store with caching
// functionality mixed in
target = declare.safeMixin(lang.delegate(target), CachePrototype);
declare.safeMixin(target, properties);
// we need to initialize it since the constructor won't have been called
init(target);
return target;
};
return Cache;
});