resourcerer 1.0.4 → 1.0.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/lib/request.js CHANGED
@@ -93,6 +93,10 @@ export default (key, Model, options={}) => {
93
93
  ModelCache.put(key, model, options.component);
94
94
  resolve([model]);
95
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
96
100
  } else {
97
101
  // this will cancel any in-flight timeouts and add the component
98
102
  // to the list of components using the resource
@@ -101,17 +101,22 @@ export const useResources = (getResources, props) => {
101
101
  listenedModelsRef = useRef([]),
102
102
 
103
103
  forceUpdate = useForceUpdate(),
104
- getPrevCacheKey = (rsrc) => findCacheKey(rsrc, getResources, prevPropsRef.current),
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 previousCacheKey = prevPropsRef.current && getPrevCacheKey([name, config]);
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
  /**
@@ -158,6 +163,13 @@ export const useResources = (getResources, props) => {
158
163
  setModels(modelAggregator(pendingResources.concat(loadedResources).filter(withoutPrefetch)));
159
164
  }
160
165
 
166
+ // because we don't go through the request module for normal-flow resources that are cached,
167
+ // we need to re-register this component with the resource. this will also clear any timeouts
168
+ // for removing the resource.
169
+ loadedResources.map(
170
+ ([, config]) => ModelCache.register(getCacheKey(config), componentRef.current)
171
+ );
172
+
161
173
  // NOTE: changing this to resourcesToFetch causes some inexplicable bugs around cached resources
162
174
  // and a UI that wouldn't update. so this is kept as resourcesToUpdate and fetchResources is
163
175
  // given resourcesToFetch. Because we are still only fetching resourcesToFetch and because any
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "1.0.4",
3
+ "version": "1.0.7",
4
4
  "repository": "github.com/noahgrant/resourcerer",
5
5
  "description": "Declarative data-fetching and caching framework for React",
6
6
  "keywords": [