resourcerer 1.2.3 → 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 +133 -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 +122 -109
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -270
- 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 -985
- package/lib/sync.js +0 -111
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/lib/constants.js
DELETED
package/lib/error-boundary.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import {omit} from './utils.js';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import {ResourcesConfig} from './config.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* As of React 16, Error Boundaries (components with a componentDidCatch method)
|
|
7
|
-
* allow component trees to gracefully fail when an unexpected error is caught
|
|
8
|
-
* somewhere else in the application. A convenient place to use one is around
|
|
9
|
-
* our withResources wrapped components, where any errors can bubble up and
|
|
10
|
-
* allow us to show an error message in its place.
|
|
11
|
-
*
|
|
12
|
-
* The component displayed when an error is caught can be custom configured by
|
|
13
|
-
* using the `setConfig` function in your config file:
|
|
14
|
-
*
|
|
15
|
-
* ```jsx
|
|
16
|
-
* import {setConfig} from 'resourcerer/config';
|
|
17
|
-
*
|
|
18
|
-
* setConfig({errorBoundaryChild: <MyErroredComponent />});
|
|
19
|
-
* ```
|
|
20
|
-
*
|
|
21
|
-
* Note that as of React 16, if these boundaries don't exist at all, an uncaught
|
|
22
|
-
* error will cause the entire application to unmount.
|
|
23
|
-
*/
|
|
24
|
-
export default class ErrorBoundary extends React.Component {
|
|
25
|
-
constructor() {
|
|
26
|
-
super();
|
|
27
|
-
|
|
28
|
-
this.state = {
|
|
29
|
-
/** Whether the error boundary has a bubbled error to catch */
|
|
30
|
-
caughtError: false
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
componentDidCatch(err, info) {
|
|
35
|
-
this.setState({caughtError: true});
|
|
36
|
-
// hook to allow custom error reporting for an application
|
|
37
|
-
ResourcesConfig.log(err, info);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
render() {
|
|
41
|
-
if (this.state.caughtError) {
|
|
42
|
-
return ResourcesConfig.errorBoundaryChild;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return React.cloneElement(this.props.children, omit(this.props, 'children'));
|
|
46
|
-
}
|
|
47
|
-
}
|
package/lib/events.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Very basic events module that gets mixed into the Model and Collection classes. Since all we
|
|
3
|
-
* care about is whether we want to re-render our react components, we don't actually even care
|
|
4
|
-
* about event names, nor event arguments. On every trigger, we're going to fire all callbacks.
|
|
5
|
-
* And when removing the listener, all we need is the context (which is the component).
|
|
6
|
-
*/
|
|
7
|
-
export default {
|
|
8
|
-
triggerUpdate() {
|
|
9
|
-
this._callbacks?.forEach(({callback, context}) => callback.call(context));
|
|
10
|
-
},
|
|
11
|
-
|
|
12
|
-
onUpdate(callback, context) {
|
|
13
|
-
this._callbacks = (this._callbacks || []).concat({callback, context});
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
offUpdate(ctx) {
|
|
17
|
-
this._callbacks = (this._callbacks || []).filter(({context}) => context !== ctx);
|
|
18
|
-
}
|
|
19
|
-
};
|
package/lib/model-cache.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import {ResourcesConfig} from './config.js';
|
|
2
|
-
|
|
3
|
-
// this is where all of our cached resources are stored
|
|
4
|
-
const modelCache = new Map();
|
|
5
|
-
// this map is {{string: Set<React.Component>}} where each property is a cache
|
|
6
|
-
// key. this is how we keep track all components that have requested a resource.
|
|
7
|
-
// when a cache key's set is empty, we schedule that resource for cache removal
|
|
8
|
-
const componentManifest = new Map();
|
|
9
|
-
// resource timeouts for cache removal. we keep references to these so that if,
|
|
10
|
-
// during the cache grace period, another component requests the resource, we
|
|
11
|
-
// can cancel the cache removal.
|
|
12
|
-
const timeouts = {};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* This module holds references to all of our returned resources as well as a
|
|
16
|
-
* set of components that have requested them. Ideally, this module is never
|
|
17
|
-
* used in an application; withResources should handle everything behind the
|
|
18
|
-
* scenes.
|
|
19
|
-
*
|
|
20
|
-
* When a component unmounts, it is removed from the set of components that have
|
|
21
|
-
* requested a resource. If that resource no longer has any components in its
|
|
22
|
-
* set, it is scheduled for cache removal. That time period can be configured
|
|
23
|
-
* with the `cacheGracePeriod` config option with the `setConfig` function.
|
|
24
|
-
*/
|
|
25
|
-
export default {
|
|
26
|
-
get(cacheKey) {
|
|
27
|
-
return modelCache.get(cacheKey);
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Adds a model to the model cache and registers its first component.
|
|
32
|
-
* A component might not exist, for example, while prefetching a resource.
|
|
33
|
-
* In this case, the user may never actually click on the component, so we
|
|
34
|
-
* preemptively schedule for removal. The timeout will be overridden when
|
|
35
|
-
* an additional component is registered to the model.
|
|
36
|
-
*
|
|
37
|
-
* @param {string} cacheKey - The cache key of the model to be removed.
|
|
38
|
-
* @param {Model | Collection} model - model to be cached
|
|
39
|
-
* @param {React.Component} component - component to register to model in the component manifest
|
|
40
|
-
*/
|
|
41
|
-
put(cacheKey, model, component) {
|
|
42
|
-
modelCache.set(cacheKey, model);
|
|
43
|
-
|
|
44
|
-
if (component) {
|
|
45
|
-
this.register(cacheKey, component);
|
|
46
|
-
} else {
|
|
47
|
-
scheduleForRemoval(cacheKey);
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Registers a component with a resource in the component manifest. Clears any
|
|
53
|
-
* currently-scheduled cache removal timeout.
|
|
54
|
-
*
|
|
55
|
-
* @param {string} cacheKey
|
|
56
|
-
* @param {React.Component} component
|
|
57
|
-
*/
|
|
58
|
-
register(cacheKey, component) {
|
|
59
|
-
window.clearTimeout(timeouts[cacheKey]);
|
|
60
|
-
componentManifest.set(cacheKey, componentManifest.get(cacheKey) || new Set());
|
|
61
|
-
componentManifest.get(cacheKey).add(component);
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Unregisters a component from the component manifest. If passed cache keys,
|
|
66
|
-
* the component will only be unregistered from those resources. Otherwise,
|
|
67
|
-
* it will be removed from all resources (ie, when the component unmounts).
|
|
68
|
-
*
|
|
69
|
-
* If, after the component is unregistered, a resource no longer has any
|
|
70
|
-
* components registered, it will be scheduled for removal from the cache.
|
|
71
|
-
*
|
|
72
|
-
* @param {React.Component} component
|
|
73
|
-
* @param {...<string>} cacheKeys - list of keys from which to unregister
|
|
74
|
-
* the component
|
|
75
|
-
*/
|
|
76
|
-
unregister(component, ...cacheKeys) {
|
|
77
|
-
cacheKeys = cacheKeys.length ? cacheKeys : componentManifest.keys();
|
|
78
|
-
|
|
79
|
-
for (let cacheKey of cacheKeys) {
|
|
80
|
-
let componentSet = componentManifest.get(cacheKey);
|
|
81
|
-
|
|
82
|
-
if (componentSet && componentSet.has(component)) {
|
|
83
|
-
componentSet.delete(component);
|
|
84
|
-
|
|
85
|
-
if (!componentSet.size) {
|
|
86
|
-
scheduleForRemoval(cacheKey);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Direct removal of a cache key from the cache. Should be used sparingly,
|
|
94
|
-
* since it shortcuts the unregistration process and timeout.
|
|
95
|
-
*
|
|
96
|
-
* @param {string} cacheKey - The cache key of the model to be removed.
|
|
97
|
-
*/
|
|
98
|
-
remove(cacheKey) {
|
|
99
|
-
clearModel(cacheKey);
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
// internal only
|
|
103
|
-
__removeAll__() {
|
|
104
|
-
modelCache.forEach((val, key) => clearModel(key));
|
|
105
|
-
componentManifest.forEach((val, key) => componentManifest.delete(key));
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Sets a timeout for clearing the model from the model cache and stores the
|
|
111
|
-
* timeout id in the timeouts object.
|
|
112
|
-
*
|
|
113
|
-
* @param {string} cacheKey - The cache key of the model to be removed.
|
|
114
|
-
*/
|
|
115
|
-
function scheduleForRemoval(cacheKey) {
|
|
116
|
-
var timeout = modelCache.get(cacheKey)?.constructor.cacheTimeout ||
|
|
117
|
-
ResourcesConfig.cacheGracePeriod;
|
|
118
|
-
|
|
119
|
-
timeouts[cacheKey] = window.setTimeout(() => clearModel(cacheKey), timeout);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Remove a model from the cache.
|
|
124
|
-
*
|
|
125
|
-
* @param {string} cacheKey - The cache key of the model to be removed.
|
|
126
|
-
*/
|
|
127
|
-
function clearModel(cacheKey) {
|
|
128
|
-
window.clearTimeout(timeouts[cacheKey]);
|
|
129
|
-
delete timeouts[cacheKey];
|
|
130
|
-
modelCache.delete(cacheKey);
|
|
131
|
-
}
|
package/lib/model.js
DELETED
|
@@ -1,372 +0,0 @@
|
|
|
1
|
-
import {isDeepEqual, result, uniqueId, urlError} from './utils.js';
|
|
2
|
-
|
|
3
|
-
import Events from './events.js';
|
|
4
|
-
import sync from './sync.js';
|
|
5
|
-
|
|
6
|
-
const RESERVED_OPTION_KEYS = [
|
|
7
|
-
'Model',
|
|
8
|
-
'comparator',
|
|
9
|
-
'parse',
|
|
10
|
-
'collection',
|
|
11
|
-
'silent',
|
|
12
|
-
'method',
|
|
13
|
-
'unset'
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The Model class should be extended for any resource that is singular, as in not a list of items.
|
|
18
|
-
* It is the core class used to represent server data in the client application (Collections are
|
|
19
|
-
* wrappers around lists of Model instances). Its responsibilities revolve around two aspects:
|
|
20
|
-
*
|
|
21
|
-
* 1. Keeping a reference to server data. This is kept within the `attributes` property, though you
|
|
22
|
-
* should never need to access `attributes` directly. Instead, use .toJSON() to get the full data
|
|
23
|
-
* object, .get() to get a single property, and .set() to set properties.
|
|
24
|
-
* 2. Interfacing with the sync module to make server requests. These happen with the .fetch(),
|
|
25
|
-
* .save(), and .destroy() methods, the former two of which also update its representation using
|
|
26
|
-
* the methods listed in the first step.
|
|
27
|
-
*/
|
|
28
|
-
export default class Model {
|
|
29
|
-
/**
|
|
30
|
-
* @param {object} attributes - initial server data representation to be kept on the model
|
|
31
|
-
* @param {object} options - generic map. in the default constructor, use:
|
|
32
|
-
* url() method
|
|
33
|
-
* * parse {boolean} - if true, runs the initial attributes through the parse function beefore
|
|
34
|
-
* setting on the model
|
|
35
|
-
* * collection {Collection} - links this model to a collection, if applicable
|
|
36
|
-
* * ...any other options that .set() takes
|
|
37
|
-
*/
|
|
38
|
-
constructor(attributes={}, options={}) {
|
|
39
|
-
this.cid = uniqueId('c');
|
|
40
|
-
this.attributes = {};
|
|
41
|
-
|
|
42
|
-
if (options.collection) {
|
|
43
|
-
this.collection = options.collection;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (options.parse) {
|
|
47
|
-
attributes = this.parse(attributes, options) || {};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
this.urlOptions = Object.keys(options).reduce((memo, key) => Object.assign(
|
|
51
|
-
memo,
|
|
52
|
-
!RESERVED_OPTION_KEYS.includes(key) ? {[key]: options[key]} : {}
|
|
53
|
-
), {});
|
|
54
|
-
|
|
55
|
-
this.set({...result(this.constructor, 'defaults'), ...attributes}, options);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* This is the field that collections will use to index models and also to consider models unique.
|
|
60
|
-
* Override this on a Model if appropriate for the indexing to happen on a different field.
|
|
61
|
-
* Regardless, the value at this attribute will be set to model.id, the id field directly on the
|
|
62
|
-
* model (not in attributes).
|
|
63
|
-
*/
|
|
64
|
-
static idAttribute = 'id'
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* This is a list of keys (could be attribute keys, but also keys passed in from the options
|
|
68
|
-
* object when instantiated) that resourcerer should take into account when determining how to
|
|
69
|
-
* cache a model and consider models to be identical. Can also be a function that returns an
|
|
70
|
-
* object.
|
|
71
|
-
*/
|
|
72
|
-
static dependencies = []
|
|
73
|
-
|
|
74
|
-
// deprecated
|
|
75
|
-
static cacheFields = []
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Default attributes on a model. Can be an object or a function that returns an object.
|
|
79
|
-
*/
|
|
80
|
-
static defaults = {}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Use this to tell resourcerer to track this model's request time via the `track` method added
|
|
84
|
-
* in the resourcerer configuration file. This can be a boolean or a function that returns a
|
|
85
|
-
* boolean. If the latter, it takes a the resource config object as an argument.
|
|
86
|
-
*/
|
|
87
|
-
static measure = false
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns a copy of the model's `attributes` object. Use this method to get the current entire
|
|
91
|
-
* server data representation.
|
|
92
|
-
*
|
|
93
|
-
* @return {object} model attributes
|
|
94
|
-
*/
|
|
95
|
-
toJSON() {
|
|
96
|
-
return {...this.attributes};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Proxies the `sync` module by default, but this can be overridden for custom behavior.
|
|
101
|
-
*/
|
|
102
|
-
sync(...args) {
|
|
103
|
-
return sync.call(this, ...args);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Gets the value of an attribute.
|
|
108
|
-
*
|
|
109
|
-
* @param {string} attr - attribute key
|
|
110
|
-
* @return {any} data attribute at that key
|
|
111
|
-
*/
|
|
112
|
-
get(attr) {
|
|
113
|
-
return this.attributes[attr];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Gets the value of multiple attributes.
|
|
118
|
-
*
|
|
119
|
-
* @param {string[]} attrs - attribute keys for which to get values
|
|
120
|
-
* @return {object} subsection of model data containing the specified keys
|
|
121
|
-
*/
|
|
122
|
-
pick(...attrs) {
|
|
123
|
-
return attrs.reduce((memo, attr) => Object.assign(
|
|
124
|
-
memo,
|
|
125
|
-
this.has(attr) ? {[attr]: this.get(attr)} : {}
|
|
126
|
-
), {});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Returns true if there is a defined value at a given attribute key.
|
|
131
|
-
*
|
|
132
|
-
* @param {string} attr - attribute key
|
|
133
|
-
* @return {boolean} whether the model has a value at that key
|
|
134
|
-
*/
|
|
135
|
-
has(attr) {
|
|
136
|
-
return ![undefined, null].includes(this.get(attr));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* This is how we change attribute values on a model. When we call .save(), this happens before
|
|
141
|
-
* the request fires, and when we call .fetch(), this happens after the request returns. Unless
|
|
142
|
-
* called with a {silent: true} flag, this will trigger an update for all subscribed components
|
|
143
|
-
* to reflect the new changes.
|
|
144
|
-
*
|
|
145
|
-
* @param {object} attrs - key-value map of new attributes to set on the model
|
|
146
|
-
* @param {object} options - map including the following options:
|
|
147
|
-
* unset: {boolean} whether to delete the given attributes keys from the model
|
|
148
|
-
* instead of set them
|
|
149
|
-
* silent: {boolean} if true, skips the triggerUpdate() call after setting the attributes
|
|
150
|
-
* @return {Model} model instance
|
|
151
|
-
*/
|
|
152
|
-
set(attrs={}, options={}) {
|
|
153
|
-
var prevId = this.id,
|
|
154
|
-
hasSomethingChanged = false;
|
|
155
|
-
|
|
156
|
-
// For each `set` attribute, update or delete the current value.
|
|
157
|
-
for (let attr of Object.keys(attrs)) {
|
|
158
|
-
if (!hasSomethingChanged && !isDeepEqual(this.attributes[attr], attrs[attr])) {
|
|
159
|
-
hasSomethingChanged = true;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
options.unset ? delete this.attributes[attr] : this.attributes[attr] = attrs[attr];
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Update the `id`.
|
|
166
|
-
if (this.constructor.idAttribute in attrs) {
|
|
167
|
-
this.id = this.get(this.constructor.idAttribute);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// trigger updated for the change
|
|
171
|
-
if (!options.silent && hasSomethingChanged) {
|
|
172
|
-
this.triggerUpdate();
|
|
173
|
-
|
|
174
|
-
if (this.collection && prevId && prevId !== this.id) {
|
|
175
|
-
this.collection._updateModelReference(this.id, prevId, this);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return this;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Removes a model's attribute at a given key.
|
|
184
|
-
*
|
|
185
|
-
* @param {string} attr - attribute key
|
|
186
|
-
* @param {object} options - set options
|
|
187
|
-
* @return {Model} model instance
|
|
188
|
-
*/
|
|
189
|
-
unset(attr, options) {
|
|
190
|
-
return this.set({[attr]: undefined}, {unset: true, ...options});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Removes all attributes for a model.
|
|
195
|
-
*
|
|
196
|
-
* @param {object} options - set options
|
|
197
|
-
* @return {Model} model instance
|
|
198
|
-
*/
|
|
199
|
-
clear(options) {
|
|
200
|
-
var attrs = {};
|
|
201
|
-
|
|
202
|
-
for (let key of Object.keys(this.attributes)) {
|
|
203
|
-
attrs[key] = undefined;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return this.set(attrs, {unset: true, ...options});
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Main method that preps a GET request at this model's url. This is the method the request module
|
|
211
|
-
* uses to sync server data after instantiating a model. Upon returning, an update is triggered
|
|
212
|
-
* for all registered components.
|
|
213
|
-
*
|
|
214
|
-
* @param {object} options - can include any property used by the sync module
|
|
215
|
-
* @return {promise} - resolves with a tuple of the instance and response object
|
|
216
|
-
*/
|
|
217
|
-
fetch(options={}) {
|
|
218
|
-
options = {parse: true, method: 'GET', ...options};
|
|
219
|
-
|
|
220
|
-
return this.sync(this, options)
|
|
221
|
-
.then(([json, response]) => {
|
|
222
|
-
var serverAttrs = options.parse ? this.parse(json, options) : json;
|
|
223
|
-
|
|
224
|
-
this.set(serverAttrs, options);
|
|
225
|
-
// sync update
|
|
226
|
-
this.triggerUpdate();
|
|
227
|
-
|
|
228
|
-
return [this, response];
|
|
229
|
-
})
|
|
230
|
-
.catch((response) => Promise.reject(response));
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Whereas fetching data is handled declaratively by resourcerer, creates and updates occur
|
|
235
|
-
* imperatively in your app via model.save(). It first sets the new properties on the model,
|
|
236
|
-
* triggering an update (unless {wait: true} is passed). Then it preps a sync write request.
|
|
237
|
-
*
|
|
238
|
-
* @param {object} attrs - new attributes to set on the model and send as a POST/PUT/PATCH body.
|
|
239
|
-
* if null then the current model attributes are used.
|
|
240
|
-
* @param {object} options - can include any property used by the sync module. pass {wait: true}
|
|
241
|
-
* to wait to set properties on the model until after the save request succeeds
|
|
242
|
-
* @return {promise} - resolves with a tuple of the instance and response object
|
|
243
|
-
*/
|
|
244
|
-
save(attrs, options) {
|
|
245
|
-
var previousAttributes = this.toJSON();
|
|
246
|
-
|
|
247
|
-
options = {parse: true, ...options};
|
|
248
|
-
attrs = attrs || this.toJSON();
|
|
249
|
-
|
|
250
|
-
// If we're not waiting and attributes exist, save acts as `set(attr).save(null, opts)`
|
|
251
|
-
if (!options.wait) {
|
|
252
|
-
this.set(attrs, options);
|
|
253
|
-
} else {
|
|
254
|
-
options.attrs = {...this.attributes, ...attrs};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
options.method = this.isNew() ? 'POST' : options.patch ? 'PATCH' : 'PUT';
|
|
258
|
-
|
|
259
|
-
if (options.method === 'PATCH' && !options.attrs) {
|
|
260
|
-
options.attrs = attrs;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return this.sync(this, options)
|
|
264
|
-
.then(([json, response]) => {
|
|
265
|
-
var serverAttrs = options.parse ? this.parse(json, options) : json;
|
|
266
|
-
|
|
267
|
-
if (options.wait) {
|
|
268
|
-
serverAttrs = {...attrs, ...serverAttrs};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// avoid triggering any updates in the set call since we'll do it immediately after
|
|
272
|
-
this.set(serverAttrs, {silent: true, ...options});
|
|
273
|
-
// sync update
|
|
274
|
-
this.triggerUpdate();
|
|
275
|
-
|
|
276
|
-
return [this, response];
|
|
277
|
-
})
|
|
278
|
-
.catch((response) => {
|
|
279
|
-
if (!options.wait) {
|
|
280
|
-
// keep the clear silent so that we only render when we reset attributes
|
|
281
|
-
this.clear({silent: true}).set(previousAttributes, options);
|
|
282
|
-
} else {
|
|
283
|
-
this.attributes = previousAttributes;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return Promise.reject(response);
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Method used to send a DELETE request to the server. If part of a collection, also removes the
|
|
292
|
-
* model from the collection. Pass {wait: true} for that to happen (along with an update) after
|
|
293
|
-
* the request returns.
|
|
294
|
-
*
|
|
295
|
-
* @param {object} options - can include any property used by the sync module. pass {wait: true}
|
|
296
|
-
* to wait to set properties on the model until after the request succeeds
|
|
297
|
-
* @return {promise} - resolves with a tuple of the instance and response object
|
|
298
|
-
*/
|
|
299
|
-
destroy(options={}) {
|
|
300
|
-
var request = this.isNew() ?
|
|
301
|
-
Promise.resolve([]) :
|
|
302
|
-
this.sync(this, {method: 'DELETE', ...options}),
|
|
303
|
-
collection = this.collection;
|
|
304
|
-
|
|
305
|
-
if (!options.wait) {
|
|
306
|
-
this.triggerUpdate();
|
|
307
|
-
this.collection?.remove(this, {silent: true});
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return request.then(([json, response]) => {
|
|
311
|
-
if (options.wait && !this.isNew()) {
|
|
312
|
-
this.triggerUpdate();
|
|
313
|
-
this.collection?.remove(this, {silent: true});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return [this, response];
|
|
317
|
-
}).catch((response) => {
|
|
318
|
-
if (!options.wait && !this.isNew()) {
|
|
319
|
-
collection?.add(this);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return Promise.reject(response);
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Default url method on a model. This should only be invoked if the model belongs to a collection
|
|
328
|
-
* or has a urlRoot method, both of which this method uses to append the model's id. Otherwise,
|
|
329
|
-
* for models outside of a collection, they should have their own overriding url method defined.
|
|
330
|
-
*
|
|
331
|
-
* @return {string} the url endpoint to request for this particular model instance
|
|
332
|
-
*/
|
|
333
|
-
url(options=this.urlOptions) {
|
|
334
|
-
var base = result(this, 'urlRoot', options) || result(this.collection, 'url', options) ||
|
|
335
|
-
urlError();
|
|
336
|
-
|
|
337
|
-
if (this.isNew()) {
|
|
338
|
-
return base;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return base.replace(/[^/]$/, '$&/') +
|
|
342
|
-
window.encodeURIComponent(this.get(this.constructor.idAttribute));
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* By default, parse() is the identity function. Override this if you need special business logic
|
|
347
|
-
* to transform the server response into a different form for your application that will be set
|
|
348
|
-
* as attributes on the model.
|
|
349
|
-
*
|
|
350
|
-
* @param {any} response - server response data
|
|
351
|
-
* @param {object} options - options map
|
|
352
|
-
* @return {object} data object transformed from the server response to be applied as the model's
|
|
353
|
-
* data attributes
|
|
354
|
-
*/
|
|
355
|
-
parse(response, options) {
|
|
356
|
-
return response;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* A model by default is considered new if it doesn't have an id property, which makes sense--it
|
|
361
|
-
* hasn't been saved to the server yet. Practically, this can also be overridden to get desired
|
|
362
|
-
* behavior, ie forcing a request to be POST instead of PUT or vice versa.
|
|
363
|
-
*
|
|
364
|
-
* @return {boolean}
|
|
365
|
-
*/
|
|
366
|
-
isNew() {
|
|
367
|
-
return !this.has(this.constructor.idAttribute);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// mix in events to the model
|
|
372
|
-
Object.assign(Model.prototype, Events);
|
package/lib/prefetch.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import {ModelMap, ResourceKeys} from './config.js';
|
|
2
|
-
import {noOp, once} from './utils.js';
|
|
3
|
-
|
|
4
|
-
import {getCacheKey} from './resourcerer.js';
|
|
5
|
-
import request from './request.js';
|
|
6
|
-
|
|
7
|
-
// prevents api request bursts based on quick swipes
|
|
8
|
-
const PREFETCH_TIMEOUT = 50;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Call this as with a resources config function and a component's props.
|
|
12
|
-
* The returned function should be applied to an onMouseEnter callback.
|
|
13
|
-
*
|
|
14
|
-
* @param {function} getResources - function that takes props as an argument
|
|
15
|
-
* and should return a map of resources. Identical to those used in
|
|
16
|
-
* `useResources`/`withResources`
|
|
17
|
-
* @param {object} expectedProps - props in the form expected when the resource
|
|
18
|
-
* would be needed
|
|
19
|
-
* @return {function} callback to be invoked onMouseEnter of appropriate DOM el
|
|
20
|
-
*/
|
|
21
|
-
export default (getResources, expectedProps={}) => {
|
|
22
|
-
var fetched,
|
|
23
|
-
resources = Object.entries(getResources(ResourceKeys, expectedProps) || {});
|
|
24
|
-
|
|
25
|
-
return (evt) => {
|
|
26
|
-
var {target} = evt,
|
|
27
|
-
prefetchTimeout;
|
|
28
|
-
|
|
29
|
-
if (target && !fetched) {
|
|
30
|
-
// set short timeout so that we can cancel if user 'doesn't intend'
|
|
31
|
-
// to click on the link
|
|
32
|
-
prefetchTimeout = window.setTimeout(() => {
|
|
33
|
-
resources.forEach(([name, config]) => {
|
|
34
|
-
request(getCacheKey({modelKey: name, ...config}, expectedProps), ModelMap[name], {
|
|
35
|
-
...config,
|
|
36
|
-
prefetch: true
|
|
37
|
-
// Prefetch is only opportunistic so if we error now,
|
|
38
|
-
// we'll retry and handle the error in withResources
|
|
39
|
-
}).catch(noOp);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
fetched = true;
|
|
43
|
-
}, PREFETCH_TIMEOUT);
|
|
44
|
-
|
|
45
|
-
// if we leave the prefetch element before the timeout, don't prefetch
|
|
46
|
-
target.addEventListener('mouseleave', once(_onMouseLeave.bind(null, prefetchTimeout)));
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* If user swipes quickly over the DOM el of interest, cancel the timeout
|
|
53
|
-
* to avoid spamming the API.
|
|
54
|
-
*
|
|
55
|
-
* @param {number} timeout - timeout to clear so that prefetch doesn't happen
|
|
56
|
-
*/
|
|
57
|
-
function _onMouseLeave(timeout) {
|
|
58
|
-
window.clearTimeout(timeout);
|
|
59
|
-
}
|