react-redux-cache 0.0.5 → 0.0.6

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/dist/index.js CHANGED
@@ -53,6 +53,7 @@ Object.defineProperty(exports, "processEntityChanges", { enumerable: true, get:
53
53
  // make error type generic
54
54
  // proper types, remove as, any, todo
55
55
  // ! low
56
+ // remove defaultState and keep values undefined?
56
57
  // add params to the state?
57
58
  // cancellation to queries
58
59
  // if mutation & query alrady loading - make options: last, throttle, debounce, parallel?
package/dist/useQuery.js CHANGED
@@ -24,6 +24,7 @@ exports.queryCacheOptionsByPolicy = {
24
24
  'cache-and-fetch': Object.assign(Object.assign({}, cacheFirstOptions), { policy: 'cache-and-fetch' }),
25
25
  };
26
26
  exports.defaultQueryCacheOptions = cacheFirstOptions;
27
+ const defaultRefState = {};
27
28
  const useQuery = (cache, options) => {
28
29
  var _a, _b, _c, _d;
29
30
  const getParamsKey = (_a = cache.queries[options.query].getParamsKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetParamsKey);
@@ -56,39 +57,43 @@ const useQuery = (cache, options) => {
56
57
  const forceUpdate = (0, utilsAndConstants_1.useForceUpdate)();
57
58
  // Keeps most of local state.
58
59
  // Reference because state is changed not only by changing hook arguments, but also by calling fetch, and it should be done synchronously.
59
- const stateRef = (0, react_1.useRef)({});
60
+ const stateRef = (0, react_1.useRef)(defaultRefState);
60
61
  (0, react_1.useMemo)(() => {
61
- if (skip || stateRef.current.paramsKey === hookParamsKey) {
62
+ if (stateRef.current.paramsKey === hookParamsKey) {
62
63
  return;
63
64
  }
64
- const resultSelectorImpl = cache.queries[queryKey].resultSelector;
65
65
  const state = stateRef.current;
66
+ state.resultSelector = createResultSelector(
67
+ // @ts-expect-error fix later
68
+ cache.queries[queryKey].resultSelector, cache.cacheStateSelector, hookParams);
69
+ if (skip) {
70
+ return;
71
+ }
66
72
  state.params = hookParams;
67
73
  state.paramsKey = hookParamsKey;
68
74
  state.latestHookParamsKey = state.paramsKey;
69
75
  // @ts-expect-error fix later
70
76
  state.cacheKey = getCacheKey(hookParams);
71
- state.resultSelector = createResultSelector(
72
- // @ts-expect-error fix later
73
- resultSelectorImpl, cache.cacheStateSelector, hookParams);
74
77
  // eslint-disable-next-line react-hooks/exhaustive-deps
75
78
  }, [hookParamsKey, skip]);
76
79
  const resultFromSelector =
77
80
  // eslint-disable-next-line react-hooks/rules-of-hooks
78
81
  stateRef.current.resultSelector && (0, react_redux_1.useSelector)(stateRef.current.resultSelector);
79
82
  const hasResultFromSelector = resultFromSelector !== undefined;
80
- const queryStateSelector = (0, react_1.useCallback)((state) => {
83
+ const queryStateSelector = (0, react_1.useCallback)((state, cacheKey = stateRef.current.cacheKey) => {
81
84
  cache.options.logsEnabled &&
82
85
  (0, utilsAndConstants_1.log)('queryStateSelector', {
83
86
  state,
84
87
  queryKey,
85
- cacheKey: stateRef.current.cacheKey,
88
+ cacheKey,
86
89
  cacheState: cache.cacheStateSelector(state),
87
90
  });
88
- const queryState = cache.cacheStateSelector(state).queries[queryKey][stateRef.current.cacheKey];
91
+ const queryState = cache.cacheStateSelector(state).queries[queryKey][cacheKey];
89
92
  return queryState; // TODO proper type
90
- // eslint-disable-next-line react-hooks/exhaustive-deps
91
- }, []);
93
+ },
94
+ // cacheKey needed only to re-evaluate queryStateSelector later
95
+ // eslint-disable-next-line react-hooks/exhaustive-deps
96
+ [stateRef.current.cacheKey]);
92
97
  const queryStateFromSelector = (_d = (0, react_redux_1.useSelector)(queryStateSelector)) !== null && _d !== void 0 ? _d : utilsAndConstants_1.defaultQueryMutationState;
93
98
  const queryState = hasResultFromSelector
94
99
  ? (Object.assign(Object.assign({}, queryStateFromSelector), { result: resultFromSelector }))
@@ -99,11 +104,11 @@ const useQuery = (cache, options) => {
99
104
  if (queryState.loading) {
100
105
  return;
101
106
  }
107
+ const { params, cacheKey } = stateRef.current;
102
108
  cacheOptions.cacheQueryState &&
103
- store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, stateRef.current.cacheKey, {
109
+ store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
104
110
  loading: true,
105
111
  }));
106
- const { paramsKey, params } = stateRef.current;
107
112
  let response;
108
113
  const fetchFn = cache.queries[queryKey].query;
109
114
  try {
@@ -112,17 +117,14 @@ const useQuery = (cache, options) => {
112
117
  params);
113
118
  }
114
119
  catch (error) {
115
- if (stateRef.current.paramsKey === paramsKey && cacheOptions.cacheQueryState) {
116
- store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, stateRef.current.cacheKey, {
117
- error: error,
118
- loading: false,
119
- }));
120
- }
120
+ store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
121
+ error: error,
122
+ loading: false,
123
+ }));
121
124
  }
