resourcerer 1.0.6 → 1.0.9

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
@@ -163,6 +163,13 @@ export const useResources = (getResources, props) => {
163
163
  setModels(modelAggregator(pendingResources.concat(loadedResources).filter(withoutPrefetch)));
164
164
  }
165
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
+
166
173
  // NOTE: changing this to resourcesToFetch causes some inexplicable bugs around cached resources
167
174
  // and a UI that wouldn't update. so this is kept as resourcesToUpdate and fetchResources is
168
175
  // given resourcesToFetch. Because we are still only fetching resourcesToFetch and because any
@@ -197,9 +204,7 @@ export const useResources = (getResources, props) => {
197
204
  // https://stackoverflow.com/questions/48563650/
198
205
  // does-react-keep-the-order-for-state-updates/48610973#48610973
199
206
  ReactDOM.unstable_batchedUpdates(() => {
200
- if (resources.filter(withoutPrefetch).length) {
201
- setModels(modelAggregator(resources.filter(withoutPrefetch)));
202
- }
207
+ setModels(modelAggregator([[name, config]]));
203
208
 
204
209
  loaderDispatch({
205
210
  type: LoadingStates.LOADED,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
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;