resourcerer 1.0.11 → 1.0.13

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 CHANGED
@@ -755,7 +755,7 @@ The hook and HOC largely operate interchangeably, but do note a couple critical
755
755
 
756
756
  `resourcerer` handles resource storage and caching, so that when multiple components request the same resource with the same parameters or the same body, they receive the same model in response. If multiple components request a resource still in-flight, only a single request is made, and each component awaits the return of the same resource. Fetched resources are stored in the `ModelCache`. Under most circumstances, you won’t need to interact with directly; but it’s still worth knowing a little bit about what it does.
757
757
 
758
- The `ModelCache` is a simple module that contains a couple of Maps&mdash;one that is the actual cache `{cacheKey<string>: model<Model|Collection>}`, and one that is a component manifest, keeping track of all component instances that are using a given resource (unique by cache key). When a component unmounts, `resourcerer` will unregister the component instance from the component manifest; if a resource no longer has any component instances attached, it gets scheduled for cache removal. The timeout period for cache removal is two minutes by default (but can be changed, see [Configuring resourcerer](#configuring-resourcerer)), to allow navigating back and forth between pages without requiring a refetch of all resources. After the timeout, if no other new component instances have requested the resource, it’s removed from the `ModelCache`. Any further requests for that resource then go back through the network.
758
+ The `ModelCache` is a simple module that contains a couple of Maps&mdash;one that is the actual cache `{cacheKey<string>: model<Model|Collection>}`, and one that is a component manifest, keeping track of all component instances that are using a given resource (unique by cache key). When a component unmounts, `resourcerer` will unregister the component instance from the component manifest; if a resource no longer has any component instances attached, it gets scheduled for cache removal. The timeout period for cache removal is two minutes by default (but can be changed, see [Configuring resourcerer](#configuring-resourcerer), or [overridden on a model-class basis](/docs/model.md#static-cachetimeout)), to allow navigating back and forth between pages without requiring a refetch of all resources. After the timeout, if no other new component instances have requested the resource, it’s removed from the `ModelCache`. Any further requests for that resource then go back through the network.
759
759
 
760
760
  Again, it’s unlikely that you’ll use `ModelCache` directly while using `resourcerer`, but it’s helpful to know a bit about what’s going on behind-the-scenes.
761
761
 
@@ -900,7 +900,7 @@ ResourcesConfig.set(configObj);
900
900
 
901
901
  `ResourcesConfig.set` accepts an object with any of the following properties:
902
902
 
903
- * `cacheGracePeriod` (number in ms): the length of time a resource will be kept in the cache after being scheduled for removal (see the [caching section](#caching-resources-with-modelcache) for more). **Default:** 120000 (2 minutes).
903
+ * `cacheGracePeriod` (number in ms): the length of time a resource will be kept in the cache after being scheduled for removal (see the [caching section](#caching-resources-with-modelcache) for more). **Default:** 120000 (2 minutes). Note that each model class can provide its own timeout override.
904
904
 
905
905
  * `errorBoundaryChild` (JSX/React.Element): the element or component that should be rendered in the ErrorBoundary included in every `withResources` wrapping. By default, a caught error renders this child:
906
906
 
@@ -113,9 +113,10 @@ export default {
113
113
  * @param {string} cacheKey - The cache key of the model to be removed.
114
114
  */
115
115
  function scheduleForRemoval(cacheKey) {
116
- timeouts[cacheKey] = window.setTimeout(() => {
117
- clearModel(cacheKey);
118
- }, ResourcesConfig.cacheGracePeriod);
116
+ var timeout = modelCache.get(cacheKey)?.constructor.cacheTimeout ||
117
+ ResourcesConfig.cacheGracePeriod;
118
+
119
+ timeouts[cacheKey] = window.setTimeout(() => clearModel(cacheKey), timeout);
119
120
  }
120
121
 
121
122
  /**
@@ -115,8 +115,8 @@ export const useResources = (getResources, props) => {
115
115
  return (!previousCacheKey || previousCacheKey !== getCacheKey(config) ||
116
116
  !hasAllDependencies(prevPropsRef.current, [, config]) || config.refetch ||
117
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));
118
+ // get included in the list to get updated (and vice versa)
119
+ (prevConfig?.lazy !== config.lazy));
120
120
  });
121
121
 
122
122
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "repository": "github.com/noahgrant/resourcerer",
5
5
  "description": "Declarative data-fetching and caching framework for React",
6
6
  "keywords": [
@@ -1,3 +1,5 @@
1
+ import Collection from '../lib/collection';
2
+ import Model from '../lib/model';
1
3
  import ModelCache from '../lib/model-cache';
2
4
 
3
5
  const CACHE_WAIT = 150000,
@@ -51,6 +53,31 @@ describe('ModelCache', () => {
51
53
  jest.advanceTimersByTime(CACHE_WAIT);
52
54
  expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
53
55
  });
56
+
57
+ it('uses a model-specific timeout if available', () => {
58
+ class TimeoutModel extends Model {
59
+ static cacheTimeout = 3000;
60
+ }
61
+
62
+ class TimeoutCollection extends Collection {
63
+ static cacheTimeout = CACHE_WAIT + 2000;
64
+ }
65
+
66
+ jest.advanceTimersByTime(CACHE_WAIT);
67
+ expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
68
+
69
+ ModelCache.put(PUT_KEY, new TimeoutModel());
70
+ jest.advanceTimersByTime(2000);
71
+ expect(ModelCache.get(PUT_KEY)).toBeDefined();
72
+ jest.advanceTimersByTime(2000);
73
+ expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
74
+
75
+ ModelCache.put(PUT_KEY, new TimeoutCollection());
76
+ jest.advanceTimersByTime(CACHE_WAIT);
77
+ expect(ModelCache.get(PUT_KEY)).toBeDefined();
78
+ jest.advanceTimersByTime(2000);
79
+ expect(ModelCache.get(PUT_KEY)).not.toBeDefined();
80
+ });
54
81
  });
55
82
  });
56
83