react-redux-cache 0.7.0-rc.1 → 0.7.1

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
@@ -291,17 +291,22 @@ export const updateBank = (bank) => {
291
291
 
292
292
  #### Invalidation
293
293
 
294
- `cache-first` cache policy (default) skips fetching on component mount if result is already cached, but we can invalidate cached query results using `invalidateQuery` action to make it run again on a next mount. Also, we can return this action as a part of query or mutation response, to perform it in one batch update.
294
+ `cache-first` cache policy (default) skips fetching on component mount if result is already cached, but we can invalidate cached query results using `invalidateQuery` action to make it run again on a next mount.
295
295
 
296
296
  ```typescript
297
- const updateUser = (user: Partial<User>) => {
298
- const response = ...
299
- return {
300
- result: response.result,
301
- expiresAt: Date.now() + 10000, // optional, expiration time of current response
302
- actions: [invalidateQuery([{ key: 'getUsers' }])] // optional, this action sets `expiresAt` field of `getUsers` states to Date.now()
303
- }
304
- }
297
+
298
+ export const cache = createCache({
299
+ ...
300
+ mutations: {
301
+ updateUser: {
302
+ mutation: updateUser,
303
+ onSuccess(_, __, {dispatch}) {
304
+ // we invalidate getUsers after a single user update (can be done better by updating getUsers state with updateQueryStateAndEntities)
305
+ dispatch(cache.actions.invalidateQuery([{query: 'getUsers'}]))
306
+ },
307
+ },
308
+ },
309
+ })
305
310
  ```
306
311
 
307
312
  #### Extended cache policy
@@ -34,7 +34,7 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
34
34
  invalidateQuery: {
35
35
  <K_2 extends keyof QP & keyof QR>(queries: {
36
36
  /** Query key */
37
- key: K_2;
37
+ query: K_2;
38
38
  /** Query cache key */
39
39
  cacheKey?: Key | undefined;
40
40
  /** Unix timestamp at which query expires. Is set to the query state. @default Date.now() */
@@ -43,7 +43,7 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
43
43
  type: `@rrc/${N}/invalidateQuery`;
44
44
  queries: {
45
45
  /** Query key */
46
- key: K_2;
46
+ query: K_2;
47
47
  /** Query cache key */
48
48
  cacheKey?: Key | undefined;
49
49
  /** Unix timestamp at which query expires. Is set to the query state. @default Date.now() */
@@ -57,14 +57,14 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
57
57
  clearQueryState: {
58
58
  <K_3 extends keyof QP & keyof QR>(queries: {
59
59
  /** Query key */
60
- key: K_3;
60
+ query: K_3;
61
61
  /** Query cache key */
62
62
  cacheKey?: Key | undefined;
63
63
  }[]): {
64
64
  type: `@rrc/${N}/clearQueryState`;
65
65
  queries: {
66
66
  /** Query key */
67
- key: K_3;
67
+ query: K_3;
68
68
  /** Query cache key */
69
69
  cacheKey?: Key | undefined;
70
70
  }[];
@@ -30,14 +30,14 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
30
30
  } | {
31
31
  type: `@rrc/${N}/invalidateQuery`;
32
32
  queries: {
33
- key: keyof QP & keyof QR;
33
+ query: keyof QP & keyof QR;
34
34
  cacheKey?: Key | undefined;
35
35
  expiresAt?: number | undefined;
36
36
  }[];
37
37
  } | {
38
38
  type: `@rrc/${N}/clearQueryState`;
39
39
  queries: {
40
- key: keyof QP & keyof QR;
40
+ query: keyof QP & keyof QR;
41
41
  cacheKey?: Key | undefined;
42
42
  }[];
43
43
  } | {
@@ -77,13 +77,13 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
77
77
  };
78
78
  invalidateQuery: {
79
79
  <K_2 extends keyof QP & keyof QR>(queries: {
80
- key: K_2;
80
+ query: K_2;
81
81
  cacheKey?: Key | undefined;
82
82
  expiresAt?: number | undefined;
83
83
  }[]): {
84
84
  type: `@rrc/${N}/invalidateQuery`;
85
85
  queries: {
86
- key: K_2;
86
+ query: K_2;
87
87
  cacheKey?: Key | undefined;
88
88
  expiresAt?: number | undefined;
89
89
  }[];
@@ -92,12 +92,12 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
92
92
  };
93
93
  clearQueryState: {
94
94
  <K_3 extends keyof QP & keyof QR>(queries: {
95
- key: K_3;
95
+ query: K_3;
96
96
  cacheKey?: Key | undefined;
97
97
  }[]): {
98
98
  type: `@rrc/${N}/clearQueryState`;
99
99
  queries: {
100
- key: K_3;
100
+ query: K_3;
101
101
  cacheKey?: Key | undefined;
102
102
  }[];
103
103
  };
@@ -22,14 +22,14 @@ export declare const createCacheReducer: <N extends string, T extends Typenames,
22
22
  } | {
23
23
  type: `@rrc/${N}/invalidateQuery`;
24
24
  queries: {
25
- key: keyof QP & keyof QR;
25
+ query: keyof QP & keyof QR;
26
26
  cacheKey?: import("./types").Key | undefined;
27
27
  expiresAt?: number | undefined;
28
28
  }[];
29
29
  } | {
30
30
  type: `@rrc/${N}/clearQueryState`;
31
31
  queries: {
32
- key: keyof QP & keyof QR;
32
+ query: keyof QP & keyof QR;
33
33
  cacheKey?: import("./types").Key | undefined;
34
34
  }[];
35
35
  } | {
@@ -96,20 +96,20 @@ const createCacheReducer = (actions, typenames, queryKeys, cacheOptions) => {
96
96
  }
97
97
  const now = Date.now();
98
98
  let newQueries = undefined;
99
- for (const { key, cacheKey, expiresAt = now } of queriesToInvalidate) {
100
- const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[key];
99
+ for (const { query: queryKey, cacheKey, expiresAt = now } of queriesToInvalidate) {
100
+ const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[queryKey];
101
101
  if (cacheKey != null) {
102
102
  if (queryStates[cacheKey]) {
103
103
  const queryState = queryStates[cacheKey];
104
104
  if (queryState && queryState.expiresAt !== expiresAt) {
105
105
  newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
106
- if (state.queries[key] === newQueries[key]) {
107
- newQueries[key] = Object.assign({}, newQueries[key]);
106
+ if (state.queries[queryKey] === newQueries[queryKey]) {
107
+ newQueries[queryKey] = Object.assign({}, newQueries[queryKey]);
108
108
  }
109
109
  // @ts-expect-error fix type later
110
- newQueries[key][cacheKey] = Object.assign(Object.assign({}, queryState), { expiresAt });
110
+ newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), { expiresAt });
111
111
  if (expiresAt === undefined) {
112
- delete newQueries[key][cacheKey].expiresAt;
112
+ delete newQueries[queryKey][cacheKey].expiresAt;
113
113
  }
114
114
  }
115
115
  }
@@ -119,12 +119,12 @@ const createCacheReducer = (actions, typenames, queryKeys, cacheOptions) => {
119
119
  const queryState = queryStates[cacheKey];
120
120
  if (queryState && queryState.expiresAt !== expiresAt) {
121
121
  newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
122
- if (state.queries[key] === newQueries[key]) {
123
- newQueries[key] = Object.assign({}, newQueries[key]);
122
+ if (state.queries[queryKey] === newQueries[queryKey]) {
123
+ newQueries[queryKey] = Object.assign({}, newQueries[queryKey]);
124
124
  }
125
- newQueries[key][cacheKey] = Object.assign(Object.assign({}, queryState), { expiresAt });
125
+ newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), { expiresAt });
126
126
  if (expiresAt === undefined) {
127
- delete newQueries[key][cacheKey].expiresAt;
127
+ delete newQueries[queryKey][cacheKey].expiresAt;
128
128
  }
129
129
  }
130
130
  }
@@ -140,20 +140,20 @@ const createCacheReducer = (actions, typenames, queryKeys, cacheOptions) => {
140
140
  return state;
141
141
  }
142
142
  let newQueries = undefined;
143
- for (const { key, cacheKey } of queriesToClear) {
144
- const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[key];
143
+ for (const { query: queryKey, cacheKey } of queriesToClear) {
144
+ const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[queryKey];
145
145
  if (cacheKey != null) {
146
146
  if (queryStates[cacheKey]) {
147
147
  newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
148
- if (state.queries[key] === newQueries[key]) {
149
- newQueries[key] = Object.assign({}, newQueries[key]);
148
+ if (state.queries[queryKey] === newQueries[queryKey]) {
149
+ newQueries[queryKey] = Object.assign({}, newQueries[queryKey]);
150
150
  }
151
- delete newQueries[key][cacheKey];
151
+ delete newQueries[queryKey][cacheKey];
152
152
  }
153
153
  }
154
154
  else if (queryStates !== EMPTY_QUERY_STATE) {
155
155
  newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
156
- newQueries[key] = EMPTY_QUERY_STATE;
156
+ newQueries[queryKey] = EMPTY_QUERY_STATE;
157
157
  }
158
158
  }
159
159
  return !newQueries
package/dist/index.js CHANGED
@@ -23,7 +23,6 @@ Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: fu
23
23
  Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE; } });
24
24
  // Backlog
25
25
  // ! high (1.0.0)
26
- // key -> query
27
26
  // rca -> vite
28
27
  // defaults
29
28
  // remove cachePolicy? make skip/enabled a function? skip -> enabled/shouldFetch?
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.7.0-rc.1",
5
+ "version": "0.7.1",
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",