resourcerer 1.1.6 → 1.1.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/index.d.ts CHANGED
@@ -48,7 +48,7 @@ declare module 'resourcerer' {
48
48
 
49
49
  fetch(options?: {parse?: boolean} & SyncOptions & SetOptions): Promise<[Model<T>, Response]>;
50
50
 
51
- save(attrs: Partial<T>, options?: {wait?: boolean} & SyncOptions & SetOptions):
51
+ save(attrs: Partial<T>, options?: {wait?: boolean; patch?: boolean;} & SyncOptions & SetOptions):
52
52
  Promise<[Model<T>, Response]>;
53
53
 
54
54
  destroy(options?: {wait?: boolean} & SyncOptions & SetOptions): Promise<[Model<T>, Response]>;
@@ -76,7 +76,7 @@ declare module 'resourcerer' {
76
76
 
77
77
  declare class Collection<
78
78
  T extends Record<string, any> = {[key: string]: any},
79
- O extends Record<string, any> & CSetOptions = {[key: string]: any}
79
+ O extends Record<string, any> & {Model?: new () => Model;} & CSetOptions = {[key: string]: any}
80
80
  > {
81
81
  constructor(
82
82
  models?: ModelArg<T> | ModelArg<T>[],
@@ -166,9 +166,13 @@ export const useResources = (getResources, props) => {
166
166
  // because we don't go through the request module for normal-flow resources that are cached,
167
167
  // we need to re-register this component with the resource. this will also clear any timeouts
168
168
  // for removing the resource.
169
- loadedResources.map(
170
- ([, config]) => ModelCache.register(getCacheKey(config), componentRef.current)
171
- );
169
+ loadedResources.forEach(([, config]) => {
170
+ var cacheKey = getCacheKey(config);
171
+
172
+ ModelCache.register(cacheKey, componentRef.current);
173
+ // make sure we add any provided props for serial requests
174
+ provideProps(ModelCache.get(cacheKey), config.provides, props, setResourceState);
175
+ });
172
176
 
173
177
  // NOTE: changing this to resourcesToFetch causes some inexplicable bugs around cached resources
174
178
  // and a UI that wouldn't update. so this is kept as resourcesToUpdate and fetchResources is
@@ -876,18 +880,7 @@ function fetchResources(resources, props, {
876
880
  // don't continue unless component is still mounted and resource is current
877
881
  if (isCurrentResource([name, config], cacheKey)) {
878
882
  // add any dependencies this resource might provide for other resources
879
- if (Object.entries(provides).length) {
880
- setResourceState((state) => ({
881
- ...state,
882
- ...Object.entries(provides).reduce((memo, [provide, transform]) => Object.assign({
883
- memo,
884
- ...(provide === SPREAD_PROVIDES_CHAR ?
885
- transform(model, props) :
886
- {[provide]: transform(model, props)})
887
- }), {})
888
- }));
889
- }
890
-
883
+ provideProps(model, provides, props, setResourceState);
891
884
  onRequestSuccess(model, status, [name, config]);
892
885
  }
893
886
  },
@@ -904,3 +897,25 @@ function fetchResources(resources, props, {
904
897
  })
905
898
  );
906
899
  }
900
+
901
+ /**
902
+ * Add any dependencies that the model provides as resource state.
903
+ *
904
+ * @param {Model|Collection} model
905
+ * @param {object} provides - config provides object
906
+ * @param {object} props - current component props
907
+ * @param {function} setResourceState - updates resource state
908
+ */
909
+ function provideProps(model, provides={}, props, setResourceState) {
910
+ if (Object.entries(provides).length) {
911
+ setResourceState((state) => ({
912
+ ...state,
913
+ ...Object.entries(provides).reduce((memo, [provide, transform]) => Object.assign({
914
+ memo,
915
+ ...(provide === SPREAD_PROVIDES_CHAR ?
916
+ transform(model, props) :
917
+ {[provide]: transform(model, props)})
918
+ }), {})
919
+ }));
920
+ }
921
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "repository": "github.com/noahgrant/resourcerer",
5
5
  "description": "Declarative data-fetching and caching framework for React",
6
6
  "keywords": ["declarative data-fetching framework", "react"],