react-redux-cache 0.0.8 → 0.0.10
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 +22 -1
- package/dist/createCache.d.ts +13 -10
- package/dist/createCache.js +6 -0
- package/dist/index.js +4 -2
- package/dist/mutate.d.ts +5 -0
- package/dist/mutate.js +89 -0
- package/dist/query.js +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/useMutation.d.ts +1 -1
- package/dist/useMutation.js +41 -71
- package/dist/useQuery.d.ts +1 -1
- package/dist/useQuery.js +36 -14
- package/dist/utilsAndConstants.js +6 -5
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ Usage example can be found in `example/` folder and run by `npm run example` com
|
|
|
17
17
|
- [api.ts](https://github.com/gentlee/react-redux-cache#apits)
|
|
18
18
|
- [Usage](https://github.com/gentlee/react-redux-cache#usage)
|
|
19
19
|
- [Advanced](https://github.com/gentlee/react-redux-cache#advanced)
|
|
20
|
+
- [redux-persist](https://github.com/gentlee/react-redux-cache#redux-persist)
|
|
20
21
|
|
|
21
22
|
### Installation
|
|
22
23
|
`react` and `redux` are peer dependencies.
|
|
@@ -116,4 +117,24 @@ export const UserScreen = () => {
|
|
|
116
117
|
```
|
|
117
118
|
|
|
118
119
|
### Advanced
|
|
119
|
-
|
|
120
|
+
#### redux-persist
|
|
121
|
+
|
|
122
|
+
Here is a simple `redux-persist` configuration:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// removes `loading` and `error` from persisted state
|
|
126
|
+
function stringifyReplacer(key: string, value: unknown) {
|
|
127
|
+
return key === 'loading' || key === 'error' ? undefined : value
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const persistedReducer = persistReducer(
|
|
131
|
+
{
|
|
132
|
+
key: 'cache',
|
|
133
|
+
storage,
|
|
134
|
+
whitelist: ['entities', 'queries'], // mutations are ignored
|
|
135
|
+
throttle: 1000, // ms
|
|
136
|
+
serialize: (value: unknown) => JSON.stringify(value, stringifyReplacer),
|
|
137
|
+
},
|
|
138
|
+
reducer
|
|
139
|
+
)
|
|
140
|
+
```
|
package/dist/createCache.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mergeEntityChanges, setMutationStateAndEntities, setQueryStateAndEntities } from './reducer';
|
|
2
|
-
import { Cache, EntitiesMap, Key, OptionalPartial, QueryOptions, Typenames } from './types';
|
|
2
|
+
import { Cache, EntitiesMap, Key, MutationCacheOptions, MutationResult, OptionalPartial, QueryOptions, QueryResult, Typenames } from './types';
|
|
3
3
|
import { useMutation } from './useMutation';
|
|
4
4
|
import { useQuery } from './useQuery';
|
|
5
5
|
/**
|
|
@@ -58,19 +58,22 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
|
|
|
58
58
|
entitiesByTypenameSelector: <TN extends keyof T>(typename: TN) => { [K_2 in keyof T]: (state: unknown) => EntitiesMap<T>[K_2]; }[TN];
|
|
59
59
|
};
|
|
60
60
|
hooks: {
|
|
61
|
+
/** Returns client object with query function */
|
|
61
62
|
useClient: () => {
|
|
62
|
-
query: <QK_1 extends keyof QP | keyof QR>(options: QueryOptions<T, QP, QR, MP, MR, QK_1>) => Promise<
|
|
63
|
+
query: <QK_1 extends keyof QP | keyof QR>(options: QueryOptions<T, QP, QR, MP, MR, QK_1>) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>;
|
|
64
|
+
mutate: <MK_1 extends keyof MP | keyof MR>(options: {
|
|
65
|
+
mutation: MK_1;
|
|
66
|
+
params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never;
|
|
67
|
+
cacheOptions?: MutationCacheOptions | undefined;
|
|
68
|
+
}) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>;
|
|
63
69
|
};
|
|
64
70
|
/** Fetches query when params change and subscribes to query state. */
|
|
65
|
-
useQuery: <QK_2 extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<T, QP, QR, MP, MR, QK_2>) => readonly [import("./types").QueryMutationState<QK_2 extends keyof QP & keyof QR ? QR[QK_2] : never>, () => Promise<void
|
|
71
|
+
useQuery: <QK_2 extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<T, QP, QR, MP, MR, QK_2>) => readonly [import("./types").QueryMutationState<QK_2 extends keyof QP & keyof QR ? QR[QK_2] : never>, () => Promise<void>];
|
|
66
72
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
67
|
-
useMutation: <
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
mutation: MK_1;
|
|
72
|
-
cacheOptions?: import("./types").MutationCacheOptions | undefined;
|
|
73
|
-
}) => readonly [(params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never) => Promise<void>, import("./types").QueryMutationState<MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never>, AbortController | undefined];
|
|
73
|
+
useMutation: <MK_2 extends keyof MP | keyof MR>(options: {
|
|
74
|
+
mutation: MK_2;
|
|
75
|
+
cacheOptions?: MutationCacheOptions | undefined;
|
|
76
|
+
}) => 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];
|
|
74
77
|
/** Selects entity by id and subscribes to the changes. */
|
|
75
78
|
useSelectEntityById: <K_3 extends keyof T>(id: Key | null | undefined, typename: K_3) => T[K_3] | undefined;
|
|
76
79
|
};
|
package/dist/createCache.js
CHANGED
|
@@ -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 mutate_1 = require("./mutate");
|
|
6
7
|
const query_1 = require("./query");
|
|
7
8
|
const reducer_1 = require("./reducer");
|
|
8
9
|
const useMutation_1 = require("./useMutation");
|
|
@@ -57,6 +58,7 @@ const createCache = (cache) => {
|
|
|
57
58
|
},
|
|
58
59
|
},
|
|
59
60
|
hooks: {
|
|
61
|
+
/** Returns client object with query function */
|
|
60
62
|
useClient: () => {
|
|
61
63
|
const store = (0, react_redux_1.useStore)();
|
|
62
64
|
return (0, react_1.useMemo)(() => {
|
|
@@ -77,6 +79,10 @@ const createCache = (cache) => {
|
|
|
77
79
|
getParamsKey(params);
|
|
78
80
|
return (0, query_1.query)('query', true, store, nonPartialCache, queryKey, cacheKey, cacheOptions, params);
|
|
79
81
|
},
|
|
82
|
+
mutate: (options) => {
|
|
83
|
+
var _a;
|
|
84
|
+
return (0, mutate_1.mutate)('mutate', true, store, nonPartialCache, options.mutation, (_a = options.cacheOptions) !== null && _a !== void 0 ? _a : useMutation_1.defaultMutationCacheOptions, options.params);
|
|
85
|
+
},
|
|
80
86
|
};
|
|
81
87
|
return client;
|
|
82
88
|
}, [store]);
|
package/dist/index.js
CHANGED
|
@@ -30,9 +30,9 @@ Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true,
|
|
|
30
30
|
Object.defineProperty(exports, "processEntityChanges", { enumerable: true, get: function () { return utilsAndConstants_1.processEntityChanges; } });
|
|
31
31
|
// Backlog
|
|
32
32
|
// ! high
|
|
33
|
+
// mutate
|
|
33
34
|
// cover with tests
|
|
34
|
-
//
|
|
35
|
-
// make cache fields readonly
|
|
35
|
+
// suport persist (no loading etc)
|
|
36
36
|
// ! medium
|
|
37
37
|
// type extractors from cache
|
|
38
38
|
// custom useStore
|
|
@@ -55,6 +55,8 @@ Object.defineProperty(exports, "processEntityChanges", { enumerable: true, get:
|
|
|
55
55
|
// make error type generic
|
|
56
56
|
// proper types, remove as, any, todo
|
|
57
57
|
// ! low
|
|
58
|
+
// make types readonly
|
|
59
|
+
// support changing query key?
|
|
58
60
|
// remove defaultState and keep values undefined?
|
|
59
61
|
// add params to the state?
|
|
60
62
|
// cancellation to queries
|
package/dist/mutate.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Store } from 'redux';
|
|
2
|
+
import { Cache, Key, MutationCacheOptions, MutationResult, Typenames } from './types';
|
|
3
|
+
export declare const abortControllers: WeakMap<Store<any, import("redux").AnyAction>, Record<Key, AbortController>>;
|
|
4
|
+
export declare const getAbortController: (store: Store, mutationKey: Key) => AbortController | undefined;
|
|
5
|
+
export declare const mutate: <T extends Typenames, QP, QR, MP, MR, MK extends keyof MP | keyof MR>(logTag: string, returnResult: boolean, store: Store, cache: Cache<T, QP, QR, MP, MR>, mutationKey: MK, cacheOptions: MutationCacheOptions, params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<void | MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
package/dist/mutate.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.mutate = exports.getAbortController = exports.abortControllers = void 0;
|
|
13
|
+
const reducer_1 = require("./reducer");
|
|
14
|
+
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
15
|
+
exports.abortControllers = new WeakMap();
|
|
16
|
+
const getAbortController = (store, mutationKey) => { var _a; return (_a = exports.abortControllers.get(store)) === null || _a === void 0 ? void 0 : _a[mutationKey]; };
|
|
17
|
+
exports.getAbortController = getAbortController;
|
|
18
|
+
const mutate = (logTag, returnResult, store, cache, mutationKey, cacheOptions, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
let abortControllersOfStore = exports.abortControllers.get(store);
|
|
20
|
+
if (abortControllersOfStore === undefined) {
|
|
21
|
+
abortControllersOfStore = {};
|
|
22
|
+
exports.abortControllers.set(store, abortControllersOfStore);
|
|
23
|
+
}
|
|
24
|
+
{
|
|
25
|
+
const abortController = abortControllersOfStore[mutationKey];
|
|
26
|
+
cache.options.logsEnabled &&
|
|
27
|
+
(0, utilsAndConstants_1.log)(logTag, {
|
|
28
|
+
mutationKey,
|
|
29
|
+
params,
|
|
30
|
+
previousAborted: abortController !== undefined,
|
|
31
|
+
});
|
|
32
|
+
if (abortController !== undefined) {
|
|
33
|
+
abortController.abort();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
cacheOptions.cacheMutationState &&
|
|
37
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
38
|
+
loading: true,
|
|
39
|
+
result: undefined,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const abortController = new AbortController();
|
|
44
|
+
abortControllersOfStore[mutationKey] = abortController;
|
|
45
|
+
let response;
|
|
46
|
+
let error;
|
|
47
|
+
const fetchFn = cache.mutations[mutationKey].mutation;
|
|
48
|
+
try {
|
|
49
|
+
response = yield fetchFn(
|
|
50
|
+
// @ts-expect-error fix later
|
|
51
|
+
params, abortController.signal);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
error = e;
|
|
55
|
+
}
|
|
56
|
+
cache.options.logsEnabled &&
|
|
57
|
+
(0, utilsAndConstants_1.log)(`${logTag} finished`, {
|
|
58
|
+
response,
|
|
59
|
+
error,
|
|
60
|
+
aborted: abortController.signal.aborted,
|
|
61
|
+
});
|
|
62
|
+
if (abortController.signal.aborted) {
|
|
63
|
+
return returnResult ? { aborted: true } : undefined;
|
|
64
|
+
}
|
|
65
|
+
delete abortControllersOfStore[mutationKey];
|
|
66
|
+
if (error) {
|
|
67
|
+
if (cacheOptions.cacheMutationState) {
|
|
68
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
69
|
+
error: error,
|
|
70
|
+
loading: false,
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
return { error };
|
|
74
|
+
}
|
|
75
|
+
if (response) {
|
|
76
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, cacheOptions.cacheMutationState
|
|
77
|
+
? {
|
|
78
|
+
error: undefined,
|
|
79
|
+
loading: false,
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
81
|
+
result: response.result,
|
|
82
|
+
}
|
|
83
|
+
: undefined, cacheOptions.cacheEntities ? response : undefined));
|
|
84
|
+
// @ts-expect-error fix later
|
|
85
|
+
return returnResult ? { result: response.result } : undefined;
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`${logTag}: both error and response are not defined`);
|
|
88
|
+
});
|
|
89
|
+
exports.mutate = mutate;
|
package/dist/query.js
CHANGED
|
@@ -21,7 +21,7 @@ const query = (logTag, returnResult, store, cache, queryKey, cacheKey, cacheOpti
|
|
|
21
21
|
const queryStateOnStart = cacheStateSelector(store.getState()).queries[queryKey][cacheKey];
|
|
22
22
|
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
23
23
|
logsEnabled &&
|
|
24
|
-
(0, utilsAndConstants_1.log)(`${logTag}
|
|
24
|
+
(0, utilsAndConstants_1.log)(`${logTag} cancelled: already loading`, {
|
|
25
25
|
queryStateOnStart,
|
|
26
26
|
params,
|
|
27
27
|
cacheKey,
|
package/dist/types.d.ts
CHANGED
|
@@ -133,6 +133,11 @@ export type MutationResponse<T extends Typenames, R> = EntityChanges<T> & {
|
|
|
133
133
|
/** Normalized result of a mutation. */
|
|
134
134
|
result?: R;
|
|
135
135
|
};
|
|
136
|
+
export type MutationResult<R> = {
|
|
137
|
+
error?: unknown;
|
|
138
|
+
aborted?: true;
|
|
139
|
+
result?: R;
|
|
140
|
+
};
|
|
136
141
|
export type QueryMutationState<R> = {
|
|
137
142
|
/** `true` when query or mutation is currently in progress. */
|
|
138
143
|
loading: boolean;
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ export declare const defaultMutationCacheOptions: MutationCacheOptions;
|
|
|
3
3
|
export declare const useMutation: <T extends Typenames, MP, MR, MK extends keyof MP | keyof MR>(cache: Cache<T, unknown, unknown, MP, MR>, options: {
|
|
4
4
|
mutation: MK;
|
|
5
5
|
cacheOptions?: MutationCacheOptions | undefined;
|
|
6
|
-
}) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<void>, QueryMutationState<MK extends keyof MP & keyof MR ? MP[MK] : never>,
|
|
6
|
+
}) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<void>, QueryMutationState<MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
package/dist/useMutation.js
CHANGED
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.useMutation = exports.defaultMutationCacheOptions = void 0;
|
|
13
13
|
const react_1 = require("react");
|
|
14
14
|
const react_redux_1 = require("react-redux");
|
|
15
|
+
const mutate_1 = require("./mutate");
|
|
15
16
|
const reducer_1 = require("./reducer");
|
|
16
17
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
17
18
|
exports.defaultMutationCacheOptions = {
|
|
@@ -21,11 +22,6 @@ exports.defaultMutationCacheOptions = {
|
|
|
21
22
|
const useMutation = (cache, options) => {
|
|
22
23
|
var _a, _b;
|
|
23
24
|
const { mutation: mutationKey, cacheOptions = (_a = cache.mutations[mutationKey].cacheOptions) !== null && _a !== void 0 ? _a : exports.defaultMutationCacheOptions, } = options;
|
|
24
|
-
const dispatch = (0, react_redux_1.useDispatch)();
|
|
25
|
-
cache.options.logsEnabled &&
|
|
26
|
-
(0, utilsAndConstants_1.log)('useMutation', {
|
|
27
|
-
cacheOptions,
|
|
28
|
-
});
|
|
29
25
|
// Check values that should be set once.
|
|
30
26
|
// Can be removed from deps.
|
|
31
27
|
cache.options.validateHookArguments &&
|
|
@@ -43,73 +39,47 @@ const useMutation = (cache, options) => {
|
|
|
43
39
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
44
40
|
.forEach((args) => (0, utilsAndConstants_1.useAssertValueNotChanged)(...args));
|
|
45
41
|
})();
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
(0,
|
|
61
|
-
mutationKey,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
else {
|
|
69
|
-
cacheOptions.cacheMutationState &&
|
|
70
|
-
dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, { loading: true }));
|
|
71
|
-
}
|
|
72
|
-
const abortController = new AbortController();
|
|
73
|
-
abortControllerRef.current = abortController;
|
|
74
|
-
let response;
|
|
75
|
-
let error;
|
|
76
|
-
const fetchFn = cache.mutations[mutationKey].mutation;
|
|
77
|
-
try {
|
|
78
|
-
response = yield fetchFn(
|
|
79
|
-
// @ts-expect-error fix later
|
|
80
|
-
params, abortController.signal);
|
|
81
|
-
}
|
|
82
|
-
catch (e) {
|
|
83
|
-
error = e;
|
|
84
|
-
}
|
|
85
|
-
cache.options.logsEnabled &&
|
|
86
|
-
(0, utilsAndConstants_1.log)('mutate finished', {
|
|
87
|
-
response,
|
|
88
|
-
error,
|
|
89
|
-
aborted: abortController.signal.aborted,
|
|
90
|
-
});
|
|
91
|
-
if (abortController.signal.aborted) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
abortControllerRef.current = undefined;
|
|
95
|
-
if (response) {
|
|
96
|
-
dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, cacheOptions.cacheMutationState
|
|
97
|
-
? {
|
|
98
|
-
error: undefined,
|
|
99
|
-
loading: false,
|
|
100
|
-
result: response.result,
|
|
42
|
+
const store = (0, react_redux_1.useStore)();
|
|
43
|
+
// Using single useMemo for performance reasons
|
|
44
|
+
const [mutationStateSelector, mutate, abort] = (0, react_1.useMemo)(() => {
|
|
45
|
+
return [
|
|
46
|
+
// mutationStateSelector
|
|
47
|
+
(state) => {
|
|
48
|
+
cache.options.logsEnabled &&
|
|
49
|
+
(0, utilsAndConstants_1.log)('mutationStateSelector', {
|
|
50
|
+
state,
|
|
51
|
+
cacheState: cache.cacheStateSelector(state),
|
|
52
|
+
});
|
|
53
|
+
return cache.cacheStateSelector(state).mutations[mutationKey];
|
|
54
|
+
},
|
|
55
|
+
// mutate
|
|
56
|
+
(params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
+
yield (0, mutate_1.mutate)('useMutation.mutate', false, store, cache, mutationKey, cacheOptions, params);
|
|
58
|
+
}),
|
|
59
|
+
// abort
|
|
60
|
+
() => {
|
|
61
|
+
const abortController = (0, mutate_1.getAbortController)(store, mutationKey);
|
|
62
|
+
if (abortController === undefined || abortController.signal.aborted) {
|
|
63
|
+
return false;
|
|
101
64
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
}
|
|
65
|
+
abortController.abort();
|
|
66
|
+
cacheOptions.cacheMutationState &&
|
|
67
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
68
|
+
loading: false,
|
|
69
|
+
}));
|
|
70
|
+
return true;
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
},
|
|
111
74
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
112
|
-
[]);
|
|
113
|
-
|
|
75
|
+
[store, cacheOptions.cacheEntities, cacheOptions.cacheMutationState]);
|
|
76
|
+
// @ts-expect-error fix later
|
|
77
|
+
const mutationState = (_b = (0, react_redux_1.useSelector)(mutationStateSelector)) !== null && _b !== void 0 ? _b : utilsAndConstants_1.defaultQueryMutationState;
|
|
78
|
+
cache.options.logsEnabled &&
|
|
79
|
+
(0, utilsAndConstants_1.log)('useMutation', {
|
|
80
|
+
options,
|
|
81
|
+
mutationState,
|
|
82
|
+
});
|
|
83
|
+
return [mutate, mutationState, abort];
|
|
114
84
|
};
|
|
115
85
|
exports.useMutation = useMutation;
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export declare const defaultQueryCacheOptions: {
|
|
|
5
5
|
readonly cacheQueryState: true;
|
|
6
6
|
readonly cacheEntities: true;
|
|
7
7
|
};
|
|
8
|
-
export declare const useQuery: <T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(cache: Cache<T, QP, QR, MP, MR>, options: UseQueryOptions<T, QP, QR, MP, MR, QK>) => readonly [QueryMutationState<QK extends keyof QP & keyof QR ? QR[QK] : never>, () => Promise<void
|
|
8
|
+
export declare const useQuery: <T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(cache: Cache<T, QP, QR, MP, MR>, options: UseQueryOptions<T, QP, QR, MP, MR, QK>) => readonly [QueryMutationState<QK extends keyof QP & keyof QR ? QR[QK] : never>, () => Promise<void>];
|
package/dist/useQuery.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.useQuery = exports.defaultQueryCacheOptions = exports.queryCacheOptionsByPolicy = void 0;
|
|
4
13
|
const react_1 = require("react");
|
|
@@ -51,25 +60,33 @@ const useQuery = (cache, options) => {
|
|
|
51
60
|
// @ts-expect-error fix later
|
|
52
61
|
params);
|
|
53
62
|
const [cacheKey, resultSelector] = (0, react_1.useMemo)(() => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
return [
|
|
64
|
+
// cacheKey
|
|
65
|
+
getCacheKey
|
|
66
|
+
? // @ts-expect-error fix types later
|
|
67
|
+
getCacheKey(params)
|
|
68
|
+
: paramsKey,
|
|
69
|
+
// resultSelector
|
|
70
|
+
cacheResultSelector &&
|
|
71
|
+
((state) => cacheResultSelector(cacheStateSelector(state),
|
|
72
|
+
// @ts-expect-error fix types later
|
|
73
|
+
params)),
|
|
74
|
+
];
|
|
63
75
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
64
76
|
}, [paramsKey]);
|
|
65
77
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
66
78
|
const resultFromSelector = (resultSelector && (0, react_redux_1.useSelector)(resultSelector));
|
|
67
79
|
const hasResultFromSelector = resultFromSelector !== undefined;
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
return (0, query_1.query)('useQuery.fetch', false, store, cache, queryKey, cacheKey, cacheOptions, params);
|
|
80
|
+
const fetch = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
+
yield (0, query_1.query)('useQuery.fetch', false, store, cache, queryKey, cacheKey, cacheOptions, params);
|
|
71
82
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
72
|
-
}, [
|
|
83
|
+
}), [
|
|
84
|
+
cacheKey,
|
|
85
|
+
paramsKey,
|
|
86
|
+
cacheOptions.policy,
|
|
87
|
+
cacheOptions.cacheEntities,
|
|
88
|
+
cacheOptions.cacheQueryState,
|
|
89
|
+
]);
|
|
73
90
|
const queryStateFromSelector = (_c = (0, react_redux_1.useSelector)((state) => {
|
|
74
91
|
const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
75
92
|
return queryState; // TODO proper type
|
|
@@ -101,6 +118,11 @@ const useQuery = (cache, options) => {
|
|
|
101
118
|
resultFromSelector,
|
|
102
119
|
queryState,
|
|
103
120
|
});
|
|
104
|
-
return [
|
|
121
|
+
return [
|
|
122
|
+
/** Query state */
|
|
123
|
+
queryState,
|
|
124
|
+
/** Refetch query with the same parameters */
|
|
125
|
+
fetch,
|
|
126
|
+
];
|
|
105
127
|
};
|
|
106
128
|
exports.useQuery = useQuery;
|
|
@@ -98,11 +98,12 @@ const processEntityChanges = (entities, changes, options) => {
|
|
|
98
98
|
result !== null && result !== void 0 ? result : (result = Object.assign({}, entities));
|
|
99
99
|
result[typename] = newEntities;
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
options.logsEnabled &&
|
|
102
|
+
(0, exports.log)('processEntityChanges', {
|
|
103
|
+
entities,
|
|
104
|
+
changes,
|
|
105
|
+
result,
|
|
106
|
+
});
|
|
106
107
|
return result;
|
|
107
108
|
};
|
|
108
109
|
exports.processEntityChanges = processEntityChanges;
|
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
|
+
"version": "0.0.10",
|
|
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",
|
|
@@ -36,7 +36,6 @@
|
|
|
36
36
|
"react-router-dom": "6.18.0",
|
|
37
37
|
"redux": "4.2.1",
|
|
38
38
|
"redux-logger": "3.0.6",
|
|
39
|
-
"redux-persist": "6.0.0",
|
|
40
39
|
"ts-jest": "29.1.0",
|
|
41
40
|
"typescript": "5.0.4"
|
|
42
41
|
},
|