-
Notifications
You must be signed in to change notification settings - Fork 83
/
Memory.js
253 lines (226 loc) · 7.27 KB
/
Memory.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
define([
'dojo/_base/declare',
'dojo/_base/lang',
'dojo/_base/array',
'./Store',
'./Promised',
'./SimpleQuery',
'./QueryResults'
], function (declare, lang, arrayUtil, Store, Promised, SimpleQuery, QueryResults) {
// module:
// dstore/Memory
return declare([Store, Promised, SimpleQuery ], {
constructor: function () {
// summary:
// Creates a memory object store.
// options: dstore/Memory
// This provides any configuration information that will be mixed into the store.
// This should generally include the data property to provide the starting set of data.
// Add a version property so subcollections can detect when they're using stale data
this.storage.version = 0;
},
postscript: function () {
this.inherited(arguments);
// Set the data in `postscript` so subclasses can override `data` in their constructors
// (e.g., a LocalStorage store that retrieves its data from localStorage)
this.setData(this.data || []);
},
// data: Array
// The array of all the objects in the memory store
data: null,
autoEmitEvents: false, // this is handled by the methods themselves
getSync: function (id) {
// summary:
// Retrieves an object by its identity
// id: Number
// The identity to use to lookup the object
// returns: Object
// The object in the store that matches the given id.
return this.storage.fullData[this.storage.index[id]];
},
putSync: function (object, options) {
// summary:
// Stores an object
// object: Object
// The object to store.
// options: dstore/Store.PutDirectives?
// Additional metadata for storing the data. Includes an 'id'
// property if a specific id is to be used.
// returns: Number
options = options || {};
var storage = this.storage,
index = storage.index,
data = storage.fullData;
var Model = this.Model;
if (Model && !(object instanceof Model)) {
// if it is not the correct type, restore a
// properly typed version of the object. Note that we do not allow
// mutation here
object = this._restore(object);
}
var id = this.getIdentity(object);
if (id == null) {
this._setIdentity(object, ('id' in options) ? options.id : Math.random());
id = this.getIdentity(object);
}
storage.version++;
var eventType = id in index ? 'update' : 'add',
event = { target: object },
previousIndex,
defaultDestination,
destination;
if (eventType === 'update') {
if (options.overwrite === false) {
throw new Error('Object already exists');
} else {
previousIndex = index[id];
defaultDestination = previousIndex;
}
} else {
defaultDestination = this.defaultNewToStart ? 0 : data.length;
}
if ('beforeId' in options) {
var beforeId = options.beforeId;
if (beforeId === null) {
destination = data.length;
if (eventType === 'update') {
--destination;
}
} else {
destination = index[beforeId];
// Account for the removed item
if (previousIndex < destination) {
--destination;
}
}
if (destination !== undefined) {
event.beforeId = beforeId;
} else {
console.error('options.beforeId was specified but no corresponding index was found');
destination = defaultDestination;
}
} else {
destination = defaultDestination;
}
if (destination === previousIndex) {
data[destination] = object;
}
else {
if (previousIndex !== undefined) {
data.splice(previousIndex, 1);
}
data.splice(destination, 0, object);
// Items have been reordered so the index needs to be updated
var i = previousIndex === undefined ? destination : Math.min(previousIndex, destination);
for (var l = data.length; i < l; ++i) {
index[this.getIdentity(data[i])] = i;
}
}
this.emit(eventType, event);
return object;
},
addSync: function (object, options) {
// summary:
// Creates an object, throws an error if the object already exists
// object: Object
// The object to store.
// options: dstore/Store.PutDirectives?
// Additional metadata for storing the data. Includes an 'id'
// property if a specific id is to be used.
// returns: Number
(options = options || {}).overwrite = false;
// call put with overwrite being false
return this.putSync(object, options);
},
removeSync: function (id) {
// summary:
// Deletes an object by its identity
// id: Number
// The identity to use to delete the object
// returns: Boolean
// Returns true if an object was removed, falsy (undefined) if no object matched the id
var storage = this.storage;
var index = storage.index;
var data = storage.fullData;
if (id in index) {
var removed = data.splice(index[id], 1)[0];
// now we have to reindex
this._reindex();
this.emit('delete', {id: id, target: removed});
return true;
}
},
setData: function (data) {
// summary:
// Sets the given data as the source for this store, and indexes it
// data: Object[]
// An array of objects to use as the source of data. Note that this
// array will not be copied, it is used directly and mutated as
// data changes.
if (this.parse) {
data = this.parse(data);
}
if (data.items) {
// just for convenience with the data format ItemFileReadStore expects
this.idProperty = data.identifier || this.idProperty;
data = data.items;
}
var storage = this.storage;
storage.fullData = this.data = data;
this._reindex();
},
_reindex: function () {
var storage = this.storage;
var index = storage.index = {};
var data = storage.fullData;
var Model = this.Model;
var ObjectPrototype = Object.prototype;
for (var i = 0, l = data.length; i < l; i++) {
var object = data[i];
if (Model && !(object instanceof Model)) {
var restoredObject = this._restore(object,
// only allow mutation if it is a plain object
// (which is generally the expected input),
// if "typed" objects are actually passed in, we will
// respect that, and leave the original alone
object.__proto__ === ObjectPrototype);
if (object !== restoredObject) {
// a new object was generated in the restoration process,
// so we have to update the item in the data array.
data[i] = object = restoredObject;
}
}
index[this.getIdentity(object)] = i;
}
storage.version++;
},
fetchSync: function () {
var data = this.data;
if (!data || data._version !== this.storage.version) {
// our data is absent or out-of-date, so we requery from the root
// start with the root data
data = this.storage.fullData;
var queryLog = this.queryLog;
// iterate through the query log, applying each querier
for (var i = 0, l = queryLog.length; i < l; i++) {
data = queryLog[i].querier(data);
}
// store it, with the storage version stamp
data._version = this.storage.version;
this.data = data;
}
return new QueryResults(data);
},
fetchRangeSync: function (kwArgs) {
var data = this.fetchSync(),
start = kwArgs.start,
end = kwArgs.end;
return new QueryResults(data.slice(start, end), {
totalLength: data.length
});
},
_includePropertyInSubCollection: function (name) {
return name !== 'data' && this.inherited(arguments);
}
});
});