resourcerer 1.0.0 → 1.0.4

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
@@ -1,6 +1,6 @@
1
1
  <img src="https://user-images.githubusercontent.com/1355779/61337603-f9fffd80-a7ea-11e9-9cb3-fa82e044c86e.png" alt="Resourcerer Icon" height="200" width="200" />
2
2
 
3
- # resourcerer (now in 1.0-beta!)
3
+ # resourcerer
4
4
 
5
5
  `resourcerer` is a library for declaratively fetching and caching your application's data. Its powerful [`useResources`](#useresources) React hook or [`withResources`](#withresources) higher-order React component (HOC) allows you to easily construct a component's data flow, including:
6
6
 
package/lib/model.js CHANGED
@@ -240,10 +240,7 @@ export default class Model {
240
240
  if (!options.wait) {
241
241
  this.set(attrs, options);
242
242
  } else {
243
- // sneaky: this will show up at this point if you .toJSON(), but it will not
244
- // be associated with an update. this is so that in the sync method, they can get all
245
- // attributes via .toJSON()
246
- this.attributes = {...this.attributes, ...attrs};
243
+ options.attrs = {...this.attributes, ...attrs};
247
244
  }
248
245
 
249
246
  options.method = this.isNew() ? 'POST' : options.patch ? 'PATCH' : 'PUT';
package/lib/request.js CHANGED
@@ -54,7 +54,7 @@ export default (key, Model, options={}) => {
54
54
  data: Model && Model.prototype instanceof Collection ? [] : {},
55
55
  params: {},
56
56
  options: {},
57
- fetch: !options.lazy,
57
+ fetch: true,
58
58
  force: false,
59
59
  lazy: false,
60
60
  method: 'GET',
@@ -66,14 +66,17 @@ export default (key, Model, options={}) => {
66
66
  if (!model || model.lazy || options.force) {
67
67
  model = model || new Model(options.data, options.options);
68
68
 
69
- if (options.fetch) {
69
+ if (options.fetch && !options.lazy) {
70
70
  addToLoadingCache = true;
71
71
 
72
- delete model.lazy;
73
-
74
72
  model.fetch({params: options.params}).then(
75
73
  ([newModel, response]) => {
76
74
  delete loadingCache[key];
75
+ // waiting to delete lazy property until after fetch completes ensures multiple
76
+ // components that call non-lazy version of the resource all get put into a
77
+ // loading state
78
+ delete model.lazy;
79
+
77
80
  ModelCache.put(key, newModel, options.component);
78
81
  resolve([newModel, response?.status]);
79
82
  },
@@ -83,7 +86,7 @@ export default (key, Model, options={}) => {
83
86
  }
84
87
  );
85
88
  } else {
86
- // lazy means resolove but don't fetch yet; it will get updates from other components
89
+ // lazy means resolve but don't fetch yet; it will get updates from other components
87
90
  // we only want to do this if a model is not yet in the cache
88
91
  options.lazy && !ModelCache.get(key) ? model.lazy = true : null;
89
92
 
@@ -499,19 +499,20 @@ function findCacheKey(resource, getResources, props) {
499
499
  * @param {object} props - current component props
500
500
  * @param {LoadingStates} defaultState - state to return if resource already exists in cache;
501
501
  * in most circumstances, this should be the LOADED state as you might expect. However, when we
502
- * prep resources to be re-fetched, in order to keep the loading state in sync with its model
503
- * state, even a cached resource should be temporarily in a LOADING state (if cached, this will
504
- * be corrected in the next microtask and before the JS stack is completed, so the LOADING state
505
- * should never be shown in the UI, despite render being called multiple times).
502
+ * prep resources to be re-fetched or if models were previously created lazily (without fetching),
503
+ * in order to keep the loading state in sync with its model state, even a cached resource should
504
+ * be temporarily in a LOADING state (if cached, this will be corrected in the next microtask and
505
+ * before the JS stack is completed, so the LOADING state should never be shown in the UI, despite
506
+ * render being called multiple times).
506
507
  * @return {object} state object with resource state keys as keys and the loading
507
508
  * state as values
508
509
  */
509
510
  function buildResourcesLoadingState(resources, props, defaultState=LoadingStates.LOADED) {
510
511
  return resources.reduce((state, [name, config]) => Object.assign(state, {
511
- [getResourceState(name)]:
512
- !config.refetch && (shouldBypassFetch(props, [name, config]) || getModelFromCache(config)) ?
513
- defaultState :
514
- (!hasAllDependencies(props, [, config]) ? LoadingStates.PENDING : LoadingStates.LOADING)
512
+ [getResourceState(name)]: !config.refetch && (shouldBypassFetch(props, [name, config]) ||
513
+ (getModelFromCache(config) && !getModelFromCache(config).lazy)) ?
514
+ defaultState :
515
+ (!hasAllDependencies(props, [, config]) ? LoadingStates.PENDING : LoadingStates.LOADING)
515
516
  }), {});
516
517
  }
517
518
 
@@ -683,10 +684,10 @@ function modelAggregator(resources) {
683
684
 
684
685
  /**
685
686
  * Separates out resources between those that are loading and those that have loaded. The latter
686
- * won't need to get passed to fetchResources, but the former will. Make sure that prefetched
687
- * resources get added to those that are loading so that the request gets made. Since prefetched
688
- * models don't factor into loading states, there's no concern that they get passed to
689
- * fetchResources even if they have been cached.
687
+ * won't need to get passed to fetchResources, but the former will. Make sure that prefetched,
688
+ * refetched, and lazy resources get added to those that are loading so that the request gets made.
689
+ * Since prefetched models don't factor into loading states, there's no concern that they get passed
690
+ * to fetchResources even if they have been cached.
690
691
  *
691
692
  * @param {array[]} resourcesToUpdate - resource configs to partition
692
693
  * @param {object} loadingStates - contains upcoming loading states for each resource
@@ -695,7 +696,8 @@ function modelAggregator(resources) {
695
696
  */
696
697
  function partitionResources(resourcesToUpdate, loadingStates) {
697
698
  return resourcesToUpdate.reduce((memo, [name, config]) =>
698
- config.refetch || config.prefetch || !hasLoaded(loadingStates[getResourceState(name)]) ?
699
+ config.refetch || config.prefetch || !hasLoaded(loadingStates[getResourceState(name)]) ||
700
+ getModelFromCache(config)?.lazy ?
699
701
  [memo[0], memo[1].concat([[name, config]])] :
700
702
  [memo[0].concat([[name, config]]), memo[1]],
701
703
  [[], []]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "repository": "github.com/noahgrant/resourcerer",
5
5
  "description": "Declarative data-fetching and caching framework for React",
6
6
  "keywords": [
@@ -314,7 +314,7 @@ describe('Model', () => {
314
314
 
315
315
  result = model.save({three: 'three'}, {wait: true});
316
316
  expect(callback).not.toHaveBeenCalled();
317
- expect(model.toJSON()).toEqual({one: 'one', three: 'three'});
317
+ expect(model.toJSON()).toEqual({one: 'one'});
318
318
 
319
319
  await result;
320
320
 
@@ -306,12 +306,17 @@ describe('Request', () => {
306
306
  });
307
307
 
308
308
  it('fetches when another component requests the same model without lazy option', async() => {
309
- var [model] = await request('lazy', Model, {component, lazy: true});
309
+ var [model] = await request('lazy', Model, {component, lazy: true}),
310
+ requestPromise;
310
311
 
311
312
  expect(model.lazy).toBe(true);
312
313
  expect(Model.prototype.fetch).not.toHaveBeenCalled();
313
314
 
314
- [model] = await request('lazy', Model, {component});
315
+ requestPromise = request('lazy', Model, {component});
316
+
317
+ expect(model.lazy).toBeDefined();
318
+ await requestPromise;
319
+
315
320
  expect(model.lazy).not.toBeDefined();
316
321
  expect(Model.prototype.fetch).toHaveBeenCalled();
317
322