react-redux-cache 0.12.0 → 0.13.0
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 +30 -15
- package/dist/createActions.d.ts +3 -10
- package/dist/createActions.js +0 -7
- package/dist/createCache.d.ts +80 -61
- package/dist/createCache.js +47 -68
- package/dist/createCacheReducer.d.ts +2 -2
- package/dist/createSelectors.d.ts +18 -0
- package/dist/createSelectors.js +61 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +10 -4
- package/dist/mutate.d.ts +3 -2
- package/dist/mutate.js +14 -9
- package/dist/query.d.ts +3 -2
- package/dist/query.js +14 -10
- package/dist/types.d.ts +42 -45
- package/dist/useMutation.d.ts +3 -2
- package/dist/useMutation.js +2 -2
- package/dist/useQuery.d.ts +5 -2
- package/dist/useQuery.js +21 -20
- package/dist/utilsAndConstants.d.ts +7 -1
- package/dist/utilsAndConstants.js +9 -1
- package/package.json +2 -2
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { Store } from 'redux';
|
|
1
|
+
import type { Store } from 'redux';
|
|
2
|
+
import type { Actions } from './createActions';
|
|
2
3
|
import type { ReduxCacheState } from './createCacheReducer';
|
|
4
|
+
import type { Selectors } from './createSelectors';
|
|
3
5
|
export type Key = string | number | symbol;
|
|
4
6
|
export type Dict<T> = Record<Key, T>;
|
|
5
7
|
export type OptionalPartial<T, K extends keyof T> = Partial<{
|
|
@@ -22,48 +24,42 @@ export type Cache<N extends string, T extends Typenames, QP, QR, MP, MR> = {
|
|
|
22
24
|
/** Used as prefix for actions and in default cacheStateSelector for selecting cache state from redux state. */
|
|
23
25
|
name: N;
|
|
24
26
|
queries: {
|
|
25
|
-
[QK in keyof (QP & QR)]: QK extends keyof (QP | QR) ? QueryInfo<T, QP[QK], QR[QK]> : never;
|
|
27
|
+
[QK in keyof (QP & QR)]: QK extends keyof (QP | QR) ? QueryInfo<N, T, QP[QK], QR[QK], QP, QR, MP, MR> : never;
|
|
26
28
|
};
|
|
27
29
|
mutations: {
|
|
28
|
-
[MK in keyof (MP & MR)]: MK extends keyof (MP | MR) ? MutationInfo<T, MP[MK], MR[MK]> : never;
|
|
30
|
+
[MK in keyof (MP & MR)]: MK extends keyof (MP | MR) ? MutationInfo<N, T, MP[MK], MR[MK], QP, QR, MP, MR> : never;
|
|
29
31
|
};
|
|
30
|
-
/** Default options for queries and mutations.
|
|
31
|
-
|
|
32
|
-
globals: Globals<QP, MP>;
|
|
32
|
+
/** Default options for queries and mutations. */
|
|
33
|
+
globals: Globals<N, T, QP, QR, MP, MR>;
|
|
33
34
|
options: CacheOptions;
|
|
34
35
|
/** Should return cache state from redux root state. Default implementation returns `state[name]`. */
|
|
35
36
|
cacheStateSelector: (state: any) => ReduxCacheState<T, QP, QR, MP, MR>;
|
|
36
37
|
};
|
|
37
|
-
export type Globals<QP, MP> = {
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
38
|
+
export type Globals<N extends string, T extends Typenames, QP, QR, MP, MR> = {
|
|
39
|
+
/** Handles errors, not handled by onError from queries and mutations. @Default undefined. */
|
|
40
|
+
onError?: (error: unknown, key: string, params: unknown, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void;
|
|
41
|
+
/** Query options. */
|
|
42
|
+
queries: {
|
|
43
|
+
/** Determines when useQuery fetch triggers should start fetching. Fetch is performed if function returns true.
|
|
44
|
+
* Fetch triggers are: 1) mount 2) cache key change 3) skipFetch value change to false.
|
|
45
|
+
* @Default FetchPolicy.NoCacheOrExpired */
|
|
46
|
+
fetchPolicy: (expired: boolean, params: unknown, state: QueryState<unknown, unknown>, store: Store, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean;
|
|
47
|
+
/** Disables any fetches when set to true. Triggers fetch when changed to false. @Default false */
|
|
48
|
+
skipFetch: boolean;
|
|
49
|
+
/** If set, this value updates expiresAt value of query state when query result is received. @Default undefined */
|
|
50
|
+
secondsToLive?: number;
|
|
51
|
+
};
|
|
50
52
|
};
|
|
51
53
|
export type CacheOptions = {
|
|
52
|
-
/**
|
|
53
|
-
* Enables additional validation with logging to console.warn. Recommened to enable in dev/testing mode.
|
|
54
|
-
* @default true in dev mode.
|
|
55
|
-
* */
|
|
54
|
+
/** Enables additional validation with logging to console.warn. Recommened to enable in dev/testing mode. @Default true in dev mode. */
|
|
56
55
|
additionalValidation: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Enables debug logs.
|
|
59
|
-
* @default false
|
|
60
|
-
*/
|
|
56
|
+
/** Enables debug logs. @Default false */
|
|
61
57
|
logsEnabled: boolean;
|
|
62
58
|
/**
|
|
63
59
|
* Enables deep comparison before merging entities to the state.
|
|
64
60
|
* Re-rendering is a heavier operation than comparison, so disabling it can lead to performance drop.
|
|
65
61
|
* Makes sense to disable only if merging equal results & entities to the state is a rare case.
|
|
66
|
-
* @
|
|
62
|
+
* @Default true
|
|
67
63
|
*/
|
|
68
64
|
deepComparisonEnabled: boolean;
|
|
69
65
|
};
|
|
@@ -76,22 +72,26 @@ export type EntitiesMap<T extends Typenames> = {
|
|
|
76
72
|
export type EntityIds<T extends Typenames> = {
|
|
77
73
|
[K in keyof T]?: Key[];
|
|
78
74
|
};
|
|
79
|
-
export type QueryInfo<T extends Typenames = Typenames, P = unknown, R = unknown> = Partial<Pick<Globals<
|
|
75
|
+
export type QueryInfo<N extends string, T extends Typenames = Typenames, P = unknown, R = unknown, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = Partial<Pick<Globals<N, T, QP, QR, MP, MR>['queries'], 'skipFetch' | 'secondsToLive'>> & {
|
|
80
76
|
query: NormalizedQuery<T, P, R>;
|
|
77
|
+
/** Determines when useQuery fetch triggers should start fetching. Fetch is performed if function returns true.
|
|
78
|
+
* Fetch triggers are: 1) mount 2) cache key change 3) skipFetch value change to false.
|
|
79
|
+
* @Default FetchPolicy.NoCacheOrExpired */
|
|
80
|
+
fetchPolicy?: (expired: boolean, params: P, queryState: QueryState<P, R>, store: Store, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean;
|
|
81
81
|
/** Merges results before saving to the store. Default implementation is using the latest result. */
|
|
82
|
-
mergeResults?: (oldResult: R | undefined, response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store) => R;
|
|
82
|
+
mergeResults?: (oldResult: R | undefined, response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => R;
|
|
83
83
|
/**
|
|
84
84
|
* 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.
|
|
85
|
-
* Default implementation uses `
|
|
85
|
+
* Default implementation uses `String()` or `JSON.stringify` depending on type.
|
|
86
86
|
* It is recommended to override it when default implementation is not optimal or when keys in params object can be sorted in random order etc.
|
|
87
|
-
|
|
87
|
+
*/
|
|
88
88
|
getCacheKey?: (params?: P) => Key;
|
|
89
|
-
/** Called after fetch
|
|
90
|
-
onCompleted?: (response: NormalizedQueryResponse<T, R> | undefined, error: unknown | undefined, params: P | undefined, store: Store) => void;
|
|
91
|
-
/** Called after fetch finished
|
|
92
|
-
onSuccess?: (response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store) => void;
|
|
89
|
+
/** Called after fetch completed either successfully or not. */
|
|
90
|
+
onCompleted?: (response: NormalizedQueryResponse<T, R> | undefined, error: unknown | undefined, params: P | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void;
|
|
91
|
+
/** Called after fetch finished successfully. */
|
|
92
|
+
onSuccess?: (response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void;
|
|
93
93
|
/** Called after fetch finished with error. Should return true if error was handled and does not require global onError handling. */
|
|
94
|
-
onError?: (error: unknown, params: P | undefined, store: Store) => boolean | void | null | undefined;
|
|
94
|
+
onError?: (error: unknown, params: P | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined;
|
|
95
95
|
};
|
|
96
96
|
export type Query<P = unknown, R = unknown> = (
|
|
97
97
|
/** Query parameters */
|
|
@@ -102,17 +102,14 @@ export type NormalizedQuery<T extends Typenames = Typenames, P = unknown, R = un
|
|
|
102
102
|
export type QueryState<P, R> = MutationState<P, R> & {
|
|
103
103
|
expiresAt?: number;
|
|
104
104
|
};
|
|
105
|
-
export type UseQueryOptions<
|
|
105
|
+
export type UseQueryOptions<N extends string, T extends Typenames, QK extends keyof (QP & QR), QP, QR, MP, MR> = {
|
|
106
106
|
query: QK;
|
|
107
107
|
params: QK extends keyof (QP | QR) ? QP[QK] : never;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
} & Pick<QueryInfo<T, QK extends keyof (QP | QR) ? QP[QK] : never, QK extends keyof (QP | QR) ? QR[QK] : never>, 'cachePolicy' | 'secondsToLive' | 'mergeResults' | 'onCompleted' | 'onSuccess' | 'onError'>;
|
|
111
|
-
export type QueryOptions<T extends Typenames, QP, QR, QK extends keyof (QP & QR)> = Omit<UseQueryOptions<T, QP, QR, QK>, 'skip' | 'cachePolicy'> & {
|
|
108
|
+
} & Pick<QueryInfo<N, T, QK extends keyof (QP | QR) ? QP[QK] : never, QK extends keyof (QP | QR) ? QR[QK] : never, QP, QR, MP, MR>, 'fetchPolicy' | 'skipFetch' | 'secondsToLive' | 'mergeResults' | 'onCompleted' | 'onSuccess' | 'onError'>;
|
|
109
|
+
export type QueryOptions<N extends string, T extends Typenames, QP, QR, QK extends keyof (QP & QR), MP, MR> = Omit<UseQueryOptions<N, T, QK, QP, QR, MP, MR>, 'skipFetch'> & {
|
|
112
110
|
/** If set to true, query will run only if it is expired or result not yet cached. */
|
|
113
111
|
onlyIfExpired?: boolean;
|
|
114
112
|
};
|
|
115
|
-
export type QueryCachePolicy = 'cache-first' | 'cache-and-fetch';
|
|
116
113
|
export type QueryResponse<R = unknown> = {
|
|
117
114
|
result: R;
|
|
118
115
|
/** If defined, overrides this value for query state, ignoring `secondsToLive`. */
|
|
@@ -124,7 +121,7 @@ export type QueryResult<R = unknown> = {
|
|
|
124
121
|
cancelled?: true;
|
|
125
122
|
result?: R;
|
|
126
123
|
};
|
|
127
|
-
export type MutationInfo<T extends Typenames = Typenames, P = unknown, R = unknown> = Pick<QueryInfo<T, P, R>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
124
|
+
export type MutationInfo<N extends string, T extends Typenames = Typenames, P = unknown, R = unknown, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = Pick<QueryInfo<N, T, P, R, QP, QR, MP, MR>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
128
125
|
mutation: NormalizedMutation<T, P, R>;
|
|
129
126
|
};
|
|
130
127
|
export type Mutation<P = unknown, R = unknown> = (
|
|
@@ -135,7 +132,7 @@ store: Store,
|
|
|
135
132
|
/** Signal is aborted for current mutation when the same mutation was called once again. */
|
|
136
133
|
abortSignal: AbortSignal) => Promise<MutationResponse<R>>;
|
|
137
134
|
export type NormalizedMutation<T extends Typenames = Typenames, P = unknown, R = unknown> = (...args: Parameters<Mutation<P, R>>) => Promise<NormalizedMutationResponse<T, R>>;
|
|
138
|
-
export type MutateOptions<T extends Typenames, MP, MR, MK extends keyof (MP & MR)> = Pick<MutationInfo<T, MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
135
|
+
export type MutateOptions<N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof (MP & MR)> = Pick<MutationInfo<N, T, MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never, QP, QR, MP, MR>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
139
136
|
mutation: MK;
|
|
140
137
|
params: MK extends keyof (MP | MR) ? MP[MK] : never;
|
|
141
138
|
};
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
|
-
import {
|
|
2
|
+
import { Actions } from './createActions';
|
|
3
|
+
import { Selectors } from './createSelectors';
|
|
3
4
|
import { Cache, Key, MutateOptions, MutationState, Typenames } from './types';
|
|
4
|
-
export declare const useMutation: <N extends string, T extends Typenames, MP, MR, MK extends keyof (MP & MR)>(cache: Cache<N, T,
|
|
5
|
+
export declare const useMutation: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof (MP & MR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, options: Omit<MutateOptions<N, T, QP, QR, MP, MR, MK>, "params">, abortControllers: WeakMap<Store, Record<Key, AbortController>>) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<import("./types").MutationResult<MK extends infer T_1 ? T_1 extends MK ? T_1 extends keyof MP & keyof MR ? MR[T_1] : never : never : never>>, MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
package/dist/useMutation.js
CHANGED
|
@@ -14,7 +14,7 @@ const react_1 = require("react");
|
|
|
14
14
|
const react_redux_1 = require("react-redux");
|
|
15
15
|
const mutate_1 = require("./mutate");
|
|
16
16
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
17
|
-
const useMutation = (cache, actions, options, abortControllers) => {
|
|
17
|
+
const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
18
18
|
var _a;
|
|
19
19
|
const { mutation: mutationKey, onCompleted, onSuccess, onError } = options;
|
|
20
20
|
const store = (0, react_redux_1.useStore)();
|
|
@@ -32,7 +32,7 @@ const useMutation = (cache, actions, options, abortControllers) => {
|
|
|
32
32
|
},
|
|
33
33
|
// mutate
|
|
34
34
|
(params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
-
return yield (0, mutate_1.mutate)('useMutation.mutate', store, cache, actions, mutationKey, params, abortControllers,
|
|
35
|
+
return yield (0, mutate_1.mutate)('useMutation.mutate', store, cache, actions, selectors, mutationKey, params, abortControllers,
|
|
36
36
|
// @ts-expect-error fix later
|
|
37
37
|
onCompleted, onSuccess, onError);
|
|
38
38
|
}),
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Actions } from './createActions';
|
|
2
|
+
import { Selectors } from './createSelectors';
|
|
2
3
|
import { Cache, QueryOptions, QueryState, Typenames, UseQueryOptions } from './types';
|
|
3
|
-
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions:
|
|
4
|
+
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, options: UseQueryOptions<N, T, QK, QP, QR, MP, MR>) => readonly [Omit<QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>, "expiresAt">, (options?: Partial<Pick<QueryOptions<N, T, QP, QR, QK, MP, MR>, "params" | "onlyIfExpired">>) => Promise<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>>];
|
|
5
|
+
/** Omit `expiresAt` from comparison */
|
|
6
|
+
export declare const useQuerySelectorStateComparer: <P, R>(state1: QueryState<P, R> | undefined, state2: QueryState<P, R> | undefined) => boolean;
|
package/dist/useQuery.js
CHANGED
|
@@ -9,14 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.useQuery = void 0;
|
|
12
|
+
exports.useQuerySelectorStateComparer = exports.useQuery = void 0;
|
|
13
13
|
const react_1 = require("react");
|
|
14
14
|
const react_redux_1 = require("react-redux");
|
|
15
15
|
const query_1 = require("./query");
|
|
16
16
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
17
|
-
const useQuery = (cache, actions, options) => {
|
|
17
|
+
const useQuery = (cache, actions, selectors, options) => {
|
|
18
18
|
var _a, _b, _c;
|
|
19
|
-
const { query: queryKey,
|
|
19
|
+
const { query: queryKey, skipFetch = false, params, secondsToLive, fetchPolicy = (_a = cache.queries[queryKey].fetchPolicy) !== null && _a !== void 0 ? _a : cache.globals.queries.fetchPolicy, mergeResults, onCompleted, onSuccess, onError, } = options;
|
|
20
20
|
const logsEnabled = cache.options.logsEnabled;
|
|
21
21
|
const getCacheKey = (_b = cache.queries[queryKey].getCacheKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetCacheKey);
|
|
22
22
|
const cacheStateSelector = cache.cacheStateSelector;
|
|
@@ -26,7 +26,7 @@ const useQuery = (cache, actions, options) => {
|
|
|
26
26
|
/** Fetch query with the new parameters, or refetch with the same if parameters not provided. */
|
|
27
27
|
const fetch = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
28
|
const paramsPassed = options && 'params' in options;
|
|
29
|
-
return yield (0, query_1.query)('useQuery.fetch', store, cache, actions, queryKey,
|
|
29
|
+
return yield (0, query_1.query)('useQuery.fetch', store, cache, actions, selectors, queryKey,
|
|
30
30
|
// @ts-expect-error fix later
|
|
31
31
|
paramsPassed ? getCacheKey(options.params) : cacheKey, paramsPassed ? options.params : params, // params type can also have null | undefined, thats why we don't check for it here
|
|
32
32
|
secondsToLive, options === null || options === void 0 ? void 0 : options.onlyIfExpired,
|
|
@@ -39,27 +39,27 @@ const useQuery = (cache, actions, options) => {
|
|
|
39
39
|
const queryState = (_c = (0, react_redux_1.useSelector)((state) => {
|
|
40
40
|
const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
41
41
|
return queryState; // TODO proper type
|
|
42
|
-
}, (
|
|
42
|
+
}, (exports.useQuerySelectorStateComparer))) !== null && _c !== void 0 ? _c : utilsAndConstants_1.EMPTY_OBJECT;
|
|
43
43
|
(0, react_1.useEffect)(() => {
|
|
44
|
-
if (
|
|
45
|
-
logsEnabled && (0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch', {
|
|
44
|
+
if (skipFetch) {
|
|
45
|
+
logsEnabled && (0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch', { skipFetch, queryKey, cacheKey });
|
|
46
46
|
return;
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const expired = queryState.expiresAt != null && queryState.expiresAt <= Date.now();
|
|
49
|
+
// @ts-expect-error params
|
|
50
|
+
if (!fetchPolicy(expired, params, queryState, store, selectors)) {
|
|
51
51
|
logsEnabled &&
|
|
52
|
-
(0, utilsAndConstants_1.log)('useQuery.useEffect
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
(0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch due to fetch policy', {
|
|
53
|
+
queryState,
|
|
54
|
+
expired,
|
|
55
|
+
queryKey,
|
|
56
|
+
cacheKey,
|
|
57
57
|
});
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
60
|
fetch();
|
|
61
61
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
62
|
-
}, [cacheKey,
|
|
62
|
+
}, [cacheKey, skipFetch]);
|
|
63
63
|
logsEnabled &&
|
|
64
64
|
(0, utilsAndConstants_1.log)('useQuery', {
|
|
65
65
|
cacheKey,
|
|
@@ -70,15 +70,16 @@ const useQuery = (cache, actions, options) => {
|
|
|
70
70
|
};
|
|
71
71
|
exports.useQuery = useQuery;
|
|
72
72
|
/** Omit `expiresAt` from comparison */
|
|
73
|
-
const
|
|
73
|
+
const useQuerySelectorStateComparer = (state1, state2) => {
|
|
74
74
|
if (state1 === state2) {
|
|
75
75
|
return true;
|
|
76
76
|
}
|
|
77
77
|
if (state1 === undefined || state2 === undefined) {
|
|
78
78
|
return false;
|
|
79
79
|
}
|
|
80
|
-
return (state1.
|
|
80
|
+
return (state1.params === state2.params &&
|
|
81
81
|
state1.loading === state2.loading &&
|
|
82
|
-
state1.
|
|
83
|
-
state1.
|
|
82
|
+
state1.result === state2.result &&
|
|
83
|
+
state1.error === state2.error);
|
|
84
84
|
};
|
|
85
|
+
exports.useQuerySelectorStateComparer = useQuerySelectorStateComparer;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CacheOptions, EntitiesMap, EntityChanges, Key, Typenames } from './types';
|
|
1
|
+
import type { CacheOptions, EntitiesMap, EntityChanges, Key, QueryState, Typenames } from './types';
|
|
2
2
|
export declare const PACKAGE_SHORT_NAME = "rrc";
|
|
3
3
|
export declare const optionalUtils: {
|
|
4
4
|
deepEqual?: (a: any, b: any) => boolean;
|
|
@@ -8,5 +8,11 @@ export declare const EMPTY_OBJECT: Readonly<{}>;
|
|
|
8
8
|
export declare const EMPTY_ARRAY: readonly never[];
|
|
9
9
|
export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key;
|
|
10
10
|
export declare const log: (tag: string, data?: unknown) => void;
|
|
11
|
+
export declare const FetchPolicy: {
|
|
12
|
+
/** Only if cache does not exist (result is undefined) or expired. */
|
|
13
|
+
NoCacheOrExpired: (expired: boolean, _: unknown, state: QueryState<unknown, unknown>) => boolean;
|
|
14
|
+
/** Every fetch trigger. */
|
|
15
|
+
Always: () => boolean;
|
|
16
|
+
};
|
|
11
17
|
export declare const applyEntityChanges: <T extends Typenames>(entities: EntitiesMap<T>, changes: EntityChanges<T>, options: CacheOptions) => EntitiesMap<T> | undefined;
|
|
12
18
|
export declare const isEmptyObject: (o: object) => boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmptyObject = exports.applyEntityChanges = exports.log = exports.defaultGetCacheKey = exports.EMPTY_ARRAY = exports.EMPTY_OBJECT = exports.IS_DEV = exports.optionalUtils = exports.PACKAGE_SHORT_NAME = void 0;
|
|
3
|
+
exports.isEmptyObject = exports.applyEntityChanges = exports.FetchPolicy = exports.log = exports.defaultGetCacheKey = exports.EMPTY_ARRAY = exports.EMPTY_OBJECT = exports.IS_DEV = exports.optionalUtils = exports.PACKAGE_SHORT_NAME = void 0;
|
|
4
4
|
exports.PACKAGE_SHORT_NAME = 'rrc';
|
|
5
5
|
exports.optionalUtils = {
|
|
6
6
|
deepEqual: undefined,
|
|
@@ -38,6 +38,14 @@ const log = (tag, data) => {
|
|
|
38
38
|
console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data);
|
|
39
39
|
};
|
|
40
40
|
exports.log = log;
|
|
41
|
+
exports.FetchPolicy = {
|
|
42
|
+
/** Only if cache does not exist (result is undefined) or expired. */
|
|
43
|
+
NoCacheOrExpired: (expired, _, state) => {
|
|
44
|
+
return expired || state.result === undefined;
|
|
45
|
+
},
|
|
46
|
+
/** Every fetch trigger. */
|
|
47
|
+
Always: () => true,
|
|
48
|
+
};
|
|
41
49
|
const applyEntityChanges = (entities, changes, options) => {
|
|
42
50
|
var _a, _b, _c, _d;
|
|
43
51
|
if (changes.merge && changes.entities) {
|
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.
|
|
5
|
+
"version": "0.13.0",
|
|
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",
|
|
13
|
+
"build": "yarn clean && yarn lint && tsc && rm -rf dist/testing && rm -rf dist/__tests__",
|
|
14
14
|
"test": "node node_modules/jest/bin/jest.js",
|
|
15
15
|
"preminor-rc": "yarn build && npm version preminor --preid rc",
|
|
16
16
|
"publish-rc": "npm publish --tag rc",
|