122
- if (response && stateRef.current.paramsKey === paramsKey) {
123
- store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, stateRef.current.cacheKey, !cacheOptions.cacheQueryState
124
- ? undefined
125
- : {
125
+ if (response) {
126
+ const newState = cacheOptions.cacheQueryState
127
+ ? {
126
128
  error: undefined,
127
129
  loading: false,
128
130
  result: hasResultFromSelector
@@ -130,13 +132,18 @@ const useQuery = (cache, options) => {
130
132
  : mergeResults
131
133
  ? mergeResults(
132
134
  // @ts-expect-error fix later
133
- (_e = queryStateSelector(store.getState())) === null || _e === void 0 ? void 0 : _e.result, response, params)
135
+ (_e = queryStateSelector(store.getState(), cacheKey)) === null || _e === void 0 ? void 0 : _e.result, response, params)
134
136
  : response.result,
135
- }, cacheOptions.cacheEntities ? response : undefined));
137
+ }
138
+ : undefined;
139
+ store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, newState, cacheOptions.cacheEntities ? response : undefined));
136
140
  }
137
141
  // eslint-disable-next-line react-hooks/exhaustive-deps
138
142
  }), [mergeResults, queryState.loading, hasResultFromSelector]);
139
143
  (0, react_1.useEffect)(() => {
144
+ if (skip) {
145
+ return;
146
+ }
140
147
  if (queryState.result != null && cacheOptions.policy === 'cache-first') {
141
148
  return;
142
149
  }
@@ -165,6 +172,13 @@ const useQuery = (cache, options) => {
165
172
  },
166
173
  // eslint-disable-next-line react-hooks/exhaustive-deps
167
174
  [fetchImpl, getCacheKey]);
175
+ cache.options.logsEnabled &&
176
+ console.debug('[useQuery]', {
177
+ state: stateRef.current,
178
+ options,
179
+ resultFromSelector,
180
+ queryState,
181
+ });
168
182
  return [queryState, fetch];
169
183
  };
170
184
  exports.useQuery = useQuery;
@@ -9,6 +9,7 @@ export declare const defaultCacheOptions: {
9
9
  };
10
10
  export declare const defaultQueryMutationState: {
11
11
  readonly loading: false;
12
+ readonly error: undefined;
12
13
  };
13
14
  export declare const defaultGetParamsKey: <P = unknown>(params: P) => string;
14
15
  /**
@@ -18,7 +18,7 @@ exports.defaultCacheOptions = {
18
18
  validateFunctionArguments: true,
19
19
  validateHookArguments: true,
20
20
  };
21
- exports.defaultQueryMutationState = { loading: false };
21
+ exports.defaultQueryMutationState = { loading: false, error: undefined };
22
22
  const defaultGetParamsKey = (params) => {
23
23
  switch (typeof params) {
24
24
  case 'string':
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "react-redux-cache",
3
3
  "author": "Alexander Danilov",
4
4
  "license": "MIT",
5
- "version": "0.0.5",
5
+ "version": "0.0.6",
6
6
  "description": "Powerful data fetching and caching library that supports normalization, built on top of redux",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  "example": "(cd example && yarn && yarn start)",
11
11
  "clean": "rm -rf dist",
12
12
  "build": "yarn clean && tsc && yarn pack -f package.tgz",
13
- "test": "node node_modules/jest/bin/jest.js",
13
+ "test": "node node_modules/jest/bin/jest.js --env=jsdom",
14
14
  "prepublishOnly": "yarn build && yarn test"
15
15
  },
16
16
  "devDependencies": {