react-redux-cache 0.0.6 → 0.0.8
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/createCache.d.ts +8 -2
- package/dist/createCache.js +32 -0
- package/dist/index.js +3 -1
- package/dist/query.d.ts +3 -0
- package/dist/query.js +75 -0
- package/dist/types.d.ts +7 -1
- package/dist/useQuery.d.ts +1 -1
- package/dist/useQuery.js +45 -126
- package/dist/utilsAndConstants.d.ts +1 -6
- package/dist/utilsAndConstants.js +1 -10
- package/package.json +2 -2
package/dist/createCache.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mergeEntityChanges, setMutationStateAndEntities, setQueryStateAndEntities } from './reducer';
|
|
2
|
-
import { Cache, EntitiesMap, Key, OptionalPartial, Typenames } from './types';
|
|
2
|
+
import { Cache, EntitiesMap, Key, OptionalPartial, QueryOptions, Typenames } from './types';
|
|
3
3
|
import { useMutation } from './useMutation';
|
|
4
4
|
import { useQuery } from './useQuery';
|
|
5
5
|
/**
|
|
@@ -58,10 +58,16 @@ 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
|
+
useClient: () => {
|
|
62
|
+
query: <QK_1 extends keyof QP | keyof QR>(options: QueryOptions<T, QP, QR, MP, MR, QK_1>) => Promise<void | import("./types").QueryResult<QK_1 extends infer T_1 ? T_1 extends QK_1 ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>;
|
|
63
|
+
};
|
|
61
64
|
/** Fetches query when params change and subscribes to query state. */
|
|
62
|
-
useQuery: <
|
|
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 | import("./types").QueryResult<QK_2 extends infer T_2 ? T_2 extends QK_2 ? T_2 extends keyof QP & keyof QR ? QR[T_2] : never : never : never>>];
|
|
63
66
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
64
67
|
useMutation: <MK_1 extends keyof MP | keyof MR>(options: {
|
|
68
|
+
/**
|
|
69
|
+
* Creates reducer, actions and hooks for managing queries and mutations through redux cache.
|
|
70
|
+
*/
|
|
65
71
|
mutation: MK_1;
|
|
66
72
|
cacheOptions?: import("./types").MutationCacheOptions | undefined;
|
|
67
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];
|
package/dist/createCache.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createCache = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
4
5
|
const react_redux_1 = require("react-redux");
|
|
6
|
+
const query_1 = require("./query");
|
|
5
7
|
const reducer_1 = require("./reducer");
|
|
6
8
|
const useMutation_1 = require("./useMutation");
|
|
7
9
|
const useQuery_1 = require("./useQuery");
|
|
@@ -15,12 +17,18 @@ const createCache = (cache) => {
|
|
|
15
17
|
// @ts-expect-error hot
|
|
16
18
|
const hotReloadEnabled = Boolean(module === null || module === void 0 ? void 0 : module.hot);
|
|
17
19
|
// provide all optional fields
|
|
20
|
+
// and transform cacheOptions from QueryCachePolicy to QueryCacheOptions
|
|
18
21
|
(_a = cache.options) !== null && _a !== void 0 ? _a : (cache.options = {});
|
|
19
22
|
(_b = (_g = cache.options).logsEnabled) !== null && _b !== void 0 ? _b : (_g.logsEnabled = false);
|
|
20
23
|
(_c = (_h = cache.options).validateFunctionArguments) !== null && _c !== void 0 ? _c : (_h.validateFunctionArguments = utilsAndConstants_1.isDev);
|
|
21
24
|
(_d = (_j = cache.options).validateHookArguments) !== null && _d !== void 0 ? _d : (_j.validateHookArguments = utilsAndConstants_1.isDev && !hotReloadEnabled);
|
|
22
25
|
(_e = cache.queries) !== null && _e !== void 0 ? _e : (cache.queries = {});
|
|
23
26
|
(_f = cache.mutations) !== null && _f !== void 0 ? _f : (cache.mutations = {});
|
|
27
|
+
for (const queryInfo of Object.values(cache.queries)) {
|
|
28
|
+
if (typeof queryInfo.cacheOptions === 'string') {
|
|
29
|
+
queryInfo.cacheOptions = useQuery_1.queryCacheOptionsByPolicy[queryInfo.cacheOptions];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
24
32
|
const nonPartialCache = cache;
|
|
25
33
|
// make selectors
|
|
26
34
|
const entitiesSelector = (state) => {
|
|
@@ -49,6 +57,30 @@ const createCache = (cache) => {
|
|
|
49
57
|
},
|
|
50
58
|
},
|
|
51
59
|
hooks: {
|
|
60
|
+
useClient: () => {
|
|
61
|
+
const store = (0, react_redux_1.useStore)();
|
|
62
|
+
return (0, react_1.useMemo)(() => {
|
|
63
|
+
const client = {
|
|
64
|
+
query: (options) => {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
const { query: queryKey, params,
|
|
67
|
+
// TODO can be memoized for all query keys while creating cache
|
|
68
|
+
cacheOptions: cacheOptionsOrPolicy = Object.assign(Object.assign({}, ((_a = nonPartialCache.queries[queryKey].cacheOptions) !== null && _a !== void 0 ? _a : useQuery_1.defaultQueryCacheOptions)), { policy: 'cache-and-fetch' }), getCacheKey = nonPartialCache.queries[queryKey].getCacheKey, } = options;
|
|
69
|
+
const cacheOptions = typeof cacheOptionsOrPolicy === 'string'
|
|
70
|
+
? useQuery_1.queryCacheOptionsByPolicy[cacheOptionsOrPolicy]
|
|
71
|
+
: cacheOptionsOrPolicy;
|
|
72
|
+
const getParamsKey = (_b = nonPartialCache.queries[queryKey].getParamsKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetParamsKey);
|
|
73
|
+
const cacheKey = getCacheKey
|
|
74
|
+
? // @ts-expect-error fix later
|
|
75
|
+
getCacheKey(params)
|
|
76
|
+
: // @ts-expect-error fix later
|
|
77
|
+
getParamsKey(params);
|
|
78
|
+
return (0, query_1.query)('query', true, store, nonPartialCache, queryKey, cacheKey, cacheOptions, params);
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
return client;
|
|
82
|
+
}, [store]);
|
|
83
|
+
},
|
|
52
84
|
/** Fetches query when params change and subscribes to query state. */
|
|
53
85
|
useQuery: (options) => (0, useQuery_1.useQuery)(nonPartialCache, options),
|
|
54
86
|
/** Subscribes to provided mutation state and provides mutate function. */
|
package/dist/index.js
CHANGED
|
@@ -31,6 +31,8 @@ Object.defineProperty(exports, "processEntityChanges", { enumerable: true, get:
|
|
|
31
31
|
// Backlog
|
|
32
32
|
// ! high
|
|
33
33
|
// cover with tests
|
|
34
|
+
// support changing query key?
|
|
35
|
+
// make cache fields readonly
|
|
34
36
|
// ! medium
|
|
35
37
|
// type extractors from cache
|
|
36
38
|
// custom useStore
|
|
@@ -65,4 +67,4 @@ Object.defineProperty(exports, "processEntityChanges", { enumerable: true, get:
|
|
|
65
67
|
// QueryInfo.defaultOptions
|
|
66
68
|
// set options in refresh/mutate functions
|
|
67
69
|
// multiple reducers instead of 1?
|
|
68
|
-
// don't cache result if resultSelector set?
|
|
70
|
+
// don't cache result if resultSelector set? throw error if mergeResult set with resultSelector?
|
package/dist/query.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { Store } from 'redux';
|
|
2
|
+
import { Cache, Key, QueryCacheOptions, QueryResult, Typenames } from './types';
|
|
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, cacheOptions: QueryCacheOptions, 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
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
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.query = void 0;
|
|
13
|
+
const reducer_1 = require("./reducer");
|
|
14
|
+
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
15
|
+
const query = (logTag, returnResult, store, cache, queryKey, cacheKey, cacheOptions, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
var _a;
|
|
17
|
+
const logsEnabled = cache.options.logsEnabled;
|
|
18
|
+
const cacheStateSelector = cache.cacheStateSelector;
|
|
19
|
+
const cacheResultSelector = cache.queries[queryKey].resultSelector;
|
|
20
|
+
const mergeResults = cache.queries[queryKey].mergeResults;
|
|
21
|
+
const queryStateOnStart = cacheStateSelector(store.getState()).queries[queryKey][cacheKey];
|
|
22
|
+
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
23
|
+
logsEnabled &&
|
|
24
|
+
(0, utilsAndConstants_1.log)(`${logTag} cancel: already loading`, {
|
|
25
|
+
queryStateOnStart,
|
|
26
|
+
params,
|
|
27
|
+
cacheKey,
|
|
28
|
+
});
|
|
29
|
+
return returnResult ? { cancelled: true } : undefined;
|
|
30
|
+
}
|
|
31
|
+
cacheOptions.cacheQueryState &&
|
|
32
|
+
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
|
|
33
|
+
loading: true,
|
|
34
|
+
}));
|
|
35
|
+
logsEnabled && (0, utilsAndConstants_1.log)(`${logTag} started`, { queryStateOnStart, params, cacheKey });
|
|
36
|
+
let response;
|
|
37
|
+
const fetchFn = cache.queries[queryKey].query;
|
|
38
|
+
try {
|
|
39
|
+
response = yield fetchFn(
|
|
40
|
+
// @ts-expect-error fix later
|
|
41
|
+
params);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
|
|
45
|
+
error: error,
|
|
46
|
+
loading: false,
|
|
47
|
+
}));
|
|
48
|
+
return returnResult ? { error } : undefined;
|
|
49
|
+
}
|
|
50
|
+
const newState = cacheOptions.cacheQueryState
|
|
51
|
+
? {
|
|
52
|
+
error: undefined,
|
|
53
|
+
loading: false,
|
|
54
|
+
result: cacheResultSelector
|
|
55
|
+
? undefined
|
|
56
|
+
: mergeResults
|
|
57
|
+
? mergeResults(
|
|
58
|
+
// @ts-expect-error fix later
|
|
59
|
+
(_a = cacheStateSelector(store.getState()).queries[queryKey][cacheKey]) === null || _a === void 0 ? void 0 : _a.result, response, params)
|
|
60
|
+
: response.result,
|
|
61
|
+
}
|
|
62
|
+
: undefined;
|
|
63
|
+
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, newState, cacheOptions.cacheEntities ? response : undefined));
|
|
64
|
+
// @ts-expect-error fix types
|
|
65
|
+
return returnResult
|
|
66
|
+
? {
|
|
67
|
+
result: cacheResultSelector
|
|
68
|
+
? cacheResultSelector(cacheStateSelector(store.getState()),
|
|
69
|
+
// @ts-expect-error fix types
|
|
70
|
+
params)
|
|
71
|
+
: newState === null || newState === void 0 ? void 0 : newState.result,
|
|
72
|
+
}
|
|
73
|
+
: undefined;
|
|
74
|
+
});
|
|
75
|
+
exports.query = query;
|
package/dist/types.d.ts
CHANGED
|
@@ -60,7 +60,7 @@ export type Query<T extends Typenames, P, R> = (params: P) => Promise<QueryRespo
|
|
|
60
60
|
export type QueryInfo<T extends Typenames, P, R, S> = {
|
|
61
61
|
query: Query<T, P, R>;
|
|
62
62
|
/**
|
|
63
|
-
* Cache policy string or cache options object.
|
|
63
|
+
* Cache policy string or cache options object. After cache created, all strings are converted to objects.
|
|
64
64
|
* Default is { policy: 'cache-first', cacheQueryState: true, cacheEntities: true }
|
|
65
65
|
* @param cache-first for each params key fetch is not called if cache exists.
|
|
66
66
|
* @param cache-and-fetch for each params key result is taken from cache and fetch is called.
|
|
@@ -112,6 +112,12 @@ export type QueryResponse<T extends Typenames, R> = EntityChanges<T> & {
|
|
|
112
112
|
/** Normalized result of a query. */
|
|
113
113
|
result: R;
|
|
114
114
|
};
|
|
115
|
+
export type QueryResult<R> = {
|
|
116
|
+
error?: unknown;
|
|
117
|
+
cancelled?: true;
|
|
118
|
+
result?: R;
|
|
119
|
+
};
|
|
120
|
+
export type QueryOptions<T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)> = Omit<UseQueryOptions<T, QP, QR, MP, MR, QK>, 'skip'>;
|
|
115
121
|
export type Mutation<T extends Typenames, P, R> = (params: P,
|
|
116
122
|
/** Signal is aborted for current mutation when the same mutation was called once again. */
|
|
117
123
|
abortSignal: AbortSignal) => Promise<MutationResponse<T, R>>;
|
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>, (
|
|
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 | import("./types").QueryResult<QK extends infer T_1 ? T_1 extends QK ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>];
|
package/dist/useQuery.js
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
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
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.useQuery = exports.defaultQueryCacheOptions = exports.queryCacheOptionsByPolicy = void 0;
|
|
13
4
|
const react_1 = require("react");
|
|
14
5
|
const react_redux_1 = require("react-redux");
|
|
15
|
-
const
|
|
6
|
+
const query_1 = require("./query");
|
|
16
7
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
17
8
|
const cacheFirstOptions = {
|
|
18
9
|
policy: 'cache-first',
|
|
@@ -24,14 +15,13 @@ exports.queryCacheOptionsByPolicy = {
|
|
|
24
15
|
'cache-and-fetch': Object.assign(Object.assign({}, cacheFirstOptions), { policy: 'cache-and-fetch' }),
|
|
25
16
|
};
|
|
26
17
|
exports.defaultQueryCacheOptions = cacheFirstOptions;
|
|
27
|
-
const defaultRefState = {};
|
|
28
18
|
const useQuery = (cache, options) => {
|
|
29
|
-
var _a, _b, _c
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
var _a, _b, _c;
|
|
20
|
+
const { query: queryKey, skip, params, cacheOptions: cacheOptionsOrPolicy = (_a = cache.queries[queryKey].cacheOptions) !== null && _a !== void 0 ? _a : exports.defaultQueryCacheOptions, getCacheKey = cache.queries[queryKey].getCacheKey, } = options;
|
|
21
|
+
const logsEnabled = cache.options.logsEnabled;
|
|
22
|
+
const getParamsKey = (_b = cache.queries[queryKey].getParamsKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetParamsKey);
|
|
23
|
+
const cacheResultSelector = cache.queries[queryKey].resultSelector;
|
|
24
|
+
const cacheStateSelector = cache.cacheStateSelector;
|
|
35
25
|
const cacheOptions = typeof cacheOptionsOrPolicy === 'string'
|
|
36
26
|
? exports.queryCacheOptionsByPolicy[cacheOptionsOrPolicy]
|
|
37
27
|
: cacheOptionsOrPolicy;
|
|
@@ -50,131 +40,63 @@ const useQuery = (cache, options) => {
|
|
|
50
40
|
['options.query', options.query],
|
|
51
41
|
['queryKey', queryKey],
|
|
52
42
|
['resultSelector', cache.queries[queryKey].resultSelector],
|
|
43
|
+
['mergeResults', cache.queries[queryKey].mergeResults],
|
|
44
|
+
['getParamsKey', cache.queries[queryKey].getParamsKey],
|
|
45
|
+
['getCacheKey', cache.queries[queryKey].getCacheKey],
|
|
53
46
|
]
|
|
54
47
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
55
48
|
.forEach((args) => (0, utilsAndConstants_1.useAssertValueNotChanged)(...args));
|
|
56
49
|
})();
|
|
57
|
-
const
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
state.params = hookParams;
|
|
73
|
-
state.paramsKey = hookParamsKey;
|
|
74
|
-
state.latestHookParamsKey = state.paramsKey;
|
|
75
|
-
// @ts-expect-error fix later
|
|
76
|
-
state.cacheKey = getCacheKey(hookParams);
|
|
50
|
+
const paramsKey = getParamsKey(
|
|
51
|
+
// @ts-expect-error fix later
|
|
52
|
+
params);
|
|
53
|
+
const [cacheKey, resultSelector] = (0, react_1.useMemo)(() => {
|
|
54
|
+
const cacheKeyImpl = getCacheKey
|
|
55
|
+
? // @ts-expect-error fix types later
|
|
56
|
+
getCacheKey(params)
|
|
57
|
+
: paramsKey;
|
|
58
|
+
const resultSelectorImpl = cacheResultSelector &&
|
|
59
|
+
((state) => cacheResultSelector(cacheStateSelector(state),
|
|
60
|
+
// @ts-expect-error fix types later
|
|
61
|
+
params));
|
|
62
|
+
return [cacheKeyImpl, resultSelectorImpl];
|
|
77
63
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
78
|
-
}, [
|
|
79
|
-
const resultFromSelector =
|
|
64
|
+
}, [paramsKey]);
|
|
80
65
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
81
|
-
|
|
66
|
+
const resultFromSelector = (resultSelector && (0, react_redux_1.useSelector)(resultSelector));
|
|
82
67
|
const hasResultFromSelector = resultFromSelector !== undefined;
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
});
|
|
91
|
-
const queryState = cache.cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
68
|
+
const cacheOptionsKey = `${cacheOptions.policy}${cacheOptions.cacheEntities}${cacheOptions.cacheQueryState}`; // Allows to not memoize cache options
|
|
69
|
+
const fetch = (0, react_1.useCallback)(() => {
|
|
70
|
+
return (0, query_1.query)('useQuery.fetch', false, store, cache, queryKey, cacheKey, cacheOptions, params);
|
|
71
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
72
|
+
}, [cacheKey, paramsKey, cacheOptionsKey]); // TODO put args to ref and make empty deps?
|
|
73
|
+
const queryStateFromSelector = (_c = (0, react_redux_1.useSelector)((state) => {
|
|
74
|
+
const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
92
75
|
return queryState; // TODO proper type
|
|
93
|
-
}
|
|
94
|
-
// cacheKey needed only to re-evaluate queryStateSelector later
|
|
95
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
96
|
-
[stateRef.current.cacheKey]);
|
|
97
|
-
const queryStateFromSelector = (_d = (0, react_redux_1.useSelector)(queryStateSelector)) !== null && _d !== void 0 ? _d : utilsAndConstants_1.defaultQueryMutationState;
|
|
76
|
+
})) !== null && _c !== void 0 ? _c : utilsAndConstants_1.defaultQueryMutationState;
|
|
98
77
|
const queryState = hasResultFromSelector
|
|
99
78
|
? (Object.assign(Object.assign({}, queryStateFromSelector), { result: resultFromSelector }))
|
|
100
79
|
: queryStateFromSelector;
|
|
101
|
-
const fetchImpl = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
-
var _e;
|
|
103
|
-
cache.options.logsEnabled && (0, utilsAndConstants_1.log)('useQuery.fetchImpl', { queryState });
|
|
104
|
-
if (queryState.loading) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
const { params, cacheKey } = stateRef.current;
|
|
108
|
-
cacheOptions.cacheQueryState &&
|
|
109
|
-
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
|
|
110
|
-
loading: true,
|
|
111
|
-
}));
|
|
112
|
-
let response;
|
|
113
|
-
const fetchFn = cache.queries[queryKey].query;
|
|
114
|
-
try {
|
|
115
|
-
response = yield fetchFn(
|
|
116
|
-
// @ts-expect-error fix later
|
|
117
|
-
params);
|
|
118
|
-
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
|
|
121
|
-
error: error,
|
|
122
|
-
loading: false,
|
|
123
|
-
}));
|
|
124
|
-
}
|
|
125
|
-
if (response) {
|
|
126
|
-
const newState = cacheOptions.cacheQueryState
|
|
127
|
-
? {
|
|
128
|
-
error: undefined,
|
|
129
|
-
loading: false,
|
|
130
|
-
result: hasResultFromSelector
|
|
131
|
-
? undefined
|
|
132
|
-
: mergeResults
|
|
133
|
-
? mergeResults(
|
|
134
|
-
// @ts-expect-error fix later
|
|
135
|
-
(_e = queryStateSelector(store.getState(), cacheKey)) === null || _e === void 0 ? void 0 : _e.result, response, params)
|
|
136
|
-
: response.result,
|
|
137
|
-
}
|
|
138
|
-
: undefined;
|
|
139
|
-
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, newState, cacheOptions.cacheEntities ? response : undefined));
|
|
140
|
-
}
|
|
141
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
142
|
-
}), [mergeResults, queryState.loading, hasResultFromSelector]);
|
|
143
80
|
(0, react_1.useEffect)(() => {
|
|
144
81
|
if (skip) {
|
|
82
|
+
logsEnabled && (0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch', { skip, paramsKey });
|
|
145
83
|
return;
|
|
146
84
|
}
|
|
147
85
|
if (queryState.result != null && cacheOptions.policy === 'cache-first') {
|
|
86
|
+
logsEnabled &&
|
|
87
|
+
(0, utilsAndConstants_1.log)('useQuery.useEffect don`t fetch due to cache policy', {
|
|
88
|
+
result: queryState.result,
|
|
89
|
+
policy: cacheOptions.policy,
|
|
90
|
+
});
|
|
148
91
|
return;
|
|
149
92
|
}
|
|
150
|
-
|
|
93
|
+
fetch();
|
|
151
94
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
152
|
-
}, [
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
// @ts-expect-error fix later
|
|
158
|
-
const paramsKey = getParamsKey(params);
|
|
159
|
-
if (state.paramsKey !== paramsKey) {
|
|
160
|
-
const resultSelectorImpl = cache.queries[queryKey].resultSelector;
|
|
161
|
-
state.params = params;
|
|
162
|
-
state.paramsKey = paramsKey;
|
|
163
|
-
// @ts-expect-error fix later
|
|
164
|
-
state.cacheKey = getCacheKey(params);
|
|
165
|
-
state.resultSelector = createResultSelector(
|
|
166
|
-
// @ts-expect-error fix later
|
|
167
|
-
resultSelectorImpl, cache.cacheStateSelector, hookParams);
|
|
168
|
-
forceUpdate();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
return fetchImpl();
|
|
172
|
-
},
|
|
173
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
174
|
-
[fetchImpl, getCacheKey]);
|
|
175
|
-
cache.options.logsEnabled &&
|
|
176
|
-
console.debug('[useQuery]', {
|
|
177
|
-
state: stateRef.current,
|
|
95
|
+
}, [paramsKey, cacheOptions.policy, skip]);
|
|
96
|
+
logsEnabled &&
|
|
97
|
+
(0, utilsAndConstants_1.log)('useQuery', {
|
|
98
|
+
paramsKey,
|
|
99
|
+
cacheKey,
|
|
178
100
|
options,
|
|
179
101
|
resultFromSelector,
|
|
180
102
|
queryState,
|
|
@@ -182,6 +104,3 @@ const useQuery = (cache, options) => {
|
|
|
182
104
|
return [queryState, fetch];
|
|
183
105
|
};
|
|
184
106
|
exports.useQuery = useQuery;
|
|
185
|
-
const createResultSelector = (resultSelector, cacheStateSelector, params) => {
|
|
186
|
-
return (resultSelector && ((state) => resultSelector(cacheStateSelector(state), params)));
|
|
187
|
-
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
import { CacheOptions, EntitiesMap, EntityChanges, Typenames } from './types';
|
|
3
2
|
export declare const PACKAGE_SHORT_NAME = "RRC";
|
|
4
3
|
export declare const isDev: boolean;
|
|
@@ -12,12 +11,8 @@ export declare const defaultQueryMutationState: {
|
|
|
12
11
|
readonly error: undefined;
|
|
13
12
|
};
|
|
14
13
|
export declare const defaultGetParamsKey: <P = unknown>(params: P) => string;
|
|
15
|
-
/**
|
|
16
|
-
* @returns function to force update a function component.
|
|
17
|
-
*/
|
|
18
|
-
export declare const useForceUpdate: () => import("react").DispatchWithoutAction;
|
|
19
14
|
export declare const useAssertValueNotChanged: (name: string, value: unknown) => void;
|
|
20
|
-
export declare const log: (tag: string, data
|
|
15
|
+
export declare const log: (tag: string, data?: unknown) => void;
|
|
21
16
|
/**
|
|
22
17
|
* Process changes to entities map.
|
|
23
18
|
* @return `undefined` if nothing to change, otherwise processed entities map.
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.processEntityChanges = exports.log = exports.useAssertValueNotChanged = exports.
|
|
3
|
+
exports.processEntityChanges = exports.log = exports.useAssertValueNotChanged = exports.defaultGetParamsKey = exports.defaultQueryMutationState = exports.defaultCacheOptions = exports.isDev = exports.PACKAGE_SHORT_NAME = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const react_2 = require("react");
|
|
6
5
|
exports.PACKAGE_SHORT_NAME = 'RRC';
|
|
7
6
|
exports.isDev = (() => {
|
|
8
7
|
try {
|
|
@@ -30,14 +29,6 @@ const defaultGetParamsKey = (params) => {
|
|
|
30
29
|
}
|
|
31
30
|
};
|
|
32
31
|
exports.defaultGetParamsKey = defaultGetParamsKey;
|
|
33
|
-
const forceUpdateReducer = (i) => i + 1;
|
|
34
|
-
/**
|
|
35
|
-
* @returns function to force update a function component.
|
|
36
|
-
*/
|
|
37
|
-
const useForceUpdate = () => {
|
|
38
|
-
return (0, react_2.useReducer)(forceUpdateReducer, 0)[1];
|
|
39
|
-
};
|
|
40
|
-
exports.useForceUpdate = useForceUpdate;
|
|
41
32
|
const useAssertValueNotChanged = (name, value) => {
|
|
42
33
|
const firstMountRef = (0, react_1.useRef)(false);
|
|
43
34
|
(0, react_1.useMemo)(() => {
|
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.8",
|
|
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",
|
|
14
14
|
"prepublishOnly": "yarn build && yarn test"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|