resourcerer 1.2.0 → 1.2.1
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 +8 -3
- package/lib/resourcerer.js +27 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -496,13 +496,18 @@ As alluded to in the [Other Props](#other-props-returned-from-the-hookpassed-fro
|
|
|
496
496
|
|
|
497
497
|
### force
|
|
498
498
|
|
|
499
|
-
Sometimes you want the latest of a resource, bypassing whatever model has already been cached in your application. To accomplish this, simply pass a `force: true` in a resource's config. The force-fetched response will replace any prior model in the cache
|
|
499
|
+
Sometimes you want the latest of a resource, bypassing whatever model has already been cached in your application. To accomplish this, simply pass a `force: true` in a resource's config. The force-fetched response will replace any prior model in the cache.
|
|
500
500
|
|
|
501
501
|
```js
|
|
502
|
-
|
|
503
|
-
|
|
502
|
+
const getResources = ({LATEST_STATS}, props) => ({[LATEST_STATS]: {force: true}});
|
|
503
|
+
|
|
504
|
+
function MyComponentWithLatestStats(props) {
|
|
505
|
+
const {latestStatsModel} = useResources(getResources, props);
|
|
506
|
+
}
|
|
504
507
|
```
|
|
505
508
|
|
|
509
|
+
The resource will only get force-requested when the component mounts; the `force` flag will get ignored on subsequent updates. If you need to refetch after mounting to get the latest resource, use [refetch](#refetching).
|
|
510
|
+
|
|
506
511
|
### Custom Resource Names
|
|
507
512
|
|
|
508
513
|
Passing a `modelKey: <ResourceKeys>` option allows you to pass a custom name as the `withResources` key, which will become the base name for component-related props passed down to the component. For example, this configuration:
|
package/lib/resourcerer.js
CHANGED
|
@@ -44,8 +44,8 @@ const SPREAD_PROVIDES_CHAR = '_';
|
|
|
44
44
|
* a value to be set for the prop. If the key is equal to
|
|
45
45
|
* SPREAD_PROVIDES_CHAR, then the return value of the transform function
|
|
46
46
|
* is spread for dynamically-provided props.
|
|
47
|
-
* * force {boolean} - force a fetch request regardless of whether the
|
|
48
|
-
* is already in the cache
|
|
47
|
+
* * force {boolean} - force a fetch request on mount regardless of whether the
|
|
48
|
+
* model is already in the cache
|
|
49
49
|
* * prefetch {object} - an individual map of expected future props to fetch
|
|
50
50
|
* a new resource and store it in the cache. it will not, however, get
|
|
51
51
|
* passed down to the child (no loading states, either)
|
|
@@ -111,7 +111,10 @@ export const useResources = (getResources, props) => {
|
|
|
111
111
|
|
|
112
112
|
// note that this only tells when cache keys have changed. if a resource has already been
|
|
113
113
|
// cached, this will still return its resource when the component mounts. that's why we
|
|
114
|
-
// partition these out later into loaded and not loaded resources
|
|
114
|
+
// partition these out later into loaded and not loaded resources.
|
|
115
|
+
//
|
|
116
|
+
// for a similar reason, forced resources get returned here on mount and not afterwards.
|
|
117
|
+
// we prevent them from getting set to a LOADED state which forces them to get fetched.
|
|
115
118
|
getResourcesToUpdate = (rsrcs) => rsrcs.filter(hasAllDependencies.bind(null, props))
|
|
116
119
|
.filter(not(shouldBypassFetch.bind(null, props)))
|
|
117
120
|
.filter(([name, config]) => {
|
|
@@ -151,7 +154,13 @@ export const useResources = (getResources, props) => {
|
|
|
151
154
|
nextLoadingStates = {
|
|
152
155
|
...buildResourcesLoadingState(pendingResources, props, LoadingStates.PENDING),
|
|
153
156
|
// but resourcesToUpdate should get set to LOADING (or LOADED if in the cache)
|
|
154
|
-
...buildResourcesLoadingState(
|
|
157
|
+
...buildResourcesLoadingState(
|
|
158
|
+
// removing forced resources here ensures that they stay in a LOADING state and thus
|
|
159
|
+
// get partitioned into resourcesToFetch. this only happens on mount; on update, they
|
|
160
|
+
// don't ever get included in resourcesToUpdate
|
|
161
|
+
resourcesToUpdate.filter(withoutPrefetch).filter(withoutForced),
|
|
162
|
+
props
|
|
163
|
+
)
|
|
155
164
|
},
|
|
156
165
|
|
|
157
166
|
// separate out those resources to update into those that are already cached and those
|
|
@@ -548,7 +557,8 @@ function findCacheKey(resource, getResources, props) {
|
|
|
548
557
|
*/
|
|
549
558
|
function buildResourcesLoadingState(resources, props, defaultState=LoadingStates.LOADED) {
|
|
550
559
|
return resources.reduce((state, [name, config]) => Object.assign(state, {
|
|
551
|
-
[getResourceState(name)]: !config.refetch &&
|
|
560
|
+
[getResourceState(name)]: !config.refetch && !config.force &&
|
|
561
|
+
(shouldBypassFetch(props, [name, config]) ||
|
|
552
562
|
(getModelFromCache(config) && !getModelFromCache(config).lazy)) ?
|
|
553
563
|
defaultState :
|
|
554
564
|
(!hasAllDependencies(props, [, config]) ? LoadingStates.PENDING : config.lazy ?
|
|
@@ -605,6 +615,16 @@ function withoutNoncritical([, config]) {
|
|
|
605
615
|
return !config.noncritical;
|
|
606
616
|
}
|
|
607
617
|
|
|
618
|
+
/**
|
|
619
|
+
* Filter predicate to remove forced resources from a resources list.
|
|
620
|
+
*
|
|
621
|
+
* @param {[, object]} config - resources config entry
|
|
622
|
+
* @return {boolean} true if a resource is not forced
|
|
623
|
+
*/
|
|
624
|
+
function withoutForced([, config]) {
|
|
625
|
+
return !config.force;
|
|
626
|
+
}
|
|
627
|
+
|
|
608
628
|
/**
|
|
609
629
|
* This method determines if props with the resource names have been
|
|
610
630
|
* passed to the component directly, in which case we'll skip the
|
|
@@ -739,8 +759,8 @@ function modelAggregator(resources) {
|
|
|
739
759
|
*/
|
|
740
760
|
function partitionResources(resourcesToUpdate, loadingStates) {
|
|
741
761
|
return resourcesToUpdate.reduce((memo, [name, config]) =>
|
|
742
|
-
config.refetch || config.
|
|
743
|
-
|
|
762
|
+
config.refetch || config.force || config.prefetch ||
|
|
763
|
+
!hasLoaded(loadingStates[getResourceState(name)]) || getModelFromCache(config)?.lazy ?
|
|
744
764
|
[memo[0], memo[1].concat([[name, config]])] :
|
|
745
765
|
[memo[0].concat([[name, config]]), memo[1]],
|
|
746
766
|
[[], []]);
|
package/package.json
CHANGED