react-redux-cache 0.18.1 → 0.18.3

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.
@@ -175,6 +175,11 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
175
175
  useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined;
176
176
  };
177
177
  utils: {
178
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
179
+ createClient: (store: Store) => {
180
+ query: <QK extends keyof (QP & QR)>(options: QueryOptions<N, T, QP, QR, QK, MP, MR>) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
181
+ mutate: <MK extends keyof (MP & MR)>(options: MutateOptions<N, T, QP, QR, MP, MR, MK>) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
182
+ };
178
183
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
179
184
  getInitialState: () => import("./types").CacheState<T, QP, QR, MP, MR>;
180
185
  /** Apply changes to the entities map.
@@ -359,6 +364,11 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
359
364
  useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined;
360
365
  };
361
366
  utils: {
367
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
368
+ createClient: (store: Store) => {
369
+ query: <QK_1 extends keyof QP | keyof QR>(options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>;
370
+ mutate: <MK_1 extends keyof MP | keyof MR>(options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>;
371
+ };
362
372
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
363
373
  getInitialState: () => import("./types").CacheState<Typenames, QP, QR, MP, MR>;
364
374
  /** Apply changes to the entities map.
@@ -37,10 +37,8 @@ const withTypenames = () => {
37
37
  (_g = (_s = partialCache.globals.queries).fetchPolicy) !== null && _g !== void 0 ? _g : (_s.fetchPolicy = utilsAndConstants_1.FetchPolicy.NoCacheOrExpired);
38
38
  (_h = (_t = partialCache.globals.queries).skipFetch) !== null && _h !== void 0 ? _h : (_t.skipFetch = false);
39
39
  (_j = partialCache.storeHooks) !== null && _j !== void 0 ? _j : (partialCache.storeHooks = {
40
- // eslint-disable-next-line @typescript-eslint/no-var-requires
41
- useStore: require('react-redux').useStore,
42
- // eslint-disable-next-line @typescript-eslint/no-var-requires
43
- useSelector: require('react-redux').useSelector,
40
+ useStore: require.call(undefined, 'react-redux').useStore,
41
+ useSelector: require.call(undefined, 'react-redux').useSelector,
44
42
  });
45
43
  (_k = partialCache.cacheStateSelector) !== null && _k !== void 0 ? _k : (partialCache.cacheStateSelector = (state) => state[cache.name]);
46
44
  (_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {});
@@ -60,6 +58,27 @@ const withTypenames = () => {
60
58
  const { updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges, invalidateQuery, clearQueryState, clearMutationState, clearCache, } = actions;
61
59
  // reducer
62
60
  const reducer = (0, createCacheReducer_1.createCacheReducer)(actions, Object.keys(cache.queries), cache.options);
61
+ // createClient
62
+ const createClient = (store) => {
63
+ const client = {
64
+ query: (options) => {
65
+ var _a;
66
+ const { query: queryKey, params } = options;
67
+ const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
68
+ // @ts-expect-error fix later
69
+ const cacheKey = getCacheKey(params);
70
+ return (0, query_1.query)('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired,
71
+ // @ts-expect-error fix later
72
+ options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
73
+ },
74
+ mutate: (options) => {
75
+ return (0, mutate_1.mutate)('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers,
76
+ // @ts-expect-error fix later
77
+ options.onCompleted, options.onSuccess, options.onError);
78
+ },
79
+ };
80
+ return client;
81
+ };
63
82
  return {
64
83
  /** Keeps all options, passed while creating the cache. */
65
84
  cache,
@@ -118,26 +137,7 @@ const withTypenames = () => {
118
137
  /** Returns client object with query and mutate functions. */
119
138
  useClient: () => {
120
139
  const store = cache.storeHooks.useStore();
121
- return (0, react_1.useMemo)(() => {
122
- const client = {
123
- query: (options) => {
124
- var _a;
125
- const { query: queryKey, params } = options;
126
- const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
127
- // @ts-expect-error fix later
128
- const cacheKey = getCacheKey(params);
129
- return (0, query_1.query)('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired,
130
- // @ts-expect-error fix later
131
- options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
132
- },
133
- mutate: (options) => {
134
- return (0, mutate_1.mutate)('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers,
135
- // @ts-expect-error fix later
136
- options.onCompleted, options.onSuccess, options.onError);
137
- },
138
- };
139
- return client;
140
- }, [store]);
140
+ return (0, react_1.useMemo)(() => createClient(store), [store]);
141
141
  },
142
142
  /** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
143
143
  useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, selectors, options),
@@ -149,6 +149,8 @@ const withTypenames = () => {
149
149
  },
150
150
  },
151
151
  utils: {
152
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
153
+ createClient,
152
154
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
153
155
  getInitialState: () => {
154
156
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -48,15 +48,24 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
48
48
  const oldQueryState = state.queries[queryKey][queryCacheKey];
49
49
  let newQueryState = queryState && Object.assign(Object.assign({}, oldQueryState), queryState);
50
50
  if (newQueryState) {
51
+ if (oldQueryState && deepEqual) {
52
+ // set back params if deeply same value
53
+ if (newQueryState.params !== oldQueryState.params &&
54
+ deepEqual(newQueryState.params, oldQueryState.params)) {
55
+ newQueryState.params = oldQueryState.params;
56
+ }
57
+ // set back if deeply same value
58
+ if (newQueryState.result !== oldQueryState.result &&
59
+ deepEqual(newQueryState.result, oldQueryState.result)) {
60
+ newQueryState.result = oldQueryState.result;
61
+ }
62
+ }
51
63
  // remove undefined optional fields
52
64
  for (const key of optionalQueryKeys) {
53
65
  if (key in newQueryState && newQueryState[key] === undefined) {
54
66
  delete newQueryState[key];
55
67
  }
56
68
  }
57
- if ('loading' in newQueryState && !newQueryState.loading) {
58
- delete newQueryState.loading;
59
- }
60
69
  // skip if new state deep equals to the old state
61
70
  if (deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldQueryState !== null && oldQueryState !== void 0 ? oldQueryState : utilsAndConstants_1.EMPTY_OBJECT, newQueryState)) {
62
71
  newQueryState = undefined;
@@ -87,6 +96,18 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
87
96
  const oldMutationState = state.mutations[mutationKey];
88
97
  let newMutationState = mutationState && Object.assign(Object.assign({}, oldMutationState), mutationState);
89
98
  if (newMutationState) {
99
+ if (oldMutationState && deepEqual) {
100
+ // set back params if deeply same value
101
+ if (newMutationState.params !== oldMutationState.params &&
102
+ deepEqual(newMutationState.params, oldMutationState.params)) {
103
+ newMutationState.params = oldMutationState.params;
104
+ }
105
+ // set back if deeply same value
106
+ if (newMutationState.result !== oldMutationState.result &&
107
+ deepEqual(newMutationState.result, oldMutationState.result)) {
108
+ newMutationState.result = oldMutationState.result;
109
+ }
110
+ }
90
111
  // remove optional fields with default values
91
112
  for (const key of optionalMutationKeys) {
92
113
  if (key in newMutationState && newMutationState[key] === undefined) {
package/dist/index.js CHANGED
@@ -26,13 +26,10 @@ Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: fu
26
26
  Object.defineProperty(exports, "FetchPolicy", { enumerable: true, get: function () { return utilsAndConstants_1.FetchPolicy; } });
27
27
  Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return utilsAndConstants_1.isEmptyObject; } });
28
28
  // Backlog
29
- // highest
30
- // generate full api docs
31
- // cancel all queries / mutations
32
- // selectors optional cache key
33
29
  // ! high (1.0.0-rc.0)
30
+ // generate full api docs
34
31
  // optimistic response
35
- // features list
32
+ // features list (table)
36
33
  // ! medium
37
34
  // onCancel & onAbort
38
35
  // remove empty entities and queries from state
@@ -51,3 +48,7 @@ Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: functio
51
48
  // readonly types?
52
49
  // proper types, remove as, any, todo
53
50
  // add number of retries param?
51
+ // mutation state wrong type - can be undefined?
52
+ // ! on hold
53
+ // better support queries without params: useQuery & selectors optional cache key (making useQuery params optional doesn't seem like a good idea)
54
+ // cancel all queries / mutations (already possible by recreating the store - this lib is pure)
@@ -6,7 +6,7 @@ exports.optionalUtils = {
6
6
  deepEqual: undefined,
7
7
  };
8
8
  try {
9
- exports.optionalUtils.deepEqual = require('fast-deep-equal/es6');
9
+ exports.optionalUtils.deepEqual = require.call(undefined, 'fast-deep-equal/es6');
10
10
  }
11
11
  catch (_a) {
12
12
  console.debug(exports.PACKAGE_SHORT_NAME + ': fast-deep-equal optional dependency was not installed');
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.18.1",
5
+ "version": "0.18.3",
6
6
  "description": "Powerful data fetching and caching library for Redux and Zustand that supports normalization.",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",