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/request.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import Collection from './collection.js';
|
|
2
|
-
import ModelCache from './model-cache.js';
|
|
3
|
-
|
|
4
|
-
const loadingCache = {};
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Retrieves a model from the ModelCache. If needed, best to use this in an
|
|
8
|
-
* application instead of importing the ModelCache directly.
|
|
9
|
-
*
|
|
10
|
-
* @param {string} key - cache lookup key
|
|
11
|
-
* @return {Model?|Collection?} model instance from
|
|
12
|
-
* cache at the cache key if it exists
|
|
13
|
-
*/
|
|
14
|
-
export const getFromCache = (key) => ModelCache.get(key);
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Return whether or not a model exists within ModelCache for the given key. If
|
|
18
|
-
* needed, best to use this in an application instead of importing the
|
|
19
|
-
* ModelCache directly.
|
|
20
|
-
*
|
|
21
|
-
* @param {string} key - The cache key of the model to check for existence.
|
|
22
|
-
* @return {boolean}
|
|
23
|
-
*/
|
|
24
|
-
export const existsInCache = (key) => !!ModelCache.get(key);
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Main method for retrieving models/collections.
|
|
28
|
-
*
|
|
29
|
-
* Note that it utilizes the loadingCache while a model is being fetched, and
|
|
30
|
-
* removes it from the loadingCache before resolve or reject is called. This
|
|
31
|
-
* way, we can attach multiple .then()s to a promise that will all be
|
|
32
|
-
* executed when the promise is fulfilled.
|
|
33
|
-
*
|
|
34
|
-
* @param {string} key - cache lookup key
|
|
35
|
-
* @param {function} Model - the model constructor
|
|
36
|
-
* @param {object} options - options object used for fetching that can include:
|
|
37
|
-
*
|
|
38
|
-
* * data {object|object[]} - attributes or models to pass a Model or Collection instance, resp.
|
|
39
|
-
* * params {object} - query params to pass into the fetch method
|
|
40
|
-
* * fetch {boolean} - whether fetch the model after creation
|
|
41
|
-
* * force {boolean} - force the fetch to be made if the model is already cached
|
|
42
|
-
* * method {string} - request type (GET|POST|PUT|DELETE)
|
|
43
|
-
* * options {object} - options to pass to the Model constructor
|
|
44
|
-
* * prefetch {boolean} - whether the request should be treated as a prefetched resource
|
|
45
|
-
*
|
|
46
|
-
* @return {promise} a promise that will resolve with the new Model/Collection instance
|
|
47
|
-
*/
|
|
48
|
-
export default (key, Model, options={}) => {
|
|
49
|
-
var model = ModelCache.get(key),
|
|
50
|
-
addToLoadingCache,
|
|
51
|
-
_promise;
|
|
52
|
-
|
|
53
|
-
options = {
|
|
54
|
-
data: Model && Model.prototype instanceof Collection ? [] : {},
|
|
55
|
-
params: {},
|
|
56
|
-
options: {},
|
|
57
|
-
fetch: true,
|
|
58
|
-
force: false,
|
|
59
|
-
lazy: false,
|
|
60
|
-
method: 'GET',
|
|
61
|
-
...options
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
if (!loadingCache[key]) {
|
|
65
|
-
_promise = new Promise((resolve, reject) => {
|
|
66
|
-
if (!model || model.lazy || options.force) {
|
|
67
|
-
model = model || new Model(options.data, options.options);
|
|
68
|
-
|
|
69
|
-
if (options.fetch && !options.lazy) {
|
|
70
|
-
addToLoadingCache = true;
|
|
71
|
-
|
|
72
|
-
model.fetch({params: options.params}).then(
|
|
73
|
-
([newModel, response]) => {
|
|
74
|
-
delete loadingCache[key];
|
|
75
|
-
// waiting to delete lazy property until after fetch completes ensures multiple
|
|
76
|
-
// components that call non-lazy version of the resource all get put into a
|
|
77
|
-
// loading state
|
|
78
|
-
delete model.lazy;
|
|
79
|
-
|
|
80
|
-
ModelCache.put(key, newModel, options.component);
|
|
81
|
-
resolve([newModel, response?.status]);
|
|
82
|
-
},
|
|
83
|
-
(response) => {
|
|
84
|
-
delete loadingCache[key];
|
|
85
|
-
reject(response?.status);
|
|
86
|
-
}
|
|
87
|
-
);
|
|
88
|
-
} else {
|
|
89
|
-
// lazy means resolve but don't fetch yet; it will get updates from other components
|
|
90
|
-
// we only want to do this if a model is not yet in the cache
|
|
91
|
-
options.lazy && !ModelCache.get(key) ? model.lazy = true : null;
|
|
92
|
-
|
|
93
|
-
ModelCache.put(key, model, options.component);
|
|
94
|
-
resolve([model]);
|
|
95
|
-
}
|
|
96
|
-
// this block normally will not get invoked because previous-cached resources will bypass
|
|
97
|
-
// the request module to avoid the async Promise tick. this will still get called for some
|
|
98
|
-
// unconventional resource loading such as prefetch requests that are cached, both from the
|
|
99
|
-
// prefetch module and from the `prefetches` resource config property
|
|
100
|
-
} else {
|
|
101
|
-
// this will cancel any in-flight timeouts and add the component
|
|
102
|
-
// to the list of components using the resource
|
|
103
|
-
ModelCache.register(key, options.component);
|
|
104
|
-
|
|
105
|
-
// the model has been fetched and is stored in the cache, so just
|
|
106
|
-
// immediately resolve with that as our value
|
|
107
|
-
resolve([model]);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
if (addToLoadingCache) {
|
|
112
|
-
// add promise to loading cache, not model
|
|
113
|
-
loadingCache[key] = _promise;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return _promise;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// a prefetch may have initiated the fetch and we need to
|
|
120
|
-
// clear the timeout and register this component
|
|
121
|
-
ModelCache.register(key, options.component);
|
|
122
|
-
|
|
123
|
-
// return the existing promise if the promise hasn't yet been fulfilled.
|
|
124
|
-
// this way we can attach more .then() handlers
|
|
125
|
-
return loadingCache[key];
|
|
126
|
-
};
|