react-redux-cache 0.0.12 → 0.1.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
@@ -6,8 +6,6 @@
6
6
 
7
7
  Remains **full control** of redux state with ability to write custom selectors, actions and reducers to manage cached state.
8
8
 
9
- Usage example can be found in `example/` folder and run by `npm run example` command from the root folder.
10
-
11
9
  ### Table of contents
12
10
 
13
11
  - [Installation](https://github.com/gentlee/react-redux-cache#Installation)
@@ -21,7 +19,8 @@ Usage example can be found in `example/` folder and run by `npm run example` com
21
19
  - [Infinite scroll pagination](https://github.com/gentlee/react-redux-cache#infinite-scroll-pagination)
22
20
  - [redux-persist](https://github.com/gentlee/react-redux-cache#redux-persist)
23
21
  - [FAQ](https://github.com/gentlee/react-redux-cache#faq)
24
- - [What is cache key?](https://github.com/gentlee/react-redux-cache#what-is-cache-key)
22
+ - [What is a query cache key?](https://github.com/gentlee/react-redux-cache#what-is-a-query-cache-key)
23
+ - [How mutation fetching differs from queries?](https://github.com/gentlee/react-redux-cache#how-mutation-fetching-differs-from-queries)
25
24
 
26
25
  ### Installation
27
26
  `react`, `redux` and `react-redux` are peer dependencies.
@@ -29,7 +28,7 @@ Usage example can be found in `example/` folder and run by `npm run example` com
29
28
  npm add react-redux-cache react redux react-redux
30
29
  ```
31
30
  ### Initialization
32
- The only function that needs to be imported is `createCache`, which creates fully typed reducer, hooks, actions, selectors and utils to be used in the app.
31
+ The only function that needs to be imported is `createCache`, which creates fully typed reducer, hooks, actions, selectors and utils to be used in the app. Only **single** cache reducer per store is supported currenlty.
33
32
  All typenames, queries and mutations should be passed while initializing the cache for proper typing.
34
33
  #### cache.ts
35
34
  ```typescript
@@ -38,7 +37,7 @@ export const {
38
37
  hooks: {useClient, useMutation, useQuery, useSelectEntityById},
39
38
  // Actions, selectors and utils may be not used at all
40
39
  selectors: {entitiesSelector, entitiesByTypenameSelector},
41
- actions: {setQueryStateAndEntities, setMutationStateAndEntities, mergeEntityChanges},
40
+ actions: {updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges},
42
41
  utils: {applyEntityChanges},
43
42
  } = createCache({
44
43
  // This selector should return the cache state based on the path to its reducer.
@@ -111,6 +110,9 @@ export const removeUser = async (id: number) => {
111
110
  ```
112
111
 
113
112
  ### Usage
113
+
114
+ Please check `example/` folder (`npm run example` to run).
115
+
114
116
  #### UserScreen.tsx
115
117
  ```typescript
116
118
  export const UserScreen = () => {
@@ -271,7 +273,7 @@ const persistedReducer = persistReducer(
271
273
 
272
274
  ### FAQ
273
275
 
274
- #### What is cache key?
276
+ #### What is a query cache key?
275
277
 
276
278
  **Cache key** is used for storing the query state and for performing a fetch when it changes. Queries with the same cache key share their state.
277
279
 
@@ -293,3 +295,9 @@ export const defaultGetCacheKey = <P = unknown>(params: P): Key => {
293
295
  It is recommended to override it when default implementation is not optimal or when keys in params object can be sorted in random order.
294
296
 
295
297
  As example, can be overriden when implementing pagination.
298
+
299
+ #### How mutation fetching differs from queries?
300
+
301
+ **Queries:** For each cache key (= unique params by default) of each query fetch is running in parallel. If fetch is already running for specific cache key, all next fetches are cancelled until it finishes.
302
+
303
+ **Mutations:** Only one mutation can be run for each mutation key at a time. If another one called, previous is aborted.
@@ -0,0 +1,32 @@
1
+ import type { EntityChanges, Key, QueryMutationState, Typenames } from './types';
2
+ export declare const updateQueryStateAndEntities: <T extends Typenames, QR, K extends keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined) => {
3
+ type: `${string}UPDATE_QUERY_STATE_AND_ENTITIES`;
4
+ queryKey: K;
5
+ queryCacheKey: Key;
6
+ state: Partial<QueryMutationState<QR[K]>> | undefined;
7
+ entityChagnes: EntityChanges<T> | undefined;
8
+ };
9
+ export declare const updateMutationStateAndEntities: <T extends Typenames, MR, K extends keyof MR>(mutationKey: K, state?: Partial<QueryMutationState<MR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined) => {
10
+ type: `${string}UPDATE_MUTATION_STATE_AND_ENTITIES`;
11
+ mutationKey: K;
12
+ state: Partial<QueryMutationState<MR[K]>> | undefined;
13
+ entityChagnes: EntityChanges<T> | undefined;
14
+ };
15
+ export declare const mergeEntityChanges: <T extends Typenames>(changes: EntityChanges<T>) => {
16
+ type: `${string}MERGE_ENTITY_CHANGES`;
17
+ changes: EntityChanges<T>;
18
+ };
19
+ export declare const clearQueryState: <QR, K extends keyof QR>(queryKeys: {
20
+ key: K;
21
+ cacheKey?: Key | undefined;
22
+ }[]) => {
23
+ type: `${string}CLEAR_QUERY_STATE`;
24
+ queryKeys: {
25
+ key: K;
26
+ cacheKey?: Key | undefined;
27
+ }[];
28
+ };
29
+ export declare const clearMutationState: <MR, K extends keyof MR>(mutationKeys: K[]) => {
30
+ type: `${string}CLEAR_MUTATION_STATE`;
31
+ mutationKeys: K[];
32
+ };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clearMutationState = exports.clearQueryState = exports.mergeEntityChanges = exports.updateMutationStateAndEntities = exports.updateQueryStateAndEntities = void 0;
4
+ const utilsAndConstants_1 = require("./utilsAndConstants");
5
+ const ACTION_PREFIX = `@${utilsAndConstants_1.PACKAGE_SHORT_NAME}/`;
6
+ const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChagnes) => ({
7
+ type: `${ACTION_PREFIX}UPDATE_QUERY_STATE_AND_ENTITIES`,
8
+ queryKey,
9
+ queryCacheKey,
10
+ state,
11
+ entityChagnes,
12
+ });
13
+ exports.updateQueryStateAndEntities = updateQueryStateAndEntities;
14
+ const updateMutationStateAndEntities = (mutationKey, state, entityChagnes) => ({
15
+ type: `${ACTION_PREFIX}UPDATE_MUTATION_STATE_AND_ENTITIES`,
16
+ mutationKey,
17
+ state,
18
+ entityChagnes,
19
+ });
20
+ exports.updateMutationStateAndEntities = updateMutationStateAndEntities;
21
+ const mergeEntityChanges = (changes) => ({
22
+ type: `${ACTION_PREFIX}MERGE_ENTITY_CHANGES`,
23
+ changes,
24
+ });
25
+ exports.mergeEntityChanges = mergeEntityChanges;
26
+ const clearQueryState = (queryKeys) => ({
27
+ type: `${ACTION_PREFIX}CLEAR_QUERY_STATE`,
28
+ queryKeys,
29
+ });
30
+ exports.clearQueryState = clearQueryState;
31
+ const clearMutationState = (mutationKeys) => ({
32
+ type: `${ACTION_PREFIX}CLEAR_MUTATION_STATE`,
33
+ mutationKeys,
34
+ });
35
+ exports.clearMutationState = clearMutationState;
@@ -1,12 +1,12 @@
1
- import { mergeEntityChanges, setMutationStateAndEntities, setQueryStateAndEntities } from './reducer';
2
- import { Cache, EntitiesMap, Key, MutationResult, OptionalPartial, QueryOptions, QueryResult, Typenames } from './types';
1
+ import { clearMutationState, clearQueryState, mergeEntityChanges, updateMutationStateAndEntities, updateQueryStateAndEntities } from './actions';
2
+ import type { Cache, EntitiesMap, Key, MutationResult, OptionalPartial, QueryOptions, QueryResult, Typenames } from './types';
3
3
  import { useMutation } from './useMutation';
4
4
  import { useQuery } from './useQuery';
5
5
  import { applyEntityChanges } from './utilsAndConstants';
6
6
  /**
7
7
  * Creates reducer, actions and hooks for managing queries and mutations through redux cache.
8
8
  */
9
- export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: OptionalPartial<Cache<T, QP, QR, MP, MR>, "queries" | "mutations" | "options">) => {
9
+ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<T, QP, QR, MP, MR>, "queries" | "mutations" | "options">) => {
10
10
  cache: Cache<T, QP, QR, MP, MR>;
11
11
  /** Reducer of the cache, should be added to redux store. */
12
12
  reducer: (state: {
@@ -17,16 +17,25 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
17
17
  type: `${string}MERGE_ENTITY_CHANGES`;
18
18
  changes: import("./types").EntityChanges<T>;
19
19
  } | {
20
- type: `${string}SET_QUERY_STATE_AND_ENTITIES`;
20
+ type: `${string}UPDATE_QUERY_STATE_AND_ENTITIES`;
21
21
  queryKey: keyof QR;
22
22
  queryCacheKey: Key;
23
23
  state: Partial<import("./types").QueryMutationState<QR[keyof QR]>> | undefined;
24
24
  entityChagnes: import("./types").EntityChanges<T> | undefined;
25
25
  } | {
26
- type: `${string}SET_MUTATION_STATE_AND_ENTITIES`;
26
+ type: `${string}UPDATE_MUTATION_STATE_AND_ENTITIES`;
27
27
  mutationKey: keyof MR;
28
28
  state: Partial<import("./types").QueryMutationState<MR[keyof MR]>> | undefined;
29
29
  entityChagnes: import("./types").EntityChanges<T> | undefined;
30
+ } | {
31
+ type: `${string}CLEAR_QUERY_STATE`;
32
+ queryKeys: {
33
+ key: keyof QR;
34
+ cacheKey?: Key | undefined;
35
+ }[];
36
+ } | {
37
+ type: `${string}CLEAR_MUTATION_STATE`;
38
+ mutationKeys: (keyof MR)[];
30
39
  }) => {
31
40
  entities: EntitiesMap<T>;
32
41
  queries: { [QK in keyof QR]: import("./types").Dict<import("./types").QueryMutationState<QR[QK]>>; };
@@ -34,16 +43,16 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
34
43
  };
35
44
  actions: {
36
45
  /** Updates query state, and optionally merges entity changes in a single action. */
37
- setQueryStateAndEntities: <K extends keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryMutationState<QR[K]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined) => {
38
- type: `${string}SET_QUERY_STATE_AND_ENTITIES`;
46
+ updateQueryStateAndEntities: <K extends keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryMutationState<QR[K]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined) => {
47
+ type: `${string}UPDATE_QUERY_STATE_AND_ENTITIES`;
39
48
  queryKey: K;
40
49
  queryCacheKey: Key;
41
50
  state: Partial<import("./types").QueryMutationState<QR[K]>> | undefined;
42
51
  entityChagnes: import("./types").EntityChanges<T> | undefined;
43
52
  };
44
53
  /** Updates mutation state, and optionally merges entity changes in a single action. */
45
- setMutationStateAndEntities: <K_1 extends keyof MR>(mutationKey: K_1, state?: Partial<import("./types").QueryMutationState<MR[K_1]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined) => {
46
- type: `${string}SET_MUTATION_STATE_AND_ENTITIES`;
54
+ updateMutationStateAndEntities: <K_1 extends keyof MR>(mutationKey: K_1, state?: Partial<import("./types").QueryMutationState<MR[K_1]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined) => {
55
+ type: `${string}UPDATE_MUTATION_STATE_AND_ENTITIES`;
47
56
  mutationKey: K_1;
48
57
  state: Partial<import("./types").QueryMutationState<MR[K_1]>> | undefined;
49
58
  entityChagnes: import("./types").EntityChanges<T> | undefined;
@@ -53,10 +62,29 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
53
62
  type: `${string}MERGE_ENTITY_CHANGES`;
54
63
  changes: import("./types").EntityChanges<T>;
55
64
  };
65
+ /** Clear states for provided query keys and cache keys.
66
+ * If cache key for query key is not provided, the whole state for query key is cleared. */
67
+ clearQueryState: <K_2 extends keyof QR>(queryKeys: {
68
+ key: K_2;
69
+ cacheKey?: Key | undefined;
70
+ }[]) => {
71
+ type: `${string}CLEAR_QUERY_STATE`;
72
+ queryKeys: {
73
+ key: K_2;
74
+ cacheKey?: Key | undefined;
75
+ }[];
76
+ };
77
+ /** Clear states for provided mutation keys. */
78
+ clearMutationState: <K_3 extends keyof MR>(mutationKeys: K_3[]) => {
79
+ type: `${string}CLEAR_MUTATION_STATE`;
80
+ mutationKeys: K_3[];
81
+ };
56
82
  };
57
83
  selectors: {
84
+ /** Select all entities from the state. */
58
85
  entitiesSelector: (state: unknown) => EntitiesMap<T>;
59
- entitiesByTypenameSelector: <TN extends keyof T>(typename: TN) => { [K_2 in keyof T]: (state: unknown) => EntitiesMap<T>[K_2]; }[TN];
86
+ /** Select all entities of provided typename. */
87
+ entitiesByTypenameSelector: <TN extends keyof T>(typename: TN) => { [K_4 in keyof T]: (state: unknown) => EntitiesMap<T>[K_4]; }[TN];
60
88
  };
61
89
  hooks: {
62
90
  /** Returns client object with query function */
@@ -74,7 +102,7 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
74
102
  mutation: MK_2;
75
103
  }) => readonly [(params: MK_2 extends keyof MP & keyof MR ? MP[MK_2] : never) => Promise<void>, import("./types").QueryMutationState<MK_2 extends keyof MP & keyof MR ? MP[MK_2] : never>, () => boolean];
76
104
  /** Selects entity by id and subscribes to the changes. */
77
- useSelectEntityById: <K_3 extends keyof T>(id: Key | null | undefined, typename: K_3) => T[K_3] | undefined;
105
+ useSelectEntityById: <K_5 extends keyof T>(id: Key | null | undefined, typename: K_5) => T[K_5] | undefined;
78
106
  };
79
107
  utils: {
80
108
  applyEntityChanges: (entities: EntitiesMap<T>, changes: import("./types").EntityChanges<T>) => EntitiesMap<T> | undefined;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createCache = void 0;
4
4
  const react_1 = require("react");
5
5
  const react_redux_1 = require("react-redux");
6
+ const actions_1 = require("./actions");
6
7
  const mutate_1 = require("./mutate");
7
8
  const query_1 = require("./query");
8
9
  const reducer_1 = require("./reducer");
@@ -12,41 +13,48 @@ const utilsAndConstants_1 = require("./utilsAndConstants");
12
13
  /**
13
14
  * Creates reducer, actions and hooks for managing queries and mutations through redux cache.
14
15
  */
15
- const createCache = (cache) => {
16
+ const createCache = (partialCache) => {
16
17
  var _a, _b, _c, _d, _e;
17
18
  var _f, _g;
18
19
  const abortControllers = new WeakMap();
19
20
  // provide all optional fields
20
- (_a = cache.options) !== null && _a !== void 0 ? _a : (cache.options = {});
21
- (_b = (_f = cache.options).logsEnabled) !== null && _b !== void 0 ? _b : (_f.logsEnabled = false);
22
- (_c = (_g = cache.options).validateFunctionArguments) !== null && _c !== void 0 ? _c : (_g.validateFunctionArguments = utilsAndConstants_1.isDev);
23
- (_d = cache.queries) !== null && _d !== void 0 ? _d : (cache.queries = {});
24
- (_e = cache.mutations) !== null && _e !== void 0 ? _e : (cache.mutations = {});
21
+ (_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {});
22
+ (_b = (_f = partialCache.options).logsEnabled) !== null && _b !== void 0 ? _b : (_f.logsEnabled = false);
23
+ (_c = (_g = partialCache.options).validateFunctionArguments) !== null && _c !== void 0 ? _c : (_g.validateFunctionArguments = utilsAndConstants_1.IS_DEV);
24
+ (_d = partialCache.queries) !== null && _d !== void 0 ? _d : (partialCache.queries = {});
25
+ (_e = partialCache.mutations) !== null && _e !== void 0 ? _e : (partialCache.mutations = {});
25
26
  // @ts-expect-error for testing
26
- cache.abortControllers = abortControllers;
27
- const nonPartialCache = cache;
27
+ partialCache.abortControllers = abortControllers;
28
+ const cache = partialCache;
28
29
  // make selectors
29
30
  const entitiesSelector = (state) => {
30
- return nonPartialCache.cacheStateSelector(state).entities;
31
+ return cache.cacheStateSelector(state).entities;
31
32
  };
32
- const enitityMapSelectorByTypename = Object.keys(cache.typenames).reduce((result, x) => {
33
- result[x] = (state) => nonPartialCache.cacheStateSelector(state).entities[x];
33
+ const enitityMapSelectorByTypename = Object.keys(partialCache.typenames).reduce((result, x) => {
34
+ result[x] = (state) => cache.cacheStateSelector(state).entities[x];
34
35
  return result;
35
36
  }, {});
36
37
  return {
37
- cache: nonPartialCache,
38
+ cache,
38
39
  /** Reducer of the cache, should be added to redux store. */
39
- reducer: (0, reducer_1.createCacheReducer)(nonPartialCache.typenames, nonPartialCache.queries, nonPartialCache.mutations, nonPartialCache.options),
40
+ reducer: (0, reducer_1.createCacheReducer)(cache.typenames, cache.queries, cache.mutations, cache.options),
40
41
  actions: {
41
42
  /** Updates query state, and optionally merges entity changes in a single action. */
42
- setQueryStateAndEntities: reducer_1.setQueryStateAndEntities,
43
+ updateQueryStateAndEntities: actions_1.updateQueryStateAndEntities,
43
44
  /** Updates mutation state, and optionally merges entity changes in a single action. */
44
- setMutationStateAndEntities: reducer_1.setMutationStateAndEntities,
45
+ updateMutationStateAndEntities: actions_1.updateMutationStateAndEntities,
45
46
  /** Merge EntityChanges to the state. */
46
- mergeEntityChanges: reducer_1.mergeEntityChanges,
47
+ mergeEntityChanges: actions_1.mergeEntityChanges,
48
+ /** Clear states for provided query keys and cache keys.
49
+ * If cache key for query key is not provided, the whole state for query key is cleared. */
50
+ clearQueryState: actions_1.clearQueryState,
51
+ /** Clear states for provided mutation keys. */
52
+ clearMutationState: actions_1.clearMutationState,
47
53
  },
48
54
  selectors: {
55
+ /** Select all entities from the state. */
49
56
  entitiesSelector,
57
+ /** Select all entities of provided typename. */
50
58
  entitiesByTypenameSelector: (typename) => {
51
59
  return enitityMapSelectorByTypename[typename];
52
60
  },
@@ -60,30 +68,30 @@ const createCache = (cache) => {
60
68
  query: (options) => {
61
69
  var _a;
62
70
  const { query: queryKey, params } = options;
63
- const getCacheKey = (_a = nonPartialCache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
71
+ const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
64
72
  // @ts-expect-error fix later
65
73
  const cacheKey = getCacheKey(params);
66
- return (0, query_1.query)('query', true, store, nonPartialCache, queryKey, cacheKey, params);
74
+ return (0, query_1.query)('query', true, store, cache, queryKey, cacheKey, params);
67
75
  },
68
76
  mutate: (options) => {
69
- return (0, mutate_1.mutate)('mutate', true, store, nonPartialCache, options.mutation, options.params, abortControllers);
77
+ return (0, mutate_1.mutate)('mutate', true, store, cache, options.mutation, options.params, abortControllers);
70
78
  },
71
79
  };
72
80
  return client;
73
81
  }, [store]);
74
82
  },
75
83
  /** Fetches query when params change and subscribes to query state. */
76
- useQuery: (options) => (0, useQuery_1.useQuery)(nonPartialCache, options),
84
+ useQuery: (options) => (0, useQuery_1.useQuery)(cache, options),
77
85
  /** Subscribes to provided mutation state and provides mutate function. */
78
- useMutation: (options) => (0, useMutation_1.useMutation)(nonPartialCache, options, abortControllers),
86
+ useMutation: (options) => (0, useMutation_1.useMutation)(cache, options, abortControllers),
79
87
  /** Selects entity by id and subscribes to the changes. */
80
88
  useSelectEntityById: (id, typename) => {
81
- return (0, react_redux_1.useSelector)((state) => id == null ? undefined : nonPartialCache.cacheStateSelector(state).entities[typename][id]);
89
+ return (0, react_redux_1.useSelector)((state) => id == null ? undefined : cache.cacheStateSelector(state).entities[typename][id]);
82
90
  },
83
91
  },
84
92
  utils: {
85
93
  applyEntityChanges: (entities, changes) => {
86
- return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, nonPartialCache.options);
94
+ return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, cache.options);
87
95
  },
88
96
  },
89
97
  };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { createCache } from './createCache';
2
2
  export type { ReduxCacheState } from './reducer';
3
3
  export * from './types';
4
- export { defaultGetCacheKey, defaultQueryMutationState } from './utilsAndConstants';
4
+ export { defaultGetCacheKey, DEFAULT_QUERY_MUTATION_STATE as defaultQueryMutationState, } from './utilsAndConstants';
package/dist/index.js CHANGED
@@ -20,45 +20,37 @@ Object.defineProperty(exports, "createCache", { enumerable: true, get: function
20
20
  __exportStar(require("./types"), exports);
21
21
  var utilsAndConstants_1 = require("./utilsAndConstants");
22
22
  Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: function () { return utilsAndConstants_1.defaultGetCacheKey; } });
23
- Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.defaultQueryMutationState; } });
23
+ Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE; } });
24
24
  // Backlog
25
25
  // ! high
26
+ // screenshot of redux state to README
27
+ // optimistic response
26
28
  // cover with tests
29
+ // try use skip for refreshing strategy?
30
+ // add example without normalization
31
+ // make query key / cache key difference more clear in the docs
27
32
  // ! medium
28
- // allow multiple mutation with sam keys?
33
+ // make named caches to produce named hooks, actions etc (same as slices in RTK)?
34
+ // allow multiple mutation with same keys?
29
35
  // type extractors from cache
30
36
  // custom useStore
31
37
  // return back deserialize selector?
32
- // resultSelector - return also boolean that result is full enough
38
+ // resultSelector - return also boolean that result is full enough or make cache policy as a function
33
39
  // selector for entities by typename
34
- // provide call query/mutation function to call them without hooks, but with all state updates
35
- // get typenames from schema? (useSelectDenormalized)
36
40
  // callback option on error / success?
37
- // cache policy as function? needsRefetch
38
- // add verbose debug logs
39
- // refetch queries on query / mutation success?
40
- // remove state when it finished without errors
41
- // set default options like getParams
42
- // selectors for loading state of similar query or mutation (wihout using params as key)
41
+ // refetch queries on mutation success
42
+ // remove query/mutation state when it finished without errors
43
43
  // deep equal entities while merging state
44
- // support multiple stores
45
- // add validation if entity is full enough
46
- // optimistic response
47
44
  // make error type generic
48
- // proper types, remove as, any, todo
45
+ // don't cache result if resultSelector set? throw error if mergeResult set with resultSelector?
49
46
  // ! low
50
- // make types readonly
51
- // support changing query key?
52
- // remove defaultState and keep values undefined?
53
47
  // add params to the state?
54
48
  // cancellation to queries
55
49
  // if mutation & query alrady loading - make options: last, throttle, debounce, parallel?
56
50
  // add time-to-live option, and/or time-to-refresh
57
- // add getUpdateTime option to check entities while merging
58
- // useLocalMutation - uses local component state, or make store option - redux or component state or context? 1 version: redux only
51
+ // add refresh interval for queries that are mounted
59
52
  // replace try/catch with returned error
60
53
  // support any store, not only redux
61
- // QueryInfo.defaultOptions
62
- // set options in refresh/mutate functions
63
- // multiple reducers instead of 1?
64
- // don't cache result if resultSelector set? throw error if mergeResult set with resultSelector?
54
+ // readonly types?
55
+ // proper types, remove as, any, todo
56
+ // add number of retries param?
package/dist/mutate.js CHANGED
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.mutate = void 0;
13
- const reducer_1 = require("./reducer");
13
+ const actions_1 = require("./actions");
14
14
  const utilsAndConstants_1 = require("./utilsAndConstants");
15
15
  const mutate = (logTag, returnResult, store, cache, mutationKey, params, abortControllers) => __awaiter(void 0, void 0, void 0, function* () {
16
16
  let abortControllersOfStore = abortControllers.get(store);
@@ -30,7 +30,7 @@ const mutate = (logTag, returnResult, store, cache, mutationKey, params, abortCo
30
30
  abortController.abort();
31
31
  }
32
32
  else {
33
- store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
33
+ store.dispatch((0, actions_1.updateMutationStateAndEntities)(mutationKey, {
34
34
  loading: true,
35
35
  result: undefined,
36
36
  }));
@@ -60,14 +60,14 @@ const mutate = (logTag, returnResult, store, cache, mutationKey, params, abortCo
60
60
  }
61
61
  delete abortControllersOfStore[mutationKey];
62
62
  if (error) {
63
- store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
63
+ store.dispatch((0, actions_1.updateMutationStateAndEntities)(mutationKey, {
64
64
  error: error,
65
65
  loading: false,
66
66
  }));
67
67
  return { error };
68
68
  }
69
69
  if (response) {
70
- store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
70
+ store.dispatch((0, actions_1.updateMutationStateAndEntities)(mutationKey, {
71
71
  error: undefined,
72
72
  loading: false,
73
73
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
package/dist/query.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { Store } from 'redux';
2
- import { Cache, Key, QueryResult, Typenames } from './types';
2
+ import type { Cache, Key, QueryResult, Typenames } from './types';
3
3
  export declare const query: <T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(logTag: string, returnResult: boolean, store: Store, cache: Cache<T, QP, QR, MP, MR>, queryKey: QK, cacheKey: Key, params: QK extends keyof QP & keyof QR ? QP[QK] : never) => Promise<void | QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
package/dist/query.js CHANGED
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.query = void 0;
13
- const reducer_1 = require("./reducer");
13
+ const actions_1 = require("./actions");
14
14
  const utilsAndConstants_1 = require("./utilsAndConstants");
15
15
  const query = (logTag, returnResult, store, cache, queryKey, cacheKey, params) => __awaiter(void 0, void 0, void 0, function* () {
16
16
  var _a;
@@ -28,7 +28,7 @@ const query = (logTag, returnResult, store, cache, queryKey, cacheKey, params) =
28
28
  });
29
29
  return returnResult ? { cancelled: true } : undefined;
30
30
  }
31
- store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
31
+ store.dispatch((0, actions_1.updateQueryStateAndEntities)(queryKey, cacheKey, {
32
32
  loading: true,
33
33
  }));
34
34
  logsEnabled && (0, utilsAndConstants_1.log)(`${logTag} started`, { queryStateOnStart, params, cacheKey });
@@ -40,7 +40,7 @@ const query = (logTag, returnResult, store, cache, queryKey, cacheKey, params) =
40
40
  params);
41
41
  }
42
42
  catch (error) {
43
- store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
43
+ store.dispatch((0, actions_1.updateQueryStateAndEntities)(queryKey, cacheKey, {
44
44
  error: error,
45
45
  loading: false,
46
46
  }));
@@ -57,7 +57,7 @@ const query = (logTag, returnResult, store, cache, queryKey, cacheKey, params) =
57
57
  (_a = cacheStateSelector(store.getState()).queries[queryKey][cacheKey]) === null || _a === void 0 ? void 0 : _a.result, response, params)
58
58
  : response.result,
59
59
  };
60
- store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, newState, response));
60
+ store.dispatch((0, actions_1.updateQueryStateAndEntities)(queryKey, cacheKey, newState, response));
61
61
  // @ts-expect-error fix types
62
62
  return returnResult
63
63
  ? {
package/dist/reducer.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Cache, Dict, EntitiesMap, EntityChanges, Key, QueryMutationState, Typenames } from './types';
1
+ import { clearMutationState, clearQueryState, mergeEntityChanges, updateMutationStateAndEntities, updateQueryStateAndEntities } from './actions';
2
+ import type { Cache, Dict, EntitiesMap, QueryMutationState, Typenames } from './types';
2
3
  export type ReduxCacheState<T extends Typenames, QP, QR, MP, MR> = ReturnType<ReturnType<typeof createCacheReducer<T, QP, QR, MP, MR>>>;
3
4
  export declare const createCacheReducer: <T extends Typenames, QP, QR, MP, MR>(typenames: T, queries: QP & QR extends infer T_1 ? { [QK in keyof T_1]: QK extends keyof QP & keyof QR ? import("./types").QueryInfo<T, QP[QK], QR[QK], {
4
5
  entities: EntitiesMap<T>;
@@ -9,38 +10,30 @@ export declare const createCacheReducer: <T extends Typenames, QP, QR, MP, MR>(t
9
10
  queries: { [QK_1 in keyof QR]: Dict<QueryMutationState<QR[QK_1]>>; };
10
11
  mutations: { [MK in keyof MR]: QueryMutationState<MR[MK]>; };
11
12
  } | undefined, action: {
12
- type: `${string}SET_QUERY_STATE_AND_ENTITIES`;
13
+ type: `${string}UPDATE_QUERY_STATE_AND_ENTITIES`;
13
14
  queryKey: keyof QR;
14
- queryCacheKey: Key;
15
+ queryCacheKey: import("./types").Key;
15
16
  state: Partial<QueryMutationState<QR[keyof QR]>> | undefined;
16
- entityChagnes: EntityChanges<T> | undefined;
17
+ entityChagnes: import("./types").EntityChanges<T> | undefined;
17
18
  } | {
18
- type: `${string}SET_MUTATION_STATE_AND_ENTITIES`;
19
+ type: `${string}UPDATE_MUTATION_STATE_AND_ENTITIES`;
19
20
  mutationKey: keyof MR;
20
21
  state: Partial<QueryMutationState<MR[keyof MR]>> | undefined;
21
- entityChagnes: EntityChanges<T> | undefined;
22
+ entityChagnes: import("./types").EntityChanges<T> | undefined;
22
23
  } | {
23
24
  type: `${string}MERGE_ENTITY_CHANGES`;
24
- changes: EntityChanges<T>;
25
+ changes: import("./types").EntityChanges<T>;
26
+ } | {
27
+ type: `${string}CLEAR_QUERY_STATE`;
28
+ queryKeys: {
29
+ key: keyof QR;
30
+ cacheKey?: import("./types").Key | undefined;
31
+ }[];
32
+ } | {
33
+ type: `${string}CLEAR_MUTATION_STATE`;
34
+ mutationKeys: (keyof MR)[];
25
35
  }) => {
26
36
  entities: EntitiesMap<T>;
27
37
  queries: { [QK_1 in keyof QR]: Dict<QueryMutationState<QR[QK_1]>>; };
28
38
  mutations: { [MK in keyof MR]: QueryMutationState<MR[MK]>; };
29
39
  };
30
- export declare const setQueryStateAndEntities: <T extends Typenames, QR, K extends keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined) => {
31
- type: `${string}SET_QUERY_STATE_AND_ENTITIES`;
32
- queryKey: K;
33
- queryCacheKey: Key;
34
- state: Partial<QueryMutationState<QR[K]>> | undefined;
35
- entityChagnes: EntityChanges<T> | undefined;
36
- };
37
- export declare const setMutationStateAndEntities: <T extends Typenames, MR, K extends keyof MR>(mutationKey: K, state?: Partial<QueryMutationState<MR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined) => {
38
- type: `${string}SET_MUTATION_STATE_AND_ENTITIES`;
39
- mutationKey: K;
40
- state: Partial<QueryMutationState<MR[K]>> | undefined;
41
- entityChagnes: EntityChanges<T> | undefined;
42
- };
43
- export declare const mergeEntityChanges: <T extends Typenames>(changes: EntityChanges<T>) => {
44
- type: `${string}MERGE_ENTITY_CHANGES`;
45
- changes: EntityChanges<T>;
46
- };
package/dist/reducer.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeEntityChanges = exports.setMutationStateAndEntities = exports.setQueryStateAndEntities = exports.createCacheReducer = void 0;
3
+ exports.createCacheReducer = void 0;
4
4
  const utilsAndConstants_1 = require("./utilsAndConstants");
5
+ const EMPTY_QUERY_STATE = Object.freeze({});
5
6
  const createCacheReducer = (typenames, queries, mutations, cacheOptions) => {
6
7
  const entitiesMap = {};
7
8
  for (const key in typenames) {
8
- entitiesMap[key] = {};
9
+ entitiesMap[key] = EMPTY_QUERY_STATE;
9
10
  }
10
11
  const queriesMap = {};
11
12
  for (const key in queries) {
@@ -25,51 +26,72 @@ const createCacheReducer = (typenames, queries, mutations, cacheOptions) => {
25
26
  initialState,
26
27
  });
27
28
  return (state = initialState, action) => {
29
+ var _a, _b;
28
30
  switch (action.type) {
29
- case '@RRC/SET_QUERY_STATE_AND_ENTITIES': {
31
+ case '@RRC/UPDATE_QUERY_STATE_AND_ENTITIES': {
30
32
  const { queryKey, queryCacheKey, state: queryState, entityChagnes } = action;
31
33
  const newEntities = entityChagnes && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChagnes, cacheOptions);
32
34
  if (!queryState && !newEntities) {
33
35
  return state;
34
36
  }
35
- return Object.assign(Object.assign(Object.assign({}, state), (newEntities ? { entities: newEntities } : null)), { queries: Object.assign(Object.assign({}, state.queries), { [queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), { [queryCacheKey]: Object.assign(Object.assign({}, state.queries[queryKey][queryCacheKey]), queryState) }) }) });
37
+ return Object.assign(Object.assign(Object.assign({}, state), (newEntities ? { entities: newEntities } : null)), { queries: Object.assign(Object.assign({}, state.queries), { [queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), { [queryCacheKey]: Object.assign(Object.assign({}, ((_a = state.queries[queryKey][queryCacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE)), queryState) }) }) });
36
38
  }
37
- case '@RRC/SET_MUTATION_STATE_AND_ENTITIES': {
39
+ case '@RRC/UPDATE_MUTATION_STATE_AND_ENTITIES': {
38
40
  const { mutationKey, state: mutationState, entityChagnes } = action;
39
41
  const newEntities = entityChagnes && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChagnes, cacheOptions);
40
42
  if (!mutationState && !newEntities) {
41
43
  return state;
42
44
  }
43
- return Object.assign(Object.assign(Object.assign({}, state), (newEntities ? { entities: newEntities } : null)), { mutations: Object.assign(Object.assign({}, state.mutations), { [mutationKey]: Object.assign(Object.assign({}, state.mutations[mutationKey]), mutationState) }) });
45
+ return Object.assign(Object.assign(Object.assign({}, state), (newEntities ? { entities: newEntities } : null)), { mutations: Object.assign(Object.assign({}, state.mutations), { [mutationKey]: Object.assign(Object.assign({}, ((_b = state.mutations[mutationKey]) !== null && _b !== void 0 ? _b : utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE)), mutationState) }) });
44
46
  }
45
47
  case '@RRC/MERGE_ENTITY_CHANGES': {
46
48
  const { changes } = action;
47
49
  const newEntities = (0, utilsAndConstants_1.applyEntityChanges)(state.entities, changes, cacheOptions);
48
50
  return newEntities ? Object.assign(Object.assign({}, state), { entities: newEntities }) : state;
49
51
  }
52
+ case '@RRC/CLEAR_QUERY_STATE': {
53
+ const { queryKeys } = action;
54
+ if (!queryKeys.length) {
55
+ return state;
56
+ }
57
+ let newQueries = undefined;
58
+ for (const query of queryKeys) {
59
+ if (query.cacheKey != null) {
60
+ if ((newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[query.key][query.cacheKey]) {
61
+ newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
62
+ newQueries[query.key] = Object.assign({}, newQueries[query.key]);
63
+ delete newQueries[query.key][query.cacheKey];
64
+ }
65
+ }
66
+ else if ((newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[query.key] !== EMPTY_QUERY_STATE) {
67
+ newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
68
+ newQueries[query.key] = EMPTY_QUERY_STATE;
69
+ }
70
+ }
71
+ if (!newQueries) {
72
+ return state;
73
+ }
74
+ return Object.assign(Object.assign({}, state), { queries: newQueries });
75
+ }
76
+ case '@RRC/CLEAR_MUTATION_STATE': {
77
+ const { mutationKeys } = action;
78
+ if (!mutationKeys.length) {
79
+ return state;
80
+ }
81
+ let newMutations = undefined;
82
+ for (const mutation of mutationKeys) {
83
+ if (state.mutations[mutation]) {
84
+ newMutations !== null && newMutations !== void 0 ? newMutations : (newMutations = Object.assign({}, state.mutations));
85
+ delete newMutations[mutation];
86
+ }
87
+ }
88
+ if (!newMutations) {
89
+ return state;
90
+ }
91
+ return Object.assign(Object.assign({}, state), { mutations: newMutations });
92
+ }
50
93
  }
51
94
  return state;
52
95
  };
53
96
  };
54
97
  exports.createCacheReducer = createCacheReducer;
55
- const actionPrefix = `@${utilsAndConstants_1.PACKAGE_SHORT_NAME}/`;
56
- const setQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChagnes) => ({
57
- type: `${actionPrefix}SET_QUERY_STATE_AND_ENTITIES`,
58
- queryKey,
59
- queryCacheKey,
60
- state,
61
- entityChagnes,
62
- });
63
- exports.setQueryStateAndEntities = setQueryStateAndEntities;
64
- const setMutationStateAndEntities = (mutationKey, state, entityChagnes) => ({
65
- type: `${actionPrefix}SET_MUTATION_STATE_AND_ENTITIES`,
66
- mutationKey,
67
- state,
68
- entityChagnes,
69
- });
70
- exports.setMutationStateAndEntities = setMutationStateAndEntities;
71
- const mergeEntityChanges = (changes) => ({
72
- type: `${actionPrefix}MERGE_ENTITY_CHANGES`,
73
- changes,
74
- });
75
- exports.mergeEntityChanges = mergeEntityChanges;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { createCacheReducer, ReduxCacheState } from './reducer';
1
+ import { createCacheReducer, ReduxCacheState } from './reducer';
2
2
  export type Key = string | number | symbol;
3
3
  export type Dict<T> = Record<Key, T>;
4
4
  export type OptionalPartial<T, K extends keyof T> = Partial<{
@@ -12,8 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useMutation = void 0;
13
13
  const react_1 = require("react");
14
14
  const react_redux_1 = require("react-redux");
15
+ const actions_1 = require("./actions");
15
16
  const mutate_1 = require("./mutate");
16
- const reducer_1 = require("./reducer");
17
17
  const utilsAndConstants_1 = require("./utilsAndConstants");
18
18
  const useMutation = (cache, options, abortControllers) => {
19
19
  var _a;
@@ -43,7 +43,7 @@ const useMutation = (cache, options, abortControllers) => {
43
43
  return false;
44
44
  }
45
45
  abortController.abort();
46
- store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
46
+ store.dispatch((0, actions_1.updateMutationStateAndEntities)(mutationKey, {
47
47
  loading: false,
48
48
  }));
49
49
  return true;
@@ -52,7 +52,7 @@ const useMutation = (cache, options, abortControllers) => {
52
52
  // eslint-disable-next-line react-hooks/exhaustive-deps
53
53
  }, [mutationKey, store]);
54
54
  // @ts-expect-error fix later
55
- const mutationState = (_a = (0, react_redux_1.useSelector)(mutationStateSelector)) !== null && _a !== void 0 ? _a : utilsAndConstants_1.defaultQueryMutationState;
55
+ const mutationState = (_a = (0, react_redux_1.useSelector)(mutationStateSelector)) !== null && _a !== void 0 ? _a : utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE;
56
56
  cache.options.logsEnabled &&
57
57
  (0, utilsAndConstants_1.log)('useMutation', {
58
58
  options,
package/dist/useQuery.js CHANGED
@@ -40,7 +40,7 @@ const useQuery = (cache, options) => {
40
40
  const queryStateFromSelector = (_c = (0, react_redux_1.useSelector)((state) => {
41
41
  const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
42
42
  return queryState; // TODO proper type
43
- })) !== null && _c !== void 0 ? _c : utilsAndConstants_1.defaultQueryMutationState;
43
+ })) !== null && _c !== void 0 ? _c : utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE;
44
44
  const queryState = hasResultFromSelector
45
45
  ? (Object.assign(Object.assign({}, queryStateFromSelector), { result: resultFromSelector }))
46
46
  : queryStateFromSelector;
@@ -1,7 +1,7 @@
1
- import { CacheOptions, EntitiesMap, EntityChanges, Key, Typenames } from './types';
1
+ import type { CacheOptions, EntitiesMap, EntityChanges, Key, Typenames } from './types';
2
2
  export declare const PACKAGE_SHORT_NAME = "RRC";
3
- export declare const isDev: boolean;
4
- export declare const defaultQueryMutationState: {
3
+ export declare const IS_DEV: boolean;
4
+ export declare const DEFAULT_QUERY_MUTATION_STATE: {
5
5
  readonly loading: false;
6
6
  readonly error: undefined;
7
7
  };
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyEntityChanges = exports.log = exports.defaultGetCacheKey = exports.defaultQueryMutationState = exports.isDev = exports.PACKAGE_SHORT_NAME = void 0;
3
+ exports.applyEntityChanges = exports.log = exports.defaultGetCacheKey = exports.DEFAULT_QUERY_MUTATION_STATE = exports.IS_DEV = exports.PACKAGE_SHORT_NAME = void 0;
4
4
  exports.PACKAGE_SHORT_NAME = 'RRC';
5
- exports.isDev = (() => {
5
+ exports.IS_DEV = (() => {
6
6
  try {
7
7
  // @ts-expect-error __DEV__ is only for React Native
8
8
  return __DEV__;
@@ -11,7 +11,7 @@ exports.isDev = (() => {
11
11
  return process.env.NODE_ENV === 'development';
12
12
  }
13
13
  })();
14
- exports.defaultQueryMutationState = { loading: false, error: undefined };
14
+ exports.DEFAULT_QUERY_MUTATION_STATE = { loading: false, error: undefined };
15
15
  const defaultGetCacheKey = (params) => {
16
16
  switch (typeof params) {
17
17
  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.12",
5
+ "version": "0.1.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",
@@ -10,7 +10,7 @@
10
10
  "example": "(cd example && yarn && yarn start)",
11
11
  "clean": "rm -rf dist",
12
12
  "lint": "yarn eslint src",
13
- "build": "yarn clean && yarn lint && tsc && yarn pack -f package.tgz",
13
+ "build": "yarn clean && yarn lint && tsc",
14
14
  "test": "node node_modules/jest/bin/jest.js",
15
15
  "prepublishOnly": "yarn build && yarn test"
16
16
  },