react-redux-cache 0.19.3 → 0.19.4-rc.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.
Files changed (57) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/createActions.js +65 -0
  3. package/dist/cjs/createCache.js +208 -0
  4. package/dist/cjs/createCacheReducer.js +285 -0
  5. package/dist/cjs/createSelectors.js +68 -0
  6. package/dist/cjs/index.js +83 -0
  7. package/dist/cjs/mutate.js +161 -0
  8. package/dist/cjs/query.js +180 -0
  9. package/dist/cjs/types.js +2 -0
  10. package/dist/cjs/useMutation.js +82 -0
  11. package/dist/cjs/useQuery.js +121 -0
  12. package/dist/cjs/utilsAndConstants.js +189 -0
  13. package/dist/esm/createActions.js +61 -0
  14. package/dist/esm/createCache.js +203 -0
  15. package/dist/esm/createCacheReducer.js +271 -0
  16. package/dist/esm/createSelectors.js +64 -0
  17. package/dist/esm/index.js +3 -0
  18. package/dist/esm/mutate.js +157 -0
  19. package/dist/esm/query.js +169 -0
  20. package/dist/esm/types.js +1 -0
  21. package/dist/esm/useMutation.js +88 -0
  22. package/dist/esm/useQuery.js +125 -0
  23. package/dist/esm/utilsAndConstants.js +168 -0
  24. package/dist/types/createActions.d.ts +106 -0
  25. package/dist/types/createCache.d.ts +712 -0
  26. package/dist/types/createCacheReducer.d.ts +11 -0
  27. package/dist/types/createSelectors.d.ts +79 -0
  28. package/dist/types/index.d.ts +3 -0
  29. package/dist/types/mutate.d.ts +94 -0
  30. package/dist/types/query.d.ts +122 -0
  31. package/dist/types/types.d.ts +322 -0
  32. package/dist/types/useMutation.d.ts +39 -0
  33. package/dist/types/useQuery.d.ts +40 -0
  34. package/dist/types/utilsAndConstants.d.ts +39 -0
  35. package/package.json +20 -10
  36. package/dist/createActions.d.ts +0 -83
  37. package/dist/createActions.js +0 -64
  38. package/dist/createCache.d.ts +0 -378
  39. package/dist/createCache.js +0 -192
  40. package/dist/createCacheReducer.d.ts +0 -3
  41. package/dist/createCacheReducer.js +0 -243
  42. package/dist/createSelectors.d.ts +0 -18
  43. package/dist/createSelectors.js +0 -61
  44. package/dist/index.d.ts +0 -3
  45. package/dist/index.js +0 -53
  46. package/dist/mutate.d.ts +0 -4
  47. package/dist/mutate.js +0 -98
  48. package/dist/query.d.ts +0 -4
  49. package/dist/query.js +0 -107
  50. package/dist/types.d.ts +0 -187
  51. package/dist/types.js +0 -3
  52. package/dist/useMutation.d.ts +0 -4
  53. package/dist/useMutation.js +0 -61
  54. package/dist/useQuery.d.ts +0 -4
  55. package/dist/useQuery.js +0 -77
  56. package/dist/utilsAndConstants.d.ts +0 -20
  57. package/dist/utilsAndConstants.js +0 -161
