resourcerer 1.0.3 → 1.0.6
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/lib/request.js +6 -3
- package/lib/resourcerer.js +8 -3
- package/package.json +1 -1
- package/test/request.test.js +7 -2
package/lib/request.js
CHANGED
|
@@ -69,11 +69,14 @@ export default (key, Model, options={}) => {
|
|
|
69
69
|
if (options.fetch && !options.lazy) {
|
|
70
70
|
addToLoadingCache = true;
|
|
71
71
|
|
|
72
|
-
delete model.lazy;
|
|
73
|
-
|
|
74
72
|
model.fetch({params: options.params}).then(
|
|
75
73
|
([newModel, response]) => {
|
|
76
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
|
+
|
|
77
80
|
ModelCache.put(key, newModel, options.component);
|
|
78
81
|
resolve([newModel, response?.status]);
|
|
79
82
|
},
|
|
@@ -83,7 +86,7 @@ export default (key, Model, options={}) => {
|
|
|
83
86
|
}
|
|
84
87
|
);
|
|
85
88
|
} else {
|
|
86
|
-
// lazy means
|
|
89
|
+
// lazy means resolve but don't fetch yet; it will get updates from other components
|
|
87
90
|
// we only want to do this if a model is not yet in the cache
|
|
88
91
|
options.lazy && !ModelCache.get(key) ? model.lazy = true : null;
|
|
89
92
|
|
package/lib/resourcerer.js
CHANGED
|
@@ -101,17 +101,22 @@ export const useResources = (getResources, props) => {
|
|
|
101
101
|
listenedModelsRef = useRef([]),
|
|
102
102
|
|
|
103
103
|
forceUpdate = useForceUpdate(),
|
|
104
|
-
|
|
104
|
+
|
|
105
105
|
// note that this only tells when cache keys have changed. if a resource has already been
|
|
106
106
|
// cached, this will still return its resource when the component mounts. that's why we
|
|
107
107
|
// partition these out later into loaded and not loaded resources
|
|
108
108
|
getResourcesToUpdate = (rsrcs) => rsrcs.filter(hasAllDependencies.bind(null, props))
|
|
109
109
|
.filter(not(shouldBypassFetch.bind(null, props)))
|
|
110
110
|
.filter(([name, config]) => {
|
|
111
|
-
var
|
|
111
|
+
var prevConfig = prevPropsRef.current &&
|
|
112
|
+
findConfig([name, config], getResources, prevPropsRef.current),
|
|
113
|
+
previousCacheKey = prevConfig && getCacheKey(prevConfig);
|
|
112
114
|
|
|
113
115
|
return (!previousCacheKey || previousCacheKey !== getCacheKey(config) ||
|
|
114
|
-
!hasAllDependencies(prevPropsRef.current, [, config]) || config.refetch
|
|
116
|
+
!hasAllDependencies(prevPropsRef.current, [, config]) || config.refetch ||
|
|
117
|
+
// make sure if we were lazy and are no longer lazy (from the same component) that we
|
|
118
|
+
// get included in the list to get updated
|
|
119
|
+
(prevConfig?.lazy && !config.lazy));
|
|
115
120
|
});
|
|
116
121
|
|
|
117
122
|
/**
|
package/package.json
CHANGED
package/test/request.test.js
CHANGED
|
@@ -306,12 +306,17 @@ describe('Request', () => {
|
|
|
306
306
|
});
|
|
307
307
|
|
|
308
308
|
it('fetches when another component requests the same model without lazy option', async() => {
|
|
309
|
-
var [model] = await request('lazy', Model, {component, lazy: true})
|
|
309
|
+
var [model] = await request('lazy', Model, {component, lazy: true}),
|
|
310
|
+
requestPromise;
|
|
310
311
|
|
|
311
312
|
expect(model.lazy).toBe(true);
|
|
312
313
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
313
314
|
|
|
314
|
-
|
|
315
|
+
requestPromise = request('lazy', Model, {component});
|
|
316
|
+
|
|
317
|
+
expect(model.lazy).toBeDefined();
|
|
318
|
+
await requestPromise;
|
|
319
|
+
|
|
315
320
|
expect(model.lazy).not.toBeDefined();
|
|
316
321
|
expect(Model.prototype.fetch).toHaveBeenCalled();
|
|
317
322
|
|