resourcerer 1.1.5 → 1.1.7
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/index.d.ts +4 -2
- package/lib/resourcerer.js +30 -15
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -57,9 +57,9 @@ declare module 'resourcerer' {
|
|
|
57
57
|
|
|
58
58
|
toJSON(): T;
|
|
59
59
|
|
|
60
|
-
url(): string;
|
|
60
|
+
url(urlOptions?: Record<string, any>): string;
|
|
61
61
|
|
|
62
|
-
urlRoot(): string;
|
|
62
|
+
urlRoot(urlOptions?: Record<string, any>): string;
|
|
63
63
|
|
|
64
64
|
static cacheFields: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
65
65
|
|
|
@@ -165,6 +165,8 @@ declare module 'resourcerer' {
|
|
|
165
165
|
setResourceState(newState: {[key: string]: any}): void;
|
|
166
166
|
isOrWillBeLoading(): boolean;
|
|
167
167
|
[key: `${string}LoadingState`]: LoadingTypes;
|
|
168
|
+
[key: `${string}Collection`]: Collection;
|
|
169
|
+
[key: `${string}Model`]: Model;
|
|
168
170
|
}
|
|
169
171
|
|
|
170
172
|
declare function haveAllLoaded(loadingStates: LoadingTypes | LoadingTypes[]): boolean;
|
package/lib/resourcerer.js
CHANGED
|
@@ -166,9 +166,13 @@ export const useResources = (getResources, props) => {
|
|
|
166
166
|
// because we don't go through the request module for normal-flow resources that are cached,
|
|
167
167
|
// we need to re-register this component with the resource. this will also clear any timeouts
|
|
168
168
|
// for removing the resource.
|
|
169
|
-
loadedResources.
|
|
170
|
-
|
|
171
|
-
|
|
169
|
+
loadedResources.forEach(([, config]) => {
|
|
170
|
+
var cacheKey = getCacheKey(config);
|
|
171
|
+
|
|
172
|
+
ModelCache.register(cacheKey, componentRef.current);
|
|
173
|
+
// make sure we add any provided props for serial requests
|
|
174
|
+
provideProps(ModelCache.get(cacheKey), config.provides, props, setResourceState);
|
|
175
|
+
});
|
|
172
176
|
|
|
173
177
|
// NOTE: changing this to resourcesToFetch causes some inexplicable bugs around cached resources
|
|
174
178
|
// and a UI that wouldn't update. so this is kept as resourcesToUpdate and fetchResources is
|
|
@@ -876,18 +880,7 @@ function fetchResources(resources, props, {
|
|
|
876
880
|
// don't continue unless component is still mounted and resource is current
|
|
877
881
|
if (isCurrentResource([name, config], cacheKey)) {
|
|
878
882
|
// add any dependencies this resource might provide for other resources
|
|
879
|
-
|
|
880
|
-
setResourceState((state) => ({
|
|
881
|
-
...state,
|
|
882
|
-
...Object.entries(provides).reduce((memo, [provide, transform]) => Object.assign({
|
|
883
|
-
memo,
|
|
884
|
-
...(provide === SPREAD_PROVIDES_CHAR ?
|
|
885
|
-
transform(model, props) :
|
|
886
|
-
{[provide]: transform(model, props)})
|
|
887
|
-
}), {})
|
|
888
|
-
}));
|
|
889
|
-
}
|
|
890
|
-
|
|
883
|
+
provideProps(model, provides, props, setResourceState);
|
|
891
884
|
onRequestSuccess(model, status, [name, config]);
|
|
892
885
|
}
|
|
893
886
|
},
|
|
@@ -904,3 +897,25 @@ function fetchResources(resources, props, {
|
|
|
904
897
|
})
|
|
905
898
|
);
|
|
906
899
|
}
|
|
900
|
+
|
|
901
|
+
/**
|
|
902
|
+
* Add any dependencies that the model provides as resource state.
|
|
903
|
+
*
|
|
904
|
+
* @param {Model|Collection} model
|
|
905
|
+
* @param {object} provides - config provides object
|
|
906
|
+
* @param {object} props - current component props
|
|
907
|
+
* @param {function} setResourceState - updates resource state
|
|
908
|
+
*/
|
|
909
|
+
function provideProps(model, provides={}, props, setResourceState) {
|
|
910
|
+
if (Object.entries(provides).length) {
|
|
911
|
+
setResourceState((state) => ({
|
|
912
|
+
...state,
|
|
913
|
+
...Object.entries(provides).reduce((memo, [provide, transform]) => Object.assign({
|
|
914
|
+
memo,
|
|
915
|
+
...(provide === SPREAD_PROVIDES_CHAR ?
|
|
916
|
+
transform(model, props) :
|
|
917
|
+
{[provide]: transform(model, props)})
|
|
918
|
+
}), {})
|
|
919
|
+
}));
|
|
920
|
+
}
|
|
921
|
+
}
|
package/package.json
CHANGED