resourcerer 1.0.5 → 1.0.8

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/collection.js CHANGED
@@ -471,7 +471,7 @@ export default class Collection {
471
471
  return attrs;
472
472
  }
473
473
 
474
- return new this.Model(attrs, {...options, collection: this});
474
+ return new this.Model(attrs, {...this.urlOptions, ...options, collection: this});
475
475
  }
476
476
 
477
477
  /**
package/lib/model.js CHANGED
@@ -3,6 +3,16 @@ import {isDeepEqual, result, uniqueId, urlError} from './utils';
3
3
  import Events from './events';
4
4
  import sync from './sync';
5
5
 
6
+ const RESERVED_OPTION_KEYS = [
7
+ 'Model',
8
+ 'comparator',
9
+ 'parse',
10
+ 'collection',
11
+ 'silent',
12
+ 'method',
13
+ 'unset'
14
+ ];
15
+
6
16
  /**
7
17
  * The Model class should be extended for any resource that is singular, as in not a list of items.
8
18
  * It is the core class used to represent server data in the client application (Collections are
@@ -26,8 +36,6 @@ export default class Model {
26
36
  * * ...any other options that .set() takes
27
37
  */
28
38
  constructor(attributes={}, options={}) {
29
- const RESERVED_OPTION_KEYS = ['parse', 'collection', 'silent', 'unset'];
30
-
31
39
  this.cid = uniqueId('c');
32
40
  this.attributes = {};
33
41
 
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
@@ -108,14 +108,15 @@ export const useResources = (getResources, props) => {
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 prevConfig = findConfig([name, config], getResources, prevPropsRef.current || {}),
112
- previousCacheKey = prevPropsRef.current && getCacheKey(prevConfig);
111
+ var prevConfig = prevPropsRef.current &&
112
+ findConfig([name, config], getResources, prevPropsRef.current),
113
+ previousCacheKey = prevConfig && getCacheKey(prevConfig);
113
114
 
114
115
  return (!previousCacheKey || previousCacheKey !== getCacheKey(config) ||
115
116
  !hasAllDependencies(prevPropsRef.current, [, config]) || config.refetch ||
116
117
  // make sure if we were lazy and are no longer lazy (from the same component) that we
117
118
  // get included in the list to get updated
118
- (prevConfig.lazy && !config.lazy));
119
+ (prevConfig?.lazy && !config.lazy));
119
120
  });
120
121
 
121
122
  /**
@@ -162,6 +163,13 @@ export const useResources = (getResources, props) => {
162
163
  setModels(modelAggregator(pendingResources.concat(loadedResources).filter(withoutPrefetch)));
163
164
  }
164
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
+
165
173
  // NOTE: changing this to resourcesToFetch causes some inexplicable bugs around cached resources
166
174
  // and a UI that wouldn't update. so this is kept as resourcesToUpdate and fetchResources is
167
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.5",
3
+ "version": "1.0.8",
4
4
  "repository": "github.com/noahgrant/resourcerer",
5
5
  "description": "Declarative data-fetching and caching framework for React",
6
6
  "keywords": [
@@ -50,6 +50,19 @@ describe('Collection', () => {
50
50
  expect(collection.toJSON()).toEqual([]);
51
51
  });
52
52
 
53
+ it('urlOptions get passed down to models', async() => {
54
+ collection = new Collection([{id: '1'}], {one: 1, two: 2, comparator: 'foo', parse: true});
55
+
56
+ expect(collection.get('1').urlOptions).toEqual({one: 1, two: 2});
57
+
58
+ collection.add({id: '2'});
59
+ expect(collection.get('2').urlOptions).toEqual({one: 1, two: 2});
60
+
61
+ sync.default.mockResolvedValue([[{id: '3'}], {}]);
62
+ await collection.fetch();
63
+ expect(collection.get('3').urlOptions).toEqual({one: 1, two: 2});
64
+ });
65
+
53
66
  it('adds any models passed to its models list', () => {
54
67
  var model1 = new Model,
55
68
  model2 = new Model;