resourcerer 1.1.10 → 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/docs/advanced_topics.md +22 -49
- package/index.d.ts +16 -6
- package/lib/resourcerer.js +120 -83
- 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/docs/advanced_topics.md
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
1. [Implicit dependent resources](#implicit-dependent-resources)
|
|
5
5
|
1. [Unfetched Resources](#unfetched-resources)
|
|
6
6
|
1. [Loading Overlays](#loading-overlays)
|
|
7
|
-
1. [isOrWillBeLoading](#isorwillbeloading)
|
|
8
7
|
1. [Recaching newly-saved models](#recaching-newly-saved-models)
|
|
9
8
|
|
|
10
9
|
## Thoughts on the PENDING Resource
|
|
@@ -37,10 +36,12 @@ Using `dependsOn` in simple cases like the one highlighted in the [README](https
|
|
|
37
36
|
1. We can modify the url in the component’s `componentDidUpdate`/`useEffect` (either url path or query parameter), which will filter the prop down.
|
|
38
37
|
|
|
39
38
|
When we provide using method (a), the dependent prop can be changed but not removed. When we provide using method (b), the dependent prop can be changed or removed.
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
|
|
40
|
+
As an example of how we might be able to remove a dependent prop via method (b), consider someone navigating to a `/todos` url that auto-navigates to the first todo item and displays its details. The `todoItem` details resource depends on a `todoId` prop, which it gets in a `useEffect` via changing the url once the `todos` resource loads. So now we’re at `/todos/todo1234`. But if the user clicks the back button, we’ll be back at `/todos` with a cached `todos` resource and `PENDING` `todoItem` resource, and all three loading states set to `false`. (Yes, this is a bit contrived because you should actually replace the history entry in this case, but hopefully it helps to illuminate the issue.)
|
|
41
|
+
|
|
42
|
+
So if we remove the dependent prop, we enter a state where `isLoading`, `hasLoaded`, and `hasErrored` are all false. And since we have to wait for a `useEffect` to re-auto-update the url with the dependent prop, a lifecycle passes with this state, and there’s really nothing we can do about it.
|
|
42
43
|
|
|
43
|
-
And again—`hasInitiallyLoaded` is still true and the model prop is empty, which can cause layout issues if you use, for example, an overlaid loader over a previously-rendered component. For this reason, if using classes/`withResources`, such a previously-rendered component should use `nextProps.hasLoaded` instead of `!nextProps.isLoading` in its `shouldComponentUpdate`:
|
|
44
|
+
And again—`hasInitiallyLoaded` is still true and the `todoItemModel` model prop is empty, which can cause layout issues if you use, for example, an overlaid loader over a previously-rendered component. For this reason, if using classes/`withResources`, such a previously-rendered component should use `nextProps.hasLoaded` instead of `!nextProps.isLoading` in its `shouldComponentUpdate`:
|
|
44
45
|
|
|
45
46
|
```js
|
|
46
47
|
// overlay-wrapped component, where a loader will show over previously-rendered children,
|
|
@@ -59,10 +60,24 @@ Using `dependsOn` in simple cases like the one highlighted in the [README](https
|
|
|
59
60
|
}
|
|
60
61
|
```
|
|
61
62
|
|
|
62
|
-
|
|
63
|
+
If using `useResources`, we'll want to do the equivalent in our memo:
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
```js
|
|
66
|
+
const MemoizedComponent = memo(<Component />, (prevProps, nextProps) => !nextProps.hasLoaded);
|
|
67
|
+
|
|
68
|
+
function Parent() {
|
|
69
|
+
return (
|
|
70
|
+
<div>
|
|
71
|
+
{isLoading ? <OverlayLoader /> : null}
|
|
72
|
+
{hasInitiallyLoaded ? <MemoizedComponent /> : null}
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
4. In the case that the model's `cacheFields` does not include the dependent prop (ie, the prop is used solely for triggering the resource request and doesn't factor into the request data), the model will still exist in the cache when the dependent prop is removed. In this case, the loading state is still returned to PENDING, but the existing model will also be present in the return value.
|
|
80
|
+
|
|
66
81
|
## Implicit dependent resources
|
|
67
82
|
|
|
68
83
|
Another way to effectively have a dependent resource is to use a conditional in your `getResources` method:
|
|
@@ -207,48 +222,6 @@ export default function UserTodos(props) {
|
|
|
207
222
|
}
|
|
208
223
|
```
|
|
209
224
|
|
|
210
|
-
## isOrWillBeLoading (`useResources` only)
|
|
211
|
-
|
|
212
|
-
Taking the previous example of [Loading Overlays](#loading-overlays) just a bit further, what if the todosCollection was 5000 entries long? Okay, you'd probably paginate. But that's not the point! Let's say it's an `<ExpensiveComponent />`, and as it is now, when we change loading states, the parent `MyComponent` would render `<ExpensiveComponent />` on every change, which will kill your UX. So let's wrap it in a `React.memo`!
|
|
213
|
-
|
|
214
|
-
But how do we tell `React.memo` when to not render? This is tricky for a couple of reasons:
|
|
215
|
-
|
|
216
|
-
* Because `useEffect`/`componentDidUpdate` occurs _after_ render, we actually have two renders we want to opt out of:
|
|
217
|
-
* parent props change
|
|
218
|
-
* render is called (opt out!) **See Note**
|
|
219
|
-
* resourcerer changes loaded state to loading from the change in props
|
|
220
|
-
* render is called (opt out!). parent shows the overlay loader
|
|
221
|
-
* request returns and loading state changes back to loaded
|
|
222
|
-
* render (we want this update!)
|
|
223
|
-
|
|
224
|
-
This is actually the reason why I am sad React did away with `componentWillReceiveProps` and does not offer an equivalent hook—using that instead would batch the props change and the loaded -> loading state change, and we'd only need to opt out of a single render. *sigh*. I digress.
|
|
225
|
-
|
|
226
|
-
* React.memo, unlike `shouldComponentUpdate`, does not change its `prevProps` if it skips a render. This makes sense if you think about `memo` being a higher-order-component—if the component doesn't update, its props don't change. But that makes it much harder to compare `prevProps` and `nextProps` when needing to skip two update cycles.
|
|
227
|
-
|
|
228
|
-
Enter `isOrWillBeLoading`. `isOrWillBeLoading` is a function returned by `useResources`/`withResources` that will take care of all these things for you, and you can invoke it in your `React.memo` [areEqual function](https://reactjs.org/docs/react-api.html#reactmemo). The previous example becomes:
|
|
229
|
-
|
|
230
|
-
```jsx
|
|
231
|
-
import ExpensiveComponent from 'components/expensive_component/expensive_component';
|
|
232
|
-
import {memo} from 'react';
|
|
233
|
-
import {useResources} from 'resourcerer';
|
|
234
|
-
|
|
235
|
-
const getResources = ({TODOS}, props) => ({[TODOS]: {}});
|
|
236
|
-
const MemoizedExpensiveComponent = memo(ExpensiveComponent, (prevProps, nextProps) => nextProps.isOrWillBeLoading());
|
|
237
|
-
|
|
238
|
-
export default function UserTodos(props) {
|
|
239
|
-
const {isLoading, isOrWillBeLoading, hasInitiallyLoaded} = useResources(getResources, props);
|
|
240
|
-
|
|
241
|
-
return (
|
|
242
|
-
<div className='MyComponent'>
|
|
243
|
-
{isLoading ? <Loader /> : null}
|
|
244
|
-
{hasInitiallyLoaded ? <MemoizedExpensiveComponent {...{isOrWillBeLoading}} /> : null}
|
|
245
|
-
</div>
|
|
246
|
-
);
|
|
247
|
-
}
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
**Note:** A future upgrade to this library will utilize a [render bailout](https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes) to remove this unnecessary render. The other two will still be required, though.
|
|
251
|
-
|
|
252
225
|
## Recaching newly-saved models
|
|
253
226
|
|
|
254
227
|
A common pattern when creating a new instance of a resource is to keep it in state until the user decides to explicitly save it. For example, when setting up a new
|
package/index.d.ts
CHANGED
|
@@ -28,8 +28,11 @@ declare module 'resourcerer' {
|
|
|
28
28
|
silent?: boolean;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
declare class Model<
|
|
32
|
-
|
|
31
|
+
declare class Model<
|
|
32
|
+
T extends Record<string, any> = {[key: string]: any},
|
|
33
|
+
O extends Record<string, any> & SetOptions = {[key: string]: any}
|
|
34
|
+
> {
|
|
35
|
+
constructor(attrs?: T, options?: O): Model<T, O>;
|
|
33
36
|
|
|
34
37
|
// TODO: need to reference idAttribute here
|
|
35
38
|
id: T extends {id: string} ? string : string | undefined;
|
|
@@ -61,9 +64,13 @@ declare module 'resourcerer' {
|
|
|
61
64
|
|
|
62
65
|
toJSON(): T;
|
|
63
66
|
|
|
64
|
-
url(urlOptions?:
|
|
67
|
+
url(urlOptions?: this["urlOptions"]): string;
|
|
68
|
+
|
|
69
|
+
urlRoot(urlOptions?: this["urlOptions"]): string;
|
|
65
70
|
|
|
66
|
-
|
|
71
|
+
collection?: Collection;
|
|
72
|
+
|
|
73
|
+
readonly urlOptions: Omit<O, keyof SetOptions>;
|
|
67
74
|
|
|
68
75
|
static cacheFields: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
69
76
|
|
|
@@ -85,7 +92,7 @@ declare module 'resourcerer' {
|
|
|
85
92
|
constructor(
|
|
86
93
|
models?: ModelArg<T> | ModelArg<T>[],
|
|
87
94
|
options?: O
|
|
88
|
-
): Collection<T>;
|
|
95
|
+
): Collection<T, O>;
|
|
89
96
|
|
|
90
97
|
length: number;
|
|
91
98
|
|
|
@@ -135,7 +142,9 @@ declare module 'resourcerer' {
|
|
|
135
142
|
|
|
136
143
|
fetch(options?: {parse?: boolean} & SyncOptions & CSetOptions): Promise<[Collection<T>, Response]>;
|
|
137
144
|
|
|
138
|
-
url(urlOptions?:
|
|
145
|
+
url(urlOptions?: this["urlOptions"]): string;
|
|
146
|
+
|
|
147
|
+
readonly urlOptions: Omit<O, keyof CSetOptions>;
|
|
139
148
|
|
|
140
149
|
static cacheFields: Array<string | ((attrs: T) => Record<string, any>)>;
|
|
141
150
|
|
|
@@ -173,6 +182,7 @@ declare module 'resourcerer' {
|
|
|
173
182
|
[key: `${string}LoadingState`]: LoadingTypes;
|
|
174
183
|
[key: `${string}Collection`]: Collection;
|
|
175
184
|
[key: `${string}Model`]: Model;
|
|
185
|
+
[key: `${string}Status`]: number;
|
|
176
186
|
}
|
|
177
187
|
|
|
178
188
|
declare function haveAllLoaded(loadingStates: LoadingTypes | LoadingTypes[]): boolean;
|
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)
|
|
@@ -99,12 +99,22 @@ export const useResources = (getResources, props) => {
|
|
|
99
99
|
// renders. using this reference will guarantee that we always remove listeners from the
|
|
100
100
|
// correct models before attaching new listeners
|
|
101
101
|
listenedModelsRef = useRef([]),
|
|
102
|
+
// when props change and we have models ready in the cache, we set model state in the render
|
|
103
|
+
// process. that means we re-run this function without calling any useEffect callbacks. since
|
|
104
|
+
// prevProps will not (and should not) have changed, the same models that we're to be updated
|
|
105
|
+
// as state in the first render phase would also be up for update in the next one. so we have
|
|
106
|
+
// to keep track of which models have been updated in state, and then we'll reset this when
|
|
107
|
+
// useEffect is finally called.
|
|
108
|
+
cachedModelsSinceLastEffect = useRef({}),
|
|
102
109
|
|
|
103
110
|
forceUpdate = useForceUpdate(),
|
|
104
111
|
|
|
105
112
|
// note that this only tells when cache keys have changed. if a resource has already been
|
|
106
113
|
// cached, this will still return its resource when the component mounts. that's why we
|
|
107
|
-
// 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.
|
|
108
118
|
getResourcesToUpdate = (rsrcs) => rsrcs.filter(hasAllDependencies.bind(null, props))
|
|
109
119
|
.filter(not(shouldBypassFetch.bind(null, props)))
|
|
110
120
|
.filter(([name, config]) => {
|
|
@@ -117,51 +127,63 @@ export const useResources = (getResources, props) => {
|
|
|
117
127
|
// make sure if we were lazy and are no longer lazy (from the same component) that we
|
|
118
128
|
// get included in the list to get updated (and vice versa)
|
|
119
129
|
(prevConfig?.lazy !== config.lazy));
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
130
|
+
}),
|
|
131
|
+
|
|
132
|
+
// every time we call this, we remove all previous listeners and attach new ones based on the
|
|
133
|
+
// current models used by the component.
|
|
134
|
+
attachModelListeners = () => {
|
|
135
|
+
// only get those from the cache. models in state might can empty models, but even so i
|
|
136
|
+
// think it would be preferable to use state (within a separate useEffect) to determine
|
|
137
|
+
// which models get listened to. but as mentioned, that would require the effect to depend
|
|
138
|
+
// on cache keys, which are dynamic, and effects can't have dynamic dependencies right now
|
|
139
|
+
let listenedModels = resources.map(([, config]) => getModelFromCache(config))
|
|
140
|
+
.filter(Boolean);
|
|
141
|
+
|
|
142
|
+
// remove previous listeners and attach new ones
|
|
143
|
+
listenedModelsRef.current.forEach((model) => model.offUpdate(componentRef.current));
|
|
144
|
+
listenedModels.forEach((model) => model.onUpdate(forceUpdate, componentRef.current));
|
|
145
|
+
|
|
146
|
+
listenedModelsRef.current = listenedModels;
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
// this should be for NEW pending resources only, not ones set in initial loading state
|
|
150
|
+
pendingResources = resources.filter(not(hasAllDependencies.bind(null, props)))
|
|
151
|
+
.filter(([, config]) =>
|
|
152
|
+
prevPropsRef.current && hasAllDependencies(prevPropsRef.current, [, config])),
|
|
153
|
+
resourcesToUpdate = getResourcesToUpdate(resources),
|
|
154
|
+
nextLoadingStates = {
|
|
155
|
+
...buildResourcesLoadingState(pendingResources, props, LoadingStates.PENDING),
|
|
156
|
+
// but resourcesToUpdate should get set to LOADING (or LOADED if in the cache)
|
|
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
|
+
)
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
// separate out those resources to update into those that are already cached and those
|
|
167
|
+
// that need to be fetched. the former will get the models updated immediately
|
|
168
|
+
[loadedResources, resourcesToFetch] = partitionResources(
|
|
169
|
+
resourcesToUpdate,
|
|
170
|
+
nextLoadingStates
|
|
171
|
+
),
|
|
157
172
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
173
|
+
// "cached" is a misnomer...
|
|
174
|
+
cachedResources = pendingResources.concat(loadedResources)
|
|
175
|
+
.filter(([name]) => !cachedModelsSinceLastEffect.current[name]);
|
|
176
|
+
|
|
177
|
+
// here we set any newly-pending resources as well as any already-loaded resources as model state,
|
|
178
|
+
// short-circuiting the render cycle since this update happens in the render phase. this is a
|
|
179
|
+
// critical piece of useResources because it means that models, dependent models, and also loading
|
|
180
|
+
// states (next block of code) get updated immediately with no extra 'in-between' blips on the
|
|
181
|
+
// screen.
|
|
182
|
+
if (cachedResources.length) {
|
|
183
|
+
setModels(modelAggregator(cachedResources));
|
|
184
|
+
cachedResources.forEach(([name, config]) => {
|
|
185
|
+
cachedModelsSinceLastEffect.current[name] = true;
|
|
186
|
+
});
|
|
165
187
|
|
|
166
188
|
// because we don't go through the request module for normal-flow resources that are cached,
|
|
167
189
|
// we need to re-register this component with the resource. this will also clear any timeouts
|
|
@@ -170,10 +192,32 @@ export const useResources = (getResources, props) => {
|
|
|
170
192
|
var cacheKey = getCacheKey(config);
|
|
171
193
|
|
|
172
194
|
ModelCache.register(cacheKey, componentRef.current);
|
|
173
|
-
// make sure we add any provided props for serial requests
|
|
195
|
+
// make sure we add any provided props for serial requests. when this function rerenders any
|
|
196
|
+
// cached dependent models will also trigger a setModels call and an additional re-render
|
|
197
|
+
// without a useEffect
|
|
174
198
|
provideProps(ModelCache.get(cacheKey), config.provides, props, setResourceState);
|
|
175
199
|
});
|
|
176
200
|
|
|
201
|
+
// make sure if we bypass fetching because of cached models that we attach fresh listeners
|
|
202
|
+
attachModelListeners();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// set our updated resources' loading states to LOADING. but don't set if we're already in a
|
|
206
|
+
// loading state for that resource, because that's a pointless extra render. also, any resources
|
|
207
|
+
// that have lost their dependencies should go back to a pending state.
|
|
208
|
+
if (Object.keys(nextLoadingStates).some((ky) => nextLoadingStates[ky] !== loadingStates[ky])) {
|
|
209
|
+
loaderDispatch({type: LoadingStates.LOADING, payload: nextLoadingStates});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Fetch things.
|
|
214
|
+
*
|
|
215
|
+
* Note also that every time we set a state, whether it be loading states or models, we are
|
|
216
|
+
* setting a new object as that state and we will re-render. That means that if a model is cached
|
|
217
|
+
* or its loading states haven't changed we should _not_ just go ahead and call fetchResources,
|
|
218
|
+
* because that will re-render our components an additional two times.
|
|
219
|
+
*/
|
|
220
|
+
useEffect(() => {
|
|
177
221
|
// NOTE: changing this to resourcesToFetch causes some inexplicable bugs around cached resources
|
|
178
222
|
// and a UI that wouldn't update. so this is kept as resourcesToUpdate and fetchResources is
|
|
179
223
|
// given resourcesToFetch. Because we are still only fetching resourcesToFetch and because any
|
|
@@ -231,23 +275,14 @@ export const useResources = (getResources, props) => {
|
|
|
231
275
|
}
|
|
232
276
|
}).then(() => {
|
|
233
277
|
if (isMountedRef.current) {
|
|
234
|
-
|
|
235
|
-
// think it would be preferable to use state (within a separate useEffect) to determine
|
|
236
|
-
// which models get listened to. but as mentioned, that would require the effect to depend
|
|
237
|
-
// on cache keys, which are dynamic, and effects can't have dynamic dependencies right now
|
|
238
|
-
let listenedModels = resources.map(([, config]) => getModelFromCache(config))
|
|
239
|
-
.filter(Boolean);
|
|
240
|
-
|
|
241
|
-
// remove previous listeners and attach new ones
|
|
242
|
-
listenedModelsRef.current.forEach((model) => model.offUpdate(componentRef.current));
|
|
243
|
-
listenedModels.forEach((model) => model.onUpdate(forceUpdate, componentRef.current));
|
|
244
|
-
|
|
245
|
-
listenedModelsRef.current = listenedModels;
|
|
278
|
+
attachModelListeners();
|
|
246
279
|
}
|
|
247
280
|
});
|
|
248
281
|
}
|
|
249
282
|
|
|
250
|
-
|
|
283
|
+
prevPropsRef.current = props;
|
|
284
|
+
// now we can reset this value
|
|
285
|
+
cachedModelsSinceLastEffect.current = {};
|
|
251
286
|
});
|
|
252
287
|
|
|
253
288
|
// this effect attaches listeners to any bypassed models (ie, those passed in) which don't get
|
|
@@ -295,19 +330,6 @@ export const useResources = (getResources, props) => {
|
|
|
295
330
|
...props[ResourcesConfig.queryParamsPropName] || {},
|
|
296
331
|
...resourceState,
|
|
297
332
|
|
|
298
|
-
// helpers
|
|
299
|
-
isOrWillBeLoading: () => {
|
|
300
|
-
var resourcesToUpdate = getResourcesToUpdate(
|
|
301
|
-
resources.filter(withoutNoncritical).filter(withoutPrefetch)
|
|
302
|
-
);
|
|
303
|
-
|
|
304
|
-
// here we only look at resources to fetch, not loaded ones
|
|
305
|
-
return !hasLoaded(criticalLoadingStates) || !!partitionResources(
|
|
306
|
-
resourcesToUpdate,
|
|
307
|
-
// 'next' loading states
|
|
308
|
-
buildResourcesLoadingState(resourcesToUpdate, props)
|
|
309
|
-
)[1].length;
|
|
310
|
-
},
|
|
311
333
|
refetch: (fn) => {
|
|
312
334
|
var refetches = fn(ResourceKeys);
|
|
313
335
|
|
|
@@ -525,20 +547,25 @@ function findCacheKey(resource, getResources, props) {
|
|
|
525
547
|
* @param {object} props - current component props
|
|
526
548
|
* @param {LoadingStates} defaultState - state to return if resource already exists in cache;
|
|
527
549
|
* in most circumstances, this should be the LOADED state as you might expect. However, when we
|
|
528
|
-
* prep resources to be re-fetched
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
550
|
+
* prep resources to be re-fetched, they should get put into a loading state. Because we use
|
|
551
|
+
* render bailouts to immediately update our loading states, a cached resource that no longer has
|
|
552
|
+
* its dependent props will go back to a PENDING state, but that could be just momentarily if the
|
|
553
|
+
* dependent prop gets added in a bailout, triggering an immediate re-render phase with no
|
|
554
|
+
* painting to screen. Lazy models should always be considered LOADED.
|
|
533
555
|
* @return {object} state object with resource state keys as keys and the loading
|
|
534
556
|
* state as values
|
|
535
557
|
*/
|
|
536
558
|
function buildResourcesLoadingState(resources, props, defaultState=LoadingStates.LOADED) {
|
|
537
559
|
return resources.reduce((state, [name, config]) => Object.assign(state, {
|
|
538
|
-
[getResourceState(name)]: !config.refetch &&
|
|
560
|
+
[getResourceState(name)]: !config.refetch && !config.force &&
|
|
561
|
+
(shouldBypassFetch(props, [name, config]) ||
|
|
539
562
|
(getModelFromCache(config) && !getModelFromCache(config).lazy)) ?
|
|
540
563
|
defaultState :
|
|
541
|
-
(!hasAllDependencies(props, [, config]) ? LoadingStates.PENDING :
|
|
564
|
+
(!hasAllDependencies(props, [, config]) ? LoadingStates.PENDING : config.lazy ?
|
|
565
|
+
// any lazy resource should be considered loaded in its resource state, even
|
|
566
|
+
// though it will temporarily show up in the resourcesToUpdate list
|
|
567
|
+
LoadingStates.LOADED :
|
|
568
|
+
LoadingStates.LOADING)
|
|
542
569
|
}), {});
|
|
543
570
|
}
|
|
544
571
|
|
|
@@ -588,6 +615,16 @@ function withoutNoncritical([, config]) {
|
|
|
588
615
|
return !config.noncritical;
|
|
589
616
|
}
|
|
590
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
|
+
|
|
591
628
|
/**
|
|
592
629
|
* This method determines if props with the resource names have been
|
|
593
630
|
* passed to the component directly, in which case we'll skip the
|
|
@@ -722,8 +759,8 @@ function modelAggregator(resources) {
|
|
|
722
759
|
*/
|
|
723
760
|
function partitionResources(resourcesToUpdate, loadingStates) {
|
|
724
761
|
return resourcesToUpdate.reduce((memo, [name, config]) =>
|
|
725
|
-
config.refetch || config.
|
|
726
|
-
|
|
762
|
+
config.refetch || config.force || config.prefetch ||
|
|
763
|
+
!hasLoaded(loadingStates[getResourceState(name)]) || getModelFromCache(config)?.lazy ?
|
|
727
764
|
[memo[0], memo[1].concat([[name, config]])] :
|
|
728
765
|
[memo[0].concat([[name, config]]), memo[1]],
|
|
729
766
|
[[], []]);
|
package/package.json
CHANGED