resourcerer 1.2.2 → 2.0.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -164
- package/build/index.d.ts +9 -0
- package/build/index.js +9 -0
- package/build/lib/collection.d.ts +107 -0
- package/build/lib/collection.js +251 -0
- package/build/lib/config.d.ts +20 -0
- package/build/lib/config.js +23 -0
- package/build/lib/error-boundary.d.ts +12 -0
- package/build/lib/error-boundary.js +18 -0
- package/build/lib/events.d.ts +11 -0
- package/build/lib/events.js +12 -0
- package/build/lib/model-cache.d.ts +13 -0
- package/build/lib/model-cache.js +62 -0
- package/build/lib/model.d.ts +48 -0
- package/build/lib/model.js +170 -0
- package/build/lib/prefetch.d.ts +3 -0
- package/build/lib/prefetch.js +27 -0
- package/build/lib/request.d.ts +20 -0
- package/build/lib/request.js +53 -0
- package/build/lib/resourcerer.d.ts +13 -0
- package/build/lib/resourcerer.js +406 -0
- package/build/lib/sync.d.ts +18 -0
- package/build/lib/sync.js +58 -0
- package/build/lib/types.d.ts +66 -0
- package/build/lib/types.js +1 -0
- package/build/lib/utils.d.ts +14 -0
- package/build/lib/utils.js +87 -0
- package/docs/advanced_topics.md +9 -77
- package/docs/collection.md +28 -28
- package/docs/migrating_to_2.0.md +72 -0
- package/docs/model.md +21 -21
- package/docs/typescript.md +68 -20
- package/package.json +27 -30
- package/resourcerer.d.ts +50 -0
- package/test/collection.test.js +305 -316
- package/test/config.test.js +21 -75
- package/test/events.test.js +42 -41
- package/test/model-cache.test.js +99 -77
- package/test/model.test.js +204 -209
- package/test/prefetch.test.js +35 -61
- package/test/request.test.js +104 -109
- package/test/sync.test.js +132 -107
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -269
- package/index.js +0 -14
- package/lib/collection.js +0 -584
- package/lib/config.js +0 -131
- package/lib/constants.js +0 -6
- package/lib/error-boundary.js +0 -47
- package/lib/events.js +0 -19
- package/lib/model-cache.js +0 -131
- package/lib/model.js +0 -372
- package/lib/prefetch.js +0 -59
- package/lib/request.js +0 -126
- package/lib/resourcerer.js +0 -963
- package/lib/sync.js +0 -94
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/lib/collection.js
DELETED
|
@@ -1,584 +0,0 @@
|
|
|
1
|
-
import {isDeepEqual, sortBy} from './utils.js';
|
|
2
|
-
|
|
3
|
-
import Events from './events.js';
|
|
4
|
-
import Model from './model.js';
|
|
5
|
-
import sync from './sync.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* A Collection instance represents a list of items in a RESTful sense. Each item returned at the
|
|
9
|
-
* GET endpoint will be instantiated as a Model instance in the collection's `models` property. When
|
|
10
|
-
* an update happens on a model, its events get filtered up to the collection, making it only
|
|
11
|
-
* necessary to ever listen on the collection itself, which is what resourcerer does.
|
|
12
|
-
*
|
|
13
|
-
* The Collection's responsibilities largely lie around managing its models. But it's important to
|
|
14
|
-
* note that all write requests occur on the model, not the collection. It has a .fetch() method
|
|
15
|
-
* for GETting it's items from the server, but its .create() method is really a small wrapper around
|
|
16
|
-
* Model#save that adds it to the collection when it is finished.
|
|
17
|
-
*/
|
|
18
|
-
export default class Collection {
|
|
19
|
-
/**
|
|
20
|
-
* @param {object[]} models - initial models to be set on the collection
|
|
21
|
-
* @param {object} options - options map used in .set(), like {parse: true} to run the models
|
|
22
|
-
* through the parse method and {silent: true} to not trigger any updates. in the default
|
|
23
|
-
* constructor, use:
|
|
24
|
-
*
|
|
25
|
-
* * Model {Model} - dynamically overrides the static model property for the type of Model that
|
|
26
|
-
* this collection's models should be instances of
|
|
27
|
-
* * comparator {function|string} - dynamically overrides the static comparator property used to
|
|
28
|
-
* sort the collection
|
|
29
|
-
*/
|
|
30
|
-
constructor(models, options={}) {
|
|
31
|
-
const RESERVED_OPTION_KEYS = ['Model', 'comparator', 'silent', 'parse'];
|
|
32
|
-
|
|
33
|
-
this.Model = options.Model || this._getModelClass();
|
|
34
|
-
this.comparator = options.comparator || this.constructor.comparator;
|
|
35
|
-
|
|
36
|
-
this._reset();
|
|
37
|
-
|
|
38
|
-
this.urlOptions = Object.keys(options).reduce((memo, key) => Object.assign(
|
|
39
|
-
memo,
|
|
40
|
-
!RESERVED_OPTION_KEYS.includes(key) ? {[key]: options[key]} : {}
|
|
41
|
-
), {});
|
|
42
|
-
|
|
43
|
-
if (models) {
|
|
44
|
-
// silent for completeness, i guess. but a nothing triggered here because it's impossible for
|
|
45
|
-
// a listener to have been attached at this point
|
|
46
|
-
this.reset(models, {silent: true, ...options});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The default model for a collection is just a Model, but this can be overridden by any other
|
|
52
|
-
* custom Model subclass.
|
|
53
|
-
*/
|
|
54
|
-
static Model = Model
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Defines the property by which we can uniquely identify models in the collection. Override this
|
|
58
|
-
* if you're not defining your own custom Model class but still need to index by a different field
|
|
59
|
-
* than the default ('id').
|
|
60
|
-
*/
|
|
61
|
-
static modelIdAttribute = null
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* This is a list of keys (could be attribute keys, but also keys passed in from the options
|
|
65
|
-
* object when instantiated) that resourcerer should take into account when determining how to
|
|
66
|
-
* cache a model and consider models to be identical. Can also be a function that returns an
|
|
67
|
-
* object.
|
|
68
|
-
*/
|
|
69
|
-
static dependencies = []
|
|
70
|
-
|
|
71
|
-
// deprecated
|
|
72
|
-
static cacheFields = []
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Use this to tell resourcerer to track this collection's request time via the `track` method
|
|
76
|
-
* added in the resourcerer configuration file. This can be a boolean or a function that returns a
|
|
77
|
-
* boolean. If the latter, it takes a the resource config object as an argument.
|
|
78
|
-
*/
|
|
79
|
-
static measure = false
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Similar to the method for an individual model, this maps through each model in the collection
|
|
83
|
-
* and returns its data attributes.
|
|
84
|
-
*
|
|
85
|
-
* @return {object[]} list of all models' data representations
|
|
86
|
-
*/
|
|
87
|
-
toJSON() {
|
|
88
|
-
return this.map((model) => model.toJSON());
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Proxies the `sync` module by default, but this can be overridden for custom behavior.
|
|
93
|
-
*/
|
|
94
|
-
sync(...args) {
|
|
95
|
-
return sync.call(this, ...args);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Adds a model or models to the collection. If a comparator exists, the collection will be
|
|
100
|
-
* re-sorted, as well. This will also create a reference on the collection to the model and apply
|
|
101
|
-
* listeners on the collection to events on the new model.
|
|
102
|
-
*
|
|
103
|
-
* @param {object|object[]} models - model or list of models to be added to the collection
|
|
104
|
-
* @param {object} options - .set() options hash
|
|
105
|
-
* @return {Collection} collection instance
|
|
106
|
-
*/
|
|
107
|
-
add(models, options={}) {
|
|
108
|
-
return this.set(models, options);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Removes a model or list of models from the collection, which will also remove any references
|
|
113
|
-
* as well as any listeners.
|
|
114
|
-
*
|
|
115
|
-
* @param {object|object[]} models - model or list of models to be removed to the collection
|
|
116
|
-
* @param {object} options - .set() options hash
|
|
117
|
-
* @return {Collection} collection instance
|
|
118
|
-
*/
|
|
119
|
-
remove(models, options={}) {
|
|
120
|
-
var removed = this._removeModels(!Array.isArray(models) ? [models] : models);
|
|
121
|
-
|
|
122
|
-
if (!options.silent && removed.length) {
|
|
123
|
-
// update trigger on collection, necessary because removed models won't trigger collection
|
|
124
|
-
// response at this point
|
|
125
|
-
this.triggerUpdate();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return this;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Sets a model or list of models on the collection, adding new ones and updating existing ones,
|
|
133
|
-
* as appropriate. Note that this is NOT for removing models. That should be done exclusively
|
|
134
|
-
* via the .remove() method (or effectively the .reset() method).
|
|
135
|
-
*
|
|
136
|
-
* @param {object|object[]} models - model or list of models to be set on the collection
|
|
137
|
-
* @param {object} options - map with the following options:
|
|
138
|
-
* * parse {boolean} - whether to run the models through the parse method (both the collection's
|
|
139
|
-
* parse method as well each model's) before setting them.
|
|
140
|
-
* * silent {boolean} - if true, does not trigger an update after setting
|
|
141
|
-
* @return {Collection} collection instance
|
|
142
|
-
*/
|
|
143
|
-
set(models, options={}) {
|
|
144
|
-
var shouldSort = false;
|
|
145
|
-
|
|
146
|
-
if (!models) {
|
|
147
|
-
return this;
|
|
148
|
-
} else if (options.parse && !this._isModel(models)) {
|
|
149
|
-
models = this.parse(models, options) || [];
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// models can be passed as a single model or a list
|
|
153
|
-
models = !Array.isArray(models) ? [models] : models;
|
|
154
|
-
|
|
155
|
-
for (let model of models) {
|
|
156
|
-
// if model already exists, swap in new attributes
|
|
157
|
-
if (this.get(model)) {
|
|
158
|
-
let attrs = this._isModel(model) ? model.attributes : model;
|
|
159
|
-
|
|
160
|
-
this.get(model).set(
|
|
161
|
-
options.parse ?
|
|
162
|
-
this.get(model).parse(attrs, options) :
|
|
163
|
-
attrs
|
|
164
|
-
);
|
|
165
|
-
// otherwise add it to the collection
|
|
166
|
-
} else {
|
|
167
|
-
model = this._prepareModel(model, options);
|
|
168
|
-
|
|
169
|
-
this.models.push(model);
|
|
170
|
-
this._addReference(model);
|
|
171
|
-
shouldSort = true;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// reset the length
|
|
176
|
-
this.length = this.models.length;
|
|
177
|
-
|
|
178
|
-
// sort the collection if appropriate.
|
|
179
|
-
if (this.comparator && shouldSort) {
|
|
180
|
-
this.sort();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (!options.silent) {
|
|
184
|
-
// 'update' trigger on collection
|
|
185
|
-
this.triggerUpdate();
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return this;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Sets a model or list of models on the collection after completely removing all references and
|
|
193
|
-
* listeners to previous models.
|
|
194
|
-
*
|
|
195
|
-
* @param {object|object[]} models - model or list of models to be set on the collection
|
|
196
|
-
* @param {object} options - .set() options hash
|
|
197
|
-
* @return {Collection} collection instance
|
|
198
|
-
*/
|
|
199
|
-
reset(models=[], options={}) {
|
|
200
|
-
for (let i = 0; i < this.models.length; i++) {
|
|
201
|
-
this._removeReference(this.models[i]);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
this._reset();
|
|
205
|
-
// this is silent so that we don't trigger until we are all done. this is extra
|
|
206
|
-
// important after a request returns because as of React 17 those are still synchronous udpates
|
|
207
|
-
this.add(models, {silent: true, ...options});
|
|
208
|
-
|
|
209
|
-
if (!options.silent) {
|
|
210
|
-
// reset trigger
|
|
211
|
-
this.triggerUpdate();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return this;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* When adding a model to a collection, the collection makes shorthand references to it, and it
|
|
219
|
-
* does it both by its idAttribute as well as its client id attribute.
|
|
220
|
-
*
|
|
221
|
-
* @param {string} obj - identifier to lookup a model, which can be its id attribute, its client
|
|
222
|
-
* id, or an object containing either
|
|
223
|
-
* @return {Model} collection's model, if found
|
|
224
|
-
*/
|
|
225
|
-
get(obj) {
|
|
226
|
-
if (!obj && typeof obj !== 'number') {
|
|
227
|
-
return undefined;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return this._byId[obj] ||
|
|
231
|
-
this._byId[(this._isModel(obj) ? obj.attributes : obj)[this.Model.idAttribute]] ||
|
|
232
|
-
obj.cid && this._byId[obj.cid];
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Looks up by id/client id/object containing either whether the collection has a model in its
|
|
237
|
-
* collection.
|
|
238
|
-
*
|
|
239
|
-
* @param {string} obj - identifier to lookup a model, which can be its id attribute, its client
|
|
240
|
-
* id, or an object containing either
|
|
241
|
-
* @return {boolean} whether the model is in the collection
|
|
242
|
-
*/
|
|
243
|
-
has(obj) {
|
|
244
|
-
return ![undefined, null].includes(this.get(obj));
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Retuns a model at the given collection index, which can be negative.
|
|
249
|
-
*
|
|
250
|
-
* @param {number} index
|
|
251
|
-
* @return {Model}
|
|
252
|
-
*/
|
|
253
|
-
at(index) {
|
|
254
|
-
if (index < 0) {
|
|
255
|
-
index += this.length;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return this.models[index];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/** COLLECTION HELPER METHODS */
|
|
262
|
-
/**
|
|
263
|
-
* @param {function} predicate - mapping function taking each model as an argument
|
|
264
|
-
* @return {any[]}
|
|
265
|
-
*/
|
|
266
|
-
map(predicate) {
|
|
267
|
-
return this.models.map(predicate);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* @param {function} predicate - function taking each model as an argument and returning a boolean
|
|
272
|
-
* @return {Model?} the first model where the predicate returned true
|
|
273
|
-
*/
|
|
274
|
-
find(predicate) {
|
|
275
|
-
return this.models.find(predicate);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* @param {function} predicate - function taking each model as an argument and returning a boolean
|
|
280
|
-
* @return {Model[]} the list of models where the predicate returned true
|
|
281
|
-
*/
|
|
282
|
-
filter(predicate) {
|
|
283
|
-
return this.models.filter(predicate);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* @param {object} attrs - object of properties and values to match models against
|
|
288
|
-
* @return {Model?} the first model found with the matched properties in `attrs`
|
|
289
|
-
*/
|
|
290
|
-
findWhere(attrs) {
|
|
291
|
-
return this.where(attrs, true);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* @param {object} attrs - object of properties and values to match models against
|
|
296
|
-
* @param {boolean} first - whether to take all matched models or just the first
|
|
297
|
-
* @return {Model[]} list of models found with the matched properties in `attrs`
|
|
298
|
-
*/
|
|
299
|
-
where(attrs, first) {
|
|
300
|
-
var predicate = (model) => {
|
|
301
|
-
for (let [attr, val] of Object.entries(attrs)) {
|
|
302
|
-
if (!isDeepEqual(model.get(attr), val)) {
|
|
303
|
-
return false;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return true;
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
return this[first ? 'find' : 'filter'](predicate);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* @param {string} attr - an attribute to get from each model in the collectionn
|
|
315
|
-
* @return {string[]} list of that attribute's values in the collection
|
|
316
|
-
*/
|
|
317
|
-
pluck(attr) {
|
|
318
|
-
return this.map((model) => model.get(attr));
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* @param {any[]} args - same args for an array's .slice() method
|
|
323
|
-
* @return {Model[]} collection model subset
|
|
324
|
-
*/
|
|
325
|
-
slice(...args) {
|
|
326
|
-
return this.models.slice(...args);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Sorts the collection by its comparator property. In most cases, you shouldn't need to call this
|
|
331
|
-
* method directly; it happens automatically when new models are added.
|
|
332
|
-
*/
|
|
333
|
-
sort() {
|
|
334
|
-
if (!this.comparator) {
|
|
335
|
-
throw new Error('Cannot sort a set without a comparator');
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// Run sort based on type of `comparator`.
|
|
339
|
-
if (typeof this.comparator === 'function' && this.comparator.length > 1) {
|
|
340
|
-
this.models.sort(this.comparator.bind(this));
|
|
341
|
-
} else {
|
|
342
|
-
this.models = sortBy(
|
|
343
|
-
this.models,
|
|
344
|
-
(model) => typeof this.comparator === 'function' ?
|
|
345
|
-
this.comparator(model) :
|
|
346
|
-
model.get(this.comparator)
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
return this;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Main method that preps a GET request at this collection's url. This is the method the request
|
|
355
|
-
* module uses to sync server data after instantiating a collection. Upon returning, an update is
|
|
356
|
-
* triggered for all registered components.
|
|
357
|
-
*
|
|
358
|
-
* @param {object} options - can include any property used by the sync module
|
|
359
|
-
* @return {promise} - resolves with a tuple of the instance and response object
|
|
360
|
-
*/
|
|
361
|
-
fetch(options={}) {
|
|
362
|
-
options = {parse: true, method: 'GET', ...options};
|
|
363
|
-
|
|
364
|
-
return this.sync(this, options)
|
|
365
|
-
.then(([json, response]) => {
|
|
366
|
-
this.reset(json, {silent: true, ...options});
|
|
367
|
-
// sync trigger
|
|
368
|
-
this.triggerUpdate();
|
|
369
|
-
|
|
370
|
-
return [this, response];
|
|
371
|
-
})
|
|
372
|
-
.catch((response) => Promise.reject(response));
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* This method is shorthand for adding a new model to the collection and then calling .save() on
|
|
377
|
-
* it.
|
|
378
|
-
*
|
|
379
|
-
* @param {object|Model} model - new model to be added to the collection and synced with the
|
|
380
|
-
* server
|
|
381
|
-
* @param {object} options - can include any property used by the sync module. pass {wait: true}
|
|
382
|
-
* to wait to add the model to the collection until after the save request succeeds
|
|
383
|
-
* @return {promise} - resolves with a tuple of the instance and response object
|
|
384
|
-
*/
|
|
385
|
-
create(model, options={}) {
|
|
386
|
-
model = this._prepareModel(model, options);
|
|
387
|
-
|
|
388
|
-
if (!options.wait) {
|
|
389
|
-
this.add(model, options);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
return model.save(null, options)
|
|
393
|
-
.then((...args) => {
|
|
394
|
-
if (options.wait) {
|
|
395
|
-
// note that this is NOT silent because even though we are already triggering an update
|
|
396
|
-
// after the model sync, because the model isn't added, the collection doesn't also get
|
|
397
|
-
// an update
|
|
398
|
-
this.add(model, options);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// model should now have an id property if it didn't previously
|
|
402
|
-
this._addReference(model);
|
|
403
|
-
|
|
404
|
-
return args[0];
|
|
405
|
-
})
|
|
406
|
-
.catch((response) => {
|
|
407
|
-
if (!options.wait) {
|
|
408
|
-
this.remove(model);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return Promise.reject(response);
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* By default, parse() is the identity function. Override this if you need special business logic
|
|
417
|
-
* to transform the server response into a different form for your application that will be set
|
|
418
|
-
* as models on the collection.
|
|
419
|
-
*
|
|
420
|
-
* @param {any} response - server response data
|
|
421
|
-
* @param {object} options - options map
|
|
422
|
-
* @return {object} data object transformed from the server response to be applied as the
|
|
423
|
-
* collections' models' data attributes
|
|
424
|
-
*/
|
|
425
|
-
parse(response, options) {
|
|
426
|
-
return response;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
* This function gets called when the collection is instantiated, and its return value is set to
|
|
431
|
-
* its Model property (and used to instantiate all its models). It's only used if a `Model`
|
|
432
|
-
* option isn't passed to the constructor, and by default it returns the Model base class. But
|
|
433
|
-
* if the collection has a `modelIdAttribute` static property, it will create a new Model class
|
|
434
|
-
* with the corresponding `idAttribute` property and return that.
|
|
435
|
-
*
|
|
436
|
-
* @return {Model} model class associated with the collection
|
|
437
|
-
*/
|
|
438
|
-
_getModelClass() {
|
|
439
|
-
if (this.constructor.modelIdAttribute) {
|
|
440
|
-
let attr = this.constructor.modelIdAttribute;
|
|
441
|
-
|
|
442
|
-
return class extends Model {
|
|
443
|
-
static idAttribute = attr
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
return this.constructor.Model;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Private method to reset all internal state. Called when the collection
|
|
452
|
-
* is first initialized or reset.
|
|
453
|
-
*/
|
|
454
|
-
_reset() {
|
|
455
|
-
this.length = 0;
|
|
456
|
-
this.models = [];
|
|
457
|
-
this._byId = {};
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* Turns a set of attributes into an instance of the collection's model. If it is already a model,
|
|
462
|
-
* it assigns the collection instance to the model's `collection` property and returns the model.
|
|
463
|
-
*
|
|
464
|
-
* @param {object|Model} attrs - attributes to turn into a model
|
|
465
|
-
* @param {object} options - options to be passed to the model's constructor
|
|
466
|
-
* @return {Model}
|
|
467
|
-
*/
|
|
468
|
-
_prepareModel(attrs, options) {
|
|
469
|
-
if (this._isModel(attrs)) {
|
|
470
|
-
if (!attrs.collection) {
|
|
471
|
-
attrs.collection = this;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return attrs;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
return new this.Model(attrs, {...this.urlOptions, ...options, collection: this});
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Internal method called by .remove() that, in addition to taking models out of the collection,
|
|
482
|
-
* also removes their references in the collection as well as their listeners.
|
|
483
|
-
*
|
|
484
|
-
* @param {Model[]} models - list of models to be removed from the collection
|
|
485
|
-
* @return {Model[]} list of models actually removed, since they won't get removed if they don't
|
|
486
|
-
* exist in the collection in the first place
|
|
487
|
-
*/
|
|
488
|
-
_removeModels(models) {
|
|
489
|
-
var removed = [];
|
|
490
|
-
|
|
491
|
-
for (let i = 0; i < models.length; i++) {
|
|
492
|
-
let model = this.get(models[i]);
|
|
493
|
-
|
|
494
|
-
if (!model) {
|
|
495
|
-
continue;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
let index = this.models.indexOf(model);
|
|
499
|
-
|
|
500
|
-
this.models.splice(index, 1);
|
|
501
|
-
this.length--;
|
|
502
|
-
|
|
503
|
-
delete this._byId[model.cid];
|
|
504
|
-
|
|
505
|
-
if (model.attributes[this.Model.idAttribute]) {
|
|
506
|
-
delete this._byId[model.attributes[this.Model.idAttribute]];
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
removed.push(model);
|
|
510
|
-
this._removeReference(model);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
return removed;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* Helper method to determine whether an object is a Model.
|
|
518
|
-
*
|
|
519
|
-
* @param {any} model - object in question
|
|
520
|
-
* @return {boolean} whether the object is an instance of Model
|
|
521
|
-
*/
|
|
522
|
-
_isModel(model) {
|
|
523
|
-
return model instanceof Model;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
/**
|
|
527
|
-
* Internal method used by .set() when adding a model to a collection. Adds reference properties
|
|
528
|
-
* on the collection for direct access to the model and subscribes the collection to model
|
|
529
|
-
* updates. This allows components to listen only on a collection and not an individual model and
|
|
530
|
-
* still see the updates they expect.
|
|
531
|
-
*
|
|
532
|
-
* @param {Model} model
|
|
533
|
-
*/
|
|
534
|
-
_addReference(model) {
|
|
535
|
-
this._byId[model.cid] = model;
|
|
536
|
-
|
|
537
|
-
let id = model.attributes[this.Model.idAttribute];
|
|
538
|
-
|
|
539
|
-
if (id || typeof id === 'number') {
|
|
540
|
-
this._byId[id] = model;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
model.onUpdate(this.triggerUpdate, this);
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
/**
|
|
547
|
-
* Internal method used by .remove() when removing a model from a collection. Basically the
|
|
548
|
-
* inverse of _addReference: it removes reference properties on the collection and unsubscribes
|
|
549
|
-
* listeners on model updates.
|
|
550
|
-
*
|
|
551
|
-
* @param {Model} model
|
|
552
|
-
*/
|
|
553
|
-
_removeReference(model) {
|
|
554
|
-
delete this._byId[model.cid];
|
|
555
|
-
|
|
556
|
-
let id = model.attributes[this.Model.idAttribute];
|
|
557
|
-
|
|
558
|
-
if (id) {
|
|
559
|
-
delete this._byId[id];
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
delete model.collection;
|
|
563
|
-
|
|
564
|
-
model.offUpdate(this);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
/**
|
|
568
|
-
* Used by models when they are given a new id, which should, frankly, rarely happen. Removes the
|
|
569
|
-
* old reference and adds the new one so that `.get()` still works.
|
|
570
|
-
*
|
|
571
|
-
* @param {string} id - the new id to make a reference with
|
|
572
|
-
* @param {string} prevId - the old id to remove
|
|
573
|
-
* @param {Model} model - Model instance for new reference
|
|
574
|
-
*/
|
|
575
|
-
_updateModelReference(id, prevId, model) {
|
|
576
|
-
if (id) {
|
|
577
|
-
delete this._byId[prevId];
|
|
578
|
-
this._byId[id] = model;
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
// mix in events to the Collection
|
|
584
|
-
Object.assign(Collection.prototype, Events);
|
package/lib/config.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import {camelize, noOp} from './utils.js';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import {setRequestPrefilter} from './sync.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* This module contains all the required setup for implementing withResources
|
|
7
|
-
* within an application.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* ResourceKeys {{string: string}}: an object that should contain a key for each
|
|
12
|
-
* model whose value is a string, ie: `USER: 'user'` for a userModel. The
|
|
13
|
-
* string becomes the prefix for all props related to this resource, for
|
|
14
|
-
* example, props.userModel and props.userLoadingState. These keys are passed
|
|
15
|
-
* as the second argument to the withResources function and are used to
|
|
16
|
-
* declaritively request a model for a component.
|
|
17
|
-
*/
|
|
18
|
-
export const ResourceKeys = {
|
|
19
|
-
/**
|
|
20
|
-
* @param {{string: string}} keys - resource keys to assign to the
|
|
21
|
-
* ResourceKeys configuration object
|
|
22
|
-
*/
|
|
23
|
-
add(keys) {
|
|
24
|
-
return Object.assign(this, keys);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* ModelMap {{string: Model|Collection}}: an object that
|
|
30
|
-
* should link the key established in ResourceKeys to Model/Collection
|
|
31
|
-
* constructors, ie: `[ResourceKeys.USER]: UserModel`. Entries in ResourceKeys
|
|
32
|
-
* and ModelMap for a resource are required in to use a resource with
|
|
33
|
-
* withResources.
|
|
34
|
-
*/
|
|
35
|
-
export const ModelMap = {
|
|
36
|
-
/**
|
|
37
|
-
* @param {{string: Model|Collection}} models - an object of
|
|
38
|
-
* ResourceKeys as keys and Models as values
|
|
39
|
-
*/
|
|
40
|
-
add(models) {
|
|
41
|
-
Object.keys(models).forEach((key) => {
|
|
42
|
-
// for backwards-compatibility, we auto-add the key only if the key
|
|
43
|
-
// doesn't exist as a property on the ResourceKeys object (as a string in
|
|
44
|
-
// KEY_FORM) and it also doesn't exist as a value on the ResourceKeys
|
|
45
|
-
// object (in camelCase form)
|
|
46
|
-
if (!ResourceKeys[key] && !Object.values(ResourceKeys).includes(key)) {
|
|
47
|
-
let camelKey = camelize(key);
|
|
48
|
-
|
|
49
|
-
// auto-add to resource keys with a camelized version of the key for its
|
|
50
|
-
// prop prefix string, then add the model to the same key, removing its
|
|
51
|
-
// passed key
|
|
52
|
-
ResourceKeys.add({[key]: camelKey});
|
|
53
|
-
|
|
54
|
-
if (key !== camelKey) {
|
|
55
|
-
models[camelKey] = models[key];
|
|
56
|
-
delete models[key];
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
return Object.assign(this, models);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* UnfetchedResources {ResourceKeys[]}: a Set of ResourceKeys that, when
|
|
67
|
-
* declaratively requested, should not be fetched because they are known to
|
|
68
|
-
* have been provided by the response of a parent resource. For use with the
|
|
69
|
-
* `providesModels` static property on a Model.
|
|
70
|
-
*/
|
|
71
|
-
export const UnfetchedResources = new Set();
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* ResourcesConfig {object}: A general config object for a limited amount of
|
|
75
|
-
* customization with the withResources library.
|
|
76
|
-
*/
|
|
77
|
-
export const ResourcesConfig = {
|
|
78
|
-
/**
|
|
79
|
-
* {number}: milliseconds to keep a resource in the cache after the last
|
|
80
|
-
* registered component is unmounted. Default 2 minutes.
|
|
81
|
-
*/
|
|
82
|
-
cacheGracePeriod: 120000,
|
|
83
|
-
/**
|
|
84
|
-
* {React.Element}: Component to display when a withResources ErrorBoundary
|
|
85
|
-
* catches an error.
|
|
86
|
-
*/
|
|
87
|
-
errorBoundaryChild: React.createElement(
|
|
88
|
-
'div',
|
|
89
|
-
{className: 'caught-error'},
|
|
90
|
-
React.createElement('p', null, 'An error occurred.')
|
|
91
|
-
),
|
|
92
|
-
/** {function}: Hook to send errors to a logging service. */
|
|
93
|
-
log: noOp,
|
|
94
|
-
/** {function}: Calls setRequestPrefilter */
|
|
95
|
-
prefilter: noOp,
|
|
96
|
-
/**
|
|
97
|
-
* {string}: the name of the prop object that will contain url query
|
|
98
|
-
* parameters. This object gets spread to flatten them as individual props
|
|
99
|
-
* within a withResources client. default 'urlParams'. You can ignore this
|
|
100
|
-
* if you flatten them yourselves.
|
|
101
|
-
*/
|
|
102
|
-
queryParamsPropName: 'urlParams',
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Method that should be used to stringify GET requests. By default, this uses URLSearchParams,
|
|
106
|
-
* but that won't handle complex values (arrays and objects). To add support for that, override
|
|
107
|
-
* this method with any logic or stringify library you want.
|
|
108
|
-
*
|
|
109
|
-
* @param {object} params - request params
|
|
110
|
-
* @param {object} options - request options map
|
|
111
|
-
* @return {string} URL query parameter string
|
|
112
|
-
*/
|
|
113
|
-
stringify(params, options) {
|
|
114
|
-
return new URLSearchParams(params).toString();
|
|
115
|
-
},
|
|
116
|
-
|
|
117
|
-
/** {function}: Hook to send resource request times to a tracking service. */
|
|
118
|
-
track: noOp,
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* @param {object} config - config object with config overrides to add to the
|
|
122
|
-
* ResourcesConfig configuration object
|
|
123
|
-
*/
|
|
124
|
-
set(config) {
|
|
125
|
-
if (config.prefilter) {
|
|
126
|
-
setRequestPrefilter(config.prefilter);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return Object.assign(this, config);
|
|
130
|
-
}
|
|
131
|
-
};
|