@@ -1,243 +0,0 @@
1
- "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.createCacheReducer = void 0;
15
- const utilsAndConstants_1 = require("./utilsAndConstants");
16
- const optionalQueryKeys = [
17
- 'error',
18
- 'expiresAt',
19
- 'result',
20
- 'params',
21
- 'loading',
22
- ];
23
- const optionalMutationKeys = [
24
- 'error',
25
- 'result',
26
- 'params',
27
- 'loading',
28
- ];
29
- const createCacheReducer = (actions, queryKeys, cacheOptions) => {
30
- const initialState = Object.freeze({
31
- entities: Object.freeze({}),
32
- queries: Object.freeze(queryKeys.reduce((result, x) => {
33
- result[x] = Object.freeze({});
34
- return result;
35
- }, {})),
36
- mutations: Object.freeze({}),
37
- });
38
- cacheOptions.logsEnabled &&
39
- (0, utilsAndConstants_1.log)('createCacheReducer', {
40
- queryKeys,
41
- initialState,
42
- });
43
- const deepEqual = cacheOptions.deepComparisonEnabled ? utilsAndConstants_1.optionalUtils.deepEqual : undefined;
44
- return (state = initialState, action) => {
45
- switch (action.type) {
46
- case actions.updateQueryStateAndEntities.type: {
47
- const { queryKey, queryCacheKey, state: queryState, entityChanges, } = action;
48
- const oldQueryState = state.queries[queryKey][queryCacheKey];
49
- let newQueryState = queryState && Object.assign(Object.assign({}, oldQueryState), queryState);
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
- }
63
- // remove undefined optional fields
64
- for (const key of optionalQueryKeys) {
65
- if (key in newQueryState && newQueryState[key] === undefined) {
66
- delete newQueryState[key];
67
- }
68
- }
69
- // skip if new state deep equals to the old state
70
- if (deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldQueryState !== null && oldQueryState !== void 0 ? oldQueryState : utilsAndConstants_1.EMPTY_OBJECT, newQueryState)) {
71
- newQueryState = undefined;
72
- }
73
- }
74
- const newEntities = entityChanges && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChanges, cacheOptions);
75
- let newState;
76
- if (newEntities) {
77
- newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
78
- newState.entities = newEntities;
79
- }
80
- if (newQueryState) {
81
- if (!(0, utilsAndConstants_1.isEmptyObject)(newQueryState)) {
82
- newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
83
- newState.queries = Object.assign(Object.assign({}, state.queries), { [queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), { [queryCacheKey]: newQueryState }) });
84
- }
85
- else if (oldQueryState !== undefined) {
86
- // empty states are removed
87
- const _a = state.queries[queryKey], _b = queryCacheKey, _ = _a[_b], withoutCacheKey = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
88
- newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
89
- newState.queries = Object.assign(Object.assign({}, state.queries), { [queryKey]: withoutCacheKey });
90
- }
91
- }
92
- return newState !== null && newState !== void 0 ? newState : state;
93
- }
94
- case actions.updateMutationStateAndEntities.type: {
95
- const { mutationKey, state: mutationState, entityChanges, } = action;
96
- const oldMutationState = state.mutations[mutationKey];
97
- let newMutationState = mutationState && Object.assign(Object.assign({}, oldMutationState), mutationState);
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
- }
111
- // remove optional fields with default values
112
- for (const key of optionalMutationKeys) {
113
- if (key in newMutationState && newMutationState[key] === undefined) {
114
- delete newMutationState[key];
115
- }
116
- }
117
- // skip if new state deep equals to the old state
118
- if (deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldMutationState !== null && oldMutationState !== void 0 ? oldMutationState : utilsAndConstants_1.EMPTY_OBJECT, newMutationState)) {
119
- newMutationState = undefined;
120
- }
121
- }
122
- const newEntities = entityChanges && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChanges, cacheOptions);
123
- let newState;
124
- if (newEntities) {
125
- newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
126
- newState.entities = newEntities;
127
- }
128
- if (newMutationState) {
129
- if (!(0, utilsAndConstants_1.isEmptyObject)(newMutationState)) {
130
- newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
131
- newState.mutations = Object.assign(Object.assign({}, state.mutations), { [mutationKey]: newMutationState });
132
- }
133
- else if (oldMutationState !== undefined) {
134
- // empty states are removed
135
- const _c = state.mutations, _d = mutationKey, _ = _c[_d], withoutMutationKey = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
136
- newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
137
- newState.mutations = withoutMutationKey;
138
- }
139
- }
140
- return newState !== null && newState !== void 0 ? newState : state;
141
- }
142
- case actions.mergeEntityChanges.type: {
143
- const { changes } = action;
144
- const newEntities = (0, utilsAndConstants_1.applyEntityChanges)(state.entities, changes, cacheOptions);
145
- return newEntities ? Object.assign(Object.assign({}, state), { entities: newEntities }) : state;
146
- }
147
- case actions.invalidateQuery.type: {
148
- const { queries: queriesToInvalidate } = action;
149
- if (queriesToInvalidate.length === 0) {
150
- return state;
151
- }
152
- const now = Date.now();
153
- let newQueries = undefined;
154
- for (const { query: queryKey, cacheKey, expiresAt = now } of queriesToInvalidate) {
155
- const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[queryKey];
156
- if (cacheKey != null) {
157
- if (queryStates[cacheKey]) {
158
- const queryState = queryStates[cacheKey];
159
- if (queryState && queryState.expiresAt !== expiresAt) {
160
- newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
161
- if (state.queries[queryKey] === newQueries[queryKey]) {
162
- newQueries[queryKey] = Object.assign({}, newQueries[queryKey]);
163
- }
164
- // @ts-expect-error fix type later
165
- newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), { expiresAt });
166
- if (expiresAt === undefined) {
167
- delete newQueries[queryKey][cacheKey].expiresAt;
168
- }
169
- }
170
- }
171
- }
172
- else {
173
- for (const cacheKey in queryStates) {
174
- const queryState = queryStates[cacheKey];
175
- if (queryState && queryState.expiresAt !== expiresAt) {
176
- newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
177
- if (state.queries[queryKey] === newQueries[queryKey]) {
178
- newQueries[queryKey] = Object.assign({}, newQueries[queryKey]);
179
- }
180
- newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), { expiresAt });
181
- if (expiresAt === undefined) {
182
- delete newQueries[queryKey][cacheKey].expiresAt;
183
- }
184
- }
185
- }
186
- }
187
- }
188
- return newQueries === undefined
189
- ? state
190
- : Object.assign(Object.assign({}, state), { queries: newQueries });
191
- }
192
- case actions.clearQueryState.type: {
193
- const { queries: queriesToClear } = action;
194
- if (queriesToClear.length === 0) {
195
- return state;
196
- }
197
- let newQueries = undefined;
198
- for (const { query: queryKey, cacheKey } of queriesToClear) {
199
- const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[queryKey];
200
- if (cacheKey != null) {
201
- if (queryStates[cacheKey]) {
202
- newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
203
- if (state.queries[queryKey] === newQueries[queryKey]) {
204
- newQueries[queryKey] = Object.assign({}, newQueries[queryKey]);
205
- }
206
- delete newQueries[queryKey][cacheKey];
207
- }
208
- }
209
- else if (queryStates !== utilsAndConstants_1.EMPTY_OBJECT) {
210
- newQueries !== null && newQueries !== void 0 ? newQueries : (newQueries = Object.assign({}, state.queries));
211
- newQueries[queryKey] = utilsAndConstants_1.EMPTY_OBJECT;
212
- }
213
- }
214
- return newQueries === undefined
215
- ? state
216
- : Object.assign(Object.assign({}, state), { queries: newQueries });
217
- }
218
- case actions.clearMutationState.type: {
219
- const { mutationKeys } = action;
220
- if (mutationKeys.length === 0) {
221
- return state;
222
- }
223
- let newMutations = undefined;
224
- for (const mutation of mutationKeys) {
225
- if (state.mutations[mutation]) {
226
- newMutations !== null && newMutations !== void 0 ? newMutations : (newMutations = Object.assign({}, state.mutations));
227
- delete newMutations[mutation];
228
- }
229
- }
230
- return newMutations === undefined
231
- ? state
232
- : Object.assign(Object.assign({}, state), { mutations: newMutations });
233
- }
234
- case actions.clearCache.type: {
235
- const { stateToKeep } = action;
236
- return stateToKeep
237
- ? Object.assign(Object.assign({}, initialState), stateToKeep) : initialState;
238
- }
239
- }
240
- return state;
241
- };
242
- };
243
- exports.createCacheReducer = createCacheReducer;
@@ -1,18 +0,0 @@
1
- import { Cache, Key, MutationState, QueryState, Typenames } from './types';
2
- export type Selectors<N extends string = string, T extends Typenames = Typenames, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = ReturnType<typeof createSelectors<N, T, QP, QR, MP, MR>>;
3
- export declare const createSelectors: <N extends string, T extends Typenames, QP, QR, MP, MR>(cache: Cache<N, T, QP, QR, MP, MR>) => {
4
- selectQueryState: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => QueryState<T, QK extends keyof (QP | QR) ? QP[QK] : never, QK extends keyof (QP | QR) ? QR[QK] : never>;
5
- selectQueryResult: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
6
- selectQueryLoading: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => false | Promise<import("./types").NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>>;
7
- selectQueryError: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
8
- selectQueryParams: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
9
- selectQueryExpiresAt: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
10
- selectMutationState: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => MutationState<T, MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never>;
11
- selectMutationResult: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
12
- selectMutationLoading: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => false | Promise<import("./types").NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>>;
13
- selectMutationError: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => Error | undefined;
14
- selectMutationParams: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
15
- selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
16
- selectEntities: (state: unknown) => import("./types").EntitiesMap<T>;
17
- selectEntitiesByTypename: <TN extends keyof T>(state: unknown, typename: TN) => import("./types").EntitiesMap<T>[TN];
18
- };
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSelectors = void 0;
4
- const utilsAndConstants_1 = require("./utilsAndConstants");
5
- const createSelectors = (cache) => {
6
- const selectEntityById = (state, id, typename) => {
7
- var _a;
8
- return id == null ? undefined : (_a = cache.cacheStateSelector(state).entities[typename]) === null || _a === void 0 ? void 0 : _a[id];
9
- };
10
- const selectQueryState = (state, query, cacheKey) => {
11
- var _a;
12
- // @ts-expect-error fix later
13
- return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
14
- };
15
- const selectMutationState = (state, mutation) => {
16
- var _a;
17
- // @ts-expect-error fix later
18
- return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
19
- };
20
- return {
21
- selectQueryState,
22
- selectQueryResult: (state, query, cacheKey) => {
23
- return selectQueryState(state, query, cacheKey).result;
24
- },
25
- selectQueryLoading: (state, query, cacheKey) => {
26
- var _a;
27
- return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false;
28
- },
29
- selectQueryError: (state, query, cacheKey) => {
30
- return selectQueryState(state, query, cacheKey).error;
31
- },
32
- selectQueryParams: (state, query, cacheKey) => {
33
- return selectQueryState(state, query, cacheKey).params;
34
- },
35
- selectQueryExpiresAt: (state, query, cacheKey) => {
36
- return selectQueryState(state, query, cacheKey).expiresAt;
37
- },
38
- selectMutationState,
39
- selectMutationResult: (state, mutation) => {
40
- return selectMutationState(state, mutation).result;
41
- },
42
- selectMutationLoading: (state, mutation) => {
43
- var _a;
44
- return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false;
45
- },
46
- selectMutationError: (state, mutation) => {
47
- return selectMutationState(state, mutation).error;
48
- },
49
- selectMutationParams: (state, mutation) => {
50
- return selectMutationState(state, mutation).params;
51
- },
52
- selectEntityById,
53
- selectEntities: (state) => {
54
- return cache.cacheStateSelector(state).entities;
55
- },
56
- selectEntitiesByTypename: (state, typename) => {
57
- return cache.cacheStateSelector(state).entities[typename];
58
- },
59
- };
60
- };
61
- exports.createSelectors = createSelectors;
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export { createCache, withTypenames } from './createCache';
2
- export * from './types';
3
- export { createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject } from './utilsAndConstants';
package/dist/index.js DELETED
@@ -1,53 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.isEmptyObject = exports.FetchPolicy = exports.defaultGetCacheKey = exports.createStateComparer = exports.withTypenames = exports.createCache = void 0;
18
- var createCache_1 = require("./createCache");
19
- Object.defineProperty(exports, "createCache", { enumerable: true, get: function () { return createCache_1.createCache; } });
20
- Object.defineProperty(exports, "withTypenames", { enumerable: true, get: function () { return createCache_1.withTypenames; } });
21
- __exportStar(require("./types"), exports);
22
- var utilsAndConstants_1 = require("./utilsAndConstants");
23
- Object.defineProperty(exports, "createStateComparer", { enumerable: true, get: function () { return utilsAndConstants_1.createStateComparer; } });
24
- Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: function () { return utilsAndConstants_1.defaultGetCacheKey; } });
25
- Object.defineProperty(exports, "FetchPolicy", { enumerable: true, get: function () { return utilsAndConstants_1.FetchPolicy; } });
26
- Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return utilsAndConstants_1.isEmptyObject; } });
27
- // Backlog
28
- // ! high (1.0.0-rc.0)
29
- // generate full api docs
30
- // optimistic response
31
- // features list (table)
32
- // ! medium
33
- // onCancel & onAbort
34
- // remove empty entities and queries from state
35
- // globals for success, completions and loading states?
36
- // make query key / cache key difference more clear in the docs
37
- // check type of function arguments in dev
38
- // make skipFetch a function?
39
- // example -> playground with changable options
40
- // ! low
41
- // make error type generic
42
- // allow multiple mutation with same keys?
43
- // easy access to all currently loading queries and mutations?
44
- // cancellation to queries
45
- // if mutation & query already loading - make options: last, throttle, debounce, parallel?
46
- // add refresh interval for queries that are mounted?
47
- // readonly types?
48
- // proper types, remove as, any, todo
49
- // add number of retries param?
50
- // mutation state wrong type - can be undefined?
51
- // ! on hold
52
- // better support queries without params: useQuery & selectors optional cache key (making useQuery params optional doesn't seem like a good idea)
53
- // cancel all queries / mutations (already possible by recreating the store - this lib is pure)
package/dist/mutate.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { Actions } from './createActions';
2
- import { Selectors } from './createSelectors';
3
- import type { Cache, Key, MutationResult, Store, Typenames } from './types';
4
- export declare const mutate: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof (MP & MR)>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, mutationKey: MK, params: MK extends keyof (MP | MR) ? MP[MK] : never, abortControllers: WeakMap<Store, Record<Key, AbortController>>, onCompleted?: ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & string]> | undefined, error: unknown | undefined, params: MP[keyof MP & keyof MR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & number]> | undefined, error: unknown | undefined, params: MP[keyof MP & keyof MR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & symbol]> | undefined, error: unknown | undefined, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onSuccess?: ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & string]>, params: MP[keyof MP & keyof MR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & number]>, params: MP[keyof MP & keyof MR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & symbol]>, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onError?: ((error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | undefined) => Promise<MutationResult<MK extends keyof (MP | MR) ? MR[MK] : never>>;
package/dist/mutate.js DELETED
@@ -1,98 +0,0 @@
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 = void 0;
13
- const utilsAndConstants_1 = require("./utilsAndConstants");
14
- const mutate = (logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_1, params_1, abortControllers_1, ...args_1) => __awaiter(void 0, [logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_1, params_1, abortControllers_1, ...args_1], void 0, function* (logTag, store, cache, actions, selectors, mutationKey, params, abortControllers, onCompleted = cache.mutations[mutationKey].onCompleted, onSuccess = cache.mutations[mutationKey].onSuccess, onError = cache.mutations[mutationKey].onError) {
15
- var _a, _b;
16
- const { updateMutationStateAndEntities } = actions;
17
- let abortControllersOfStore = abortControllers.get(store);
18
- if (abortControllersOfStore === undefined) {
19
- abortControllersOfStore = {};
20
- abortControllers.set(store, abortControllersOfStore);
21
- }
22
- {
23
- const abortController = abortControllersOfStore[mutationKey];
24
- cache.options.logsEnabled &&
25
- (0, utilsAndConstants_1.log)(logTag, {
26
- mutationKey,
27
- params,
28
- previousAborted: abortController !== undefined,
29
- });
30
- if (abortController !== undefined) {
31
- abortController.abort();
32
- }
33
- }
34
- const abortController = new AbortController();
35
- abortControllersOfStore[mutationKey] = abortController;
36
- const mutatePromise = cache.mutations[mutationKey].mutation(
37
- // @ts-expect-error fix later
38
- params, store, abortController.signal);
39
- store.dispatch(updateMutationStateAndEntities(mutationKey, {
40
- // @ts-expect-error TODO fix types
41
- loading: mutatePromise,
42
- params,
43
- result: undefined,
44
- }));
45
- let response;
46
- let error;
47
- try {
48
- response = yield mutatePromise;
49
- }
50
- catch (e) {
51
- error = e;
52
- }
53
- cache.options.logsEnabled &&
54
- (0, utilsAndConstants_1.log)(`${logTag} finished`, {
55
- response,
56
- error,
57
- aborted: abortController.signal.aborted,
58
- });
59
- if (abortController.signal.aborted) {
60
- return ABORTED_RESULT;
61
- }
62
- delete abortControllersOfStore[mutationKey];
63
- if (error) {
64
- store.dispatch(updateMutationStateAndEntities(mutationKey, {
65
- error: error,
66
- loading: undefined,
67
- }));
68
- // @ts-expect-error params
69
- if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store, actions, selectors))) {
70
- (_b = (_a = cache.globals).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error,
71
- // @ts-expect-error mutationKey
72
- mutationKey, params, store, actions, selectors);
73
- }
74
- onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(
75
- // @ts-expect-error response
76
- response, error, params, store, actions, selectors);
77
- return { error };
78
- }
79
- if (response) {
80
- const newState = {
81
- error: undefined,
82
- loading: undefined,
83
- result: response.result,
84
- };
85
- store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response));
86
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(
87
- // @ts-expect-error response
88
- response, params, store, actions, selectors);
89
- onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(
90
- // @ts-expect-error response
91
- response, error, params, store, actions, selectors);
92
- // @ts-expect-error fix later
93
- return { result: response.result };
94
- }
95
- throw new Error(`${logTag}: both error and response are not defined`);
96
- });
97
- exports.mutate = mutate;
98
- const ABORTED_RESULT = Object.freeze({ aborted: true });
package/dist/query.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { Actions } from './createActions';
2
- import { Selectors } from './createSelectors';
3
- import type { Cache, Key, QueryResult, Store, Typenames } from './types';
4
- export declare const query: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, queryKey: QK, cacheKey: Key, params: QK extends keyof (QP | QR) ? QP[QK] : never, secondsToLive: number | undefined, onlyIfExpired: boolean | undefined, mergeResults?: ((oldResult: QR[keyof QP & keyof QR & string] | undefined, response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => QR[keyof QP & keyof QR & string]) | ((oldResult: QR[keyof QP & keyof QR & number] | undefined, response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => QR[keyof QP & keyof QR & number]) | ((oldResult: QR[keyof QP & keyof QR & symbol] | undefined, response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => QR[keyof QP & keyof QR & symbol]) | undefined, onCompleted?: ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]> | undefined, error: unknown | undefined, params: QP[keyof QP & keyof QR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]> | undefined, error: unknown | undefined, params: QP[keyof QP & keyof QR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]> | undefined, error: unknown | undefined, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onSuccess?: ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onError?: ((error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | undefined) => Promise<QueryResult<QK extends keyof (QP | QR) ? QR[QK] : never>>;
package/dist/query.js DELETED
@@ -1,107 +0,0 @@
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 utilsAndConstants_1 = require("./utilsAndConstants");
14
- const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, params, secondsToLive, onlyIfExpired, mergeResults, onCompleted, onSuccess, onError) => __awaiter(void 0, void 0, void 0, function* () {
15
- var _a, _b, _c, _d;
16
- if (secondsToLive === void 0) { secondsToLive = (_a = cache.queries[queryKey].secondsToLive) !== null && _a !== void 0 ? _a : cache.globals.queries.secondsToLive; }
17
- if (mergeResults === void 0) { mergeResults = cache.queries[queryKey].mergeResults; }
18
- if (onCompleted === void 0) { onCompleted = cache.queries[queryKey].onCompleted; }
19
- if (onSuccess === void 0) { onSuccess = cache.queries[queryKey].onSuccess; }
20
- if (onError === void 0) { onError = cache.queries[queryKey].onError; }
21
- const { selectQueryResult, selectQueryState } = selectors;
22
- const logsEnabled = cache.options.logsEnabled;
23
- const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey);
24
- if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
25
- logsEnabled &&
26
- (0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: already loading`, {
27
- queryStateOnStart,
28
- params,
29
- cacheKey,
30
- });
31
- const error = yield queryStateOnStart.loading.then(utilsAndConstants_1.NOOP).catch(catchAndReturn);
32
- return error
33
- ? {
34
- cancelled: 'loading',
35
- error,
36
- }
37
- : {
38
- cancelled: 'loading',
39
- result: selectQueryResult(store.getState(), queryKey, cacheKey),
40
- };
41
- }
42
- if (onlyIfExpired && (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.expiresAt) != null && queryStateOnStart.expiresAt > Date.now()) {
43
- logsEnabled &&
44
- (0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: not expired yet`, {
45
- queryStateOnStart,
46
- params,
47
- cacheKey,
48
- onlyIfExpired,
49
- });
50
- return {
51
- cancelled: 'not-expired',
52
- result: queryStateOnStart.result,
53
- };
54
- }
55
- const { updateQueryStateAndEntities } = actions;
56
- const fetchPromise = cache.queries[queryKey].query(
57
- // @ts-expect-error fix later
58
- params, store);
59
- store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, {
60
- loading: fetchPromise,
61
- params,
62
- }));
63
- logsEnabled && (0, utilsAndConstants_1.log)(`${logTag} started`, { queryKey, params, cacheKey, queryStateOnStart, onlyIfExpired });
64
- let response;
65
- try {
66
- response = yield fetchPromise;
67
- }
68
- catch (error) {
69
- store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, {
70
- error: error,
71
- loading: undefined,
72
- }));
73
- // @ts-expect-error params
74
- if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
75
- (_c = (_b = cache.globals).onError) === null || _c === void 0 ? void 0 : _c.call(_b, error,
76
- // @ts-expect-error queryKey
77
- queryKey, params, store, actions, selectors);
78
- }
79
- onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(undefined, error,
80
- // @ts-expect-error params
81
- params, store, actions, selectors);
82
- return { error };
83
- }
84
- const newState = {
85
- error: undefined,
86
- loading: undefined,
87
- expiresAt: (_d = response.expiresAt) !== null && _d !== void 0 ? _d : (secondsToLive != null ? Date.now() + secondsToLive * 1000 : undefined),
88
- result: mergeResults
89
- ? mergeResults(
90
- // @ts-expect-error fix later
91
- selectQueryResult(store.getState(), queryKey, cacheKey), response, params, store, actions, selectors)
92
- : response.result,
93
- };
94
- store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response));
95
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(
96
- // @ts-expect-error response
97
- response, params, store, actions, selectors);
98
- onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(
99
- // @ts-expect-error response
100
- response, undefined, params, store, actions, selectors);
101
- return {
102
- // @ts-expect-error fix types
103
- result: newState === null || newState === void 0 ? void 0 : newState.result,
104
- };
105
- });
106
- exports.query = query;
107
- const catchAndReturn = (x) => x;