react-redux-cache 0.22.1 → 0.22.2

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.
@@ -1,168 +1,221 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.incrementChangeKey = exports.FetchPolicy = exports.createStateComparer = exports.isEmptyObject = exports.applyEntityChanges = exports.defaultGetCacheKey = exports.noop = exports.EMPTY_ARRAY = exports.EMPTY_OBJECT = exports.IS_DEV = exports.logWarn = exports.logDebug = exports.optionalUtils = exports.PACKAGE_SHORT_NAME = void 0;
4
- exports.PACKAGE_SHORT_NAME = 'rrc';
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.incrementChangeKey =
4
+ exports.FetchPolicy =
5
+ exports.createStateComparer =
6
+ exports.isEmptyObject =
7
+ exports.applyEntityChanges =
8
+ exports.defaultGetCacheKey =
9
+ exports.noop =
10
+ exports.EMPTY_ARRAY =
11
+ exports.EMPTY_OBJECT =
12
+ exports.IS_DEV =
13
+ exports.logWarn =
14
+ exports.logDebug =
15
+ exports.optionalUtils =
16
+ exports.PACKAGE_SHORT_NAME =
17
+ void 0
18
+ exports.PACKAGE_SHORT_NAME = 'rrc'
5
19
  exports.optionalUtils = {
6
- deepEqual: undefined,
7
- };
20
+ deepEqual: undefined,
21
+ }
8
22
  const logDebug = (tag, data) => {
9
- console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data);
10
- };
11
- exports.logDebug = logDebug;
23
+ console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
24
+ }
25
+ exports.logDebug = logDebug
12
26
  const logWarn = (tag, data) => {
13
- console.warn(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data);
14
- };
15
- exports.logWarn = logWarn;
16
- try {
17
- exports.optionalUtils.deepEqual = require('fast-deep-equal/es6');
27
+ console.warn(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
18
28
  }
19
- catch (_a) {
20
- (0, exports.logDebug)('deepEqual', 'fast-deep-equal optional dependency was not installed');
29
+ exports.logWarn = logWarn
30
+ try {
31
+ exports.optionalUtils.deepEqual = require('fast-deep-equal/es6')
32
+ } catch (_a) {
33
+ ;(0, exports.logDebug)('deepEqual', 'fast-deep-equal optional dependency was not installed')
21
34
  }
22
35
  exports.IS_DEV = (() => {
23
- try {
24
- return __DEV__;
25
- }
26
- catch (_a) {
27
- return process.env.NODE_ENV === 'development';
28
- }
29
- })();
30
- exports.EMPTY_OBJECT = Object.freeze({});
31
- exports.EMPTY_ARRAY = Object.freeze([]);
32
- const noop = () => { };
33
- exports.noop = noop;
36
+ try {
37
+ return __DEV__
38
+ } catch (_a) {
39
+ return process.env.NODE_ENV === 'development'
40
+ }
41
+ })()
42
+ exports.EMPTY_OBJECT = Object.freeze({})
43
+ exports.EMPTY_ARRAY = Object.freeze([])
44
+ const noop = () => {}
45
+ exports.noop = noop
34
46
  const defaultGetCacheKey = (params) => {
35
- switch (typeof params) {
36
- case 'string':
37
- case 'symbol':
38
- return params;
39
- case 'object':
40
- return JSON.stringify(params);
41
- default:
42
- return String(params);
43
- }
44
- };
45
- exports.defaultGetCacheKey = defaultGetCacheKey;
47
+ switch (typeof params) {
48
+ case 'string':
49
+ case 'symbol':
50
+ return params
51
+ case 'object':
52
+ return JSON.stringify(params)
53
+ default:
54
+ return String(params)
55
+ }
56
+ }
57
+ exports.defaultGetCacheKey = defaultGetCacheKey
46
58
  const applyEntityChanges = (entities, changes, options) => {
47
- var _a, _b, _c;
48
- if (changes.merge && changes.entities) {
49
- (0, exports.logWarn)('applyEntityChanges', 'merge and entities should not be both set');
59
+ var _a, _b, _c
60
+ if (changes.merge && changes.entities) {
61
+ ;(0, exports.logWarn)('applyEntityChanges', 'merge and entities should not be both set')
62
+ }
63
+ const {merge = changes.entities, replace, remove} = changes
64
+ if (!merge && !replace && !remove) {
65
+ return undefined
66
+ }
67
+ const mutable = options.mutableCollections
68
+ const deepEqual = options.deepComparisonEnabled ? exports.optionalUtils.deepEqual : undefined
69
+ let result
70
+ const objectWithAllTypenames = Object.assign(
71
+ Object.assign(Object.assign({}, changes.merge), changes.remove),
72
+ changes.replace,
73
+ )
74
+ for (const typename in objectWithAllTypenames) {
75
+ const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
76
+ const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
77
+ const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
78
+ if (
79
+ !entitiesToMerge &&
80
+ !entitiesToReplace &&
81
+ !(entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.length)
82
+ ) {
83
+ continue
50
84
  }
51
- const { merge = changes.entities, replace, remove } = changes;
52
- if (!merge && !replace && !remove) {
53
- return undefined;
85
+ if (options.additionalValidation) {
86
+ const mergeIds = entitiesToMerge && Object.keys(entitiesToMerge)
87
+ const replaceIds = entitiesToReplace && Object.keys(entitiesToReplace)
88
+ const idsSet = new Set(mergeIds)
89
+ replaceIds === null || replaceIds === void 0 ? void 0 : replaceIds.forEach((id) => idsSet.add(id))
90
+ entitiesToRemove === null || entitiesToRemove === void 0
91
+ ? void 0
92
+ : entitiesToRemove.forEach((id) => idsSet.add(String(id)))
93
+ const totalKeysInResponse =
94
+ ((_a = mergeIds === null || mergeIds === void 0 ? void 0 : mergeIds.length) !== null && _a !== void 0
95
+ ? _a
96
+ : 0) +
97
+ ((_b = replaceIds === null || replaceIds === void 0 ? void 0 : replaceIds.length) !== null &&
98
+ _b !== void 0
99
+ ? _b
100
+ : 0) +
101
+ ((_c =
102
+ entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.length) !==
103
+ null && _c !== void 0
104
+ ? _c
105
+ : 0)
106
+ if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
107
+ ;(0, exports.logWarn)(
108
+ 'applyEntityChanges',
109
+ 'merge, replace and remove changes have intersections for: ' + typename,
110
+ )
111
+ }
54
112
  }
55
- const mutable = options.mutableCollections;
56
- const deepEqual = options.deepComparisonEnabled ? exports.optionalUtils.deepEqual : undefined;
57
- let result;
58
- const objectWithAllTypenames = Object.assign(Object.assign(Object.assign({}, changes.merge), changes.remove), changes.replace);
59
- for (const typename in objectWithAllTypenames) {
60
- const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename];
61
- const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename];
62
- const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename];
63
- if (!entitiesToMerge && !entitiesToReplace && !(entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.length)) {
64
- continue;
113
+ const oldEntities = entities[typename]
114
+ let newEntities
115
+ entitiesToRemove === null || entitiesToRemove === void 0
116
+ ? void 0
117
+ : entitiesToRemove.forEach((id) => {
118
+ if (oldEntities === null || oldEntities === void 0 ? void 0 : oldEntities[id]) {
119
+ newEntities !== null && newEntities !== void 0
120
+ ? newEntities
121
+ : (newEntities = mutable ? oldEntities : Object.assign({}, oldEntities))
122
+ delete newEntities[id]
123
+ }
124
+ })
125
+ if (entitiesToReplace) {
126
+ for (const id in entitiesToReplace) {
127
+ const newEntity = entitiesToReplace[id]
128
+ if (
129
+ oldEntities === undefined ||
130
+ !(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntities[id], newEntity))
131
+ ) {
132
+ newEntities !== null && newEntities !== void 0
133
+ ? newEntities
134
+ : (newEntities = mutable
135
+ ? oldEntities !== null && oldEntities !== void 0
136
+ ? oldEntities
137
+ : {}
138
+ : Object.assign({}, oldEntities))
139
+ newEntities[id] = newEntity
65
140
  }
66
- if (options.additionalValidation) {
67
- const mergeIds = entitiesToMerge && Object.keys(entitiesToMerge);
68
- const replaceIds = entitiesToReplace && Object.keys(entitiesToReplace);
69
- const idsSet = new Set(mergeIds);
70
- replaceIds === null || replaceIds === void 0 ? void 0 : replaceIds.forEach((id) => idsSet.add(id));
71
- entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.forEach((id) => idsSet.add(String(id)));
72
- const totalKeysInResponse = ((_a = mergeIds === null || mergeIds === void 0 ? void 0 : mergeIds.length) !== null && _a !== void 0 ? _a : 0) + ((_b = replaceIds === null || replaceIds === void 0 ? void 0 : replaceIds.length) !== null && _b !== void 0 ? _b : 0) + ((_c = entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.length) !== null && _c !== void 0 ? _c : 0);
73
- if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
74
- (0, exports.logWarn)('applyEntityChanges', 'merge, replace and remove changes have intersections for: ' + typename);
75
- }
76
- }
77
- const oldEntities = entities[typename];
78
- let newEntities;
79
- entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.forEach((id) => {
80
- if (oldEntities === null || oldEntities === void 0 ? void 0 : oldEntities[id]) {
81
- newEntities !== null && newEntities !== void 0 ? newEntities : (newEntities = mutable ? oldEntities : Object.assign({}, oldEntities));
82
- delete newEntities[id];
83
- }
84
- });
85
- if (entitiesToReplace) {
86
- for (const id in entitiesToReplace) {
87
- const newEntity = entitiesToReplace[id];
88
- if (oldEntities === undefined || !(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntities[id], newEntity))) {
89
- newEntities !== null && newEntities !== void 0 ? newEntities : (newEntities = mutable ? (oldEntities !== null && oldEntities !== void 0 ? oldEntities : {}) : Object.assign({}, oldEntities));
90
- newEntities[id] = newEntity;
91
- }
92
- }
93
- }
94
- if (entitiesToMerge) {
95
- for (const id in entitiesToMerge) {
96
- const oldEntity = oldEntities === null || oldEntities === void 0 ? void 0 : oldEntities[id];
97
- const newEntity = Object.assign(Object.assign({}, oldEntity), entitiesToMerge[id]);
98
- if (!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntity, newEntity))) {
99
- newEntities !== null && newEntities !== void 0 ? newEntities : (newEntities = mutable ? (oldEntities !== null && oldEntities !== void 0 ? oldEntities : {}) : Object.assign({}, oldEntities));
100
- newEntities[id] = newEntity;
101
- }
102
- }
103
- }
104
- if (!newEntities) {
105
- continue;
106
- }
107
- if (mutable) {
108
- (0, exports.incrementChangeKey)(newEntities);
109
- if (result === undefined) {
110
- (0, exports.incrementChangeKey)(entities);
111
- result = entities;
112
- }
113
- }
114
- else {
115
- result !== null && result !== void 0 ? result : (result = Object.assign({}, entities));
141
+ }
142
+ }
143
+ if (entitiesToMerge) {
144
+ for (const id in entitiesToMerge) {
145
+ const oldEntity = oldEntities === null || oldEntities === void 0 ? void 0 : oldEntities[id]
146
+ const newEntity = Object.assign(Object.assign({}, oldEntity), entitiesToMerge[id])
147
+ if (!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntity, newEntity))) {
148
+ newEntities !== null && newEntities !== void 0
149
+ ? newEntities
150
+ : (newEntities = mutable
151
+ ? oldEntities !== null && oldEntities !== void 0
152
+ ? oldEntities
153
+ : {}
154
+ : Object.assign({}, oldEntities))
155
+ newEntities[id] = newEntity
116
156
  }
117
- result[typename] = newEntities;
157
+ }
118
158
  }
119
- options.logsEnabled &&
120
- (0, exports.logDebug)('applyEntityChanges', {
121
- entities,
122
- changes,
123
- options,
124
- result,
125
- });
126
- return result;
127
- };
128
- exports.applyEntityChanges = applyEntityChanges;
129
- const isEmptyObject = (obj) => {
130
- for (const _ in obj) {
131
- return false;
159
+ if (!newEntities) {
160
+ continue
132
161
  }
133
- return true;
134
- };
135
- exports.isEmptyObject = isEmptyObject;
162
+ if (mutable) {
163
+ ;(0, exports.incrementChangeKey)(newEntities)
164
+ if (result === undefined) {
165
+ ;(0, exports.incrementChangeKey)(entities)
166
+ result = entities
167
+ }
168
+ } else {
169
+ result !== null && result !== void 0 ? result : (result = Object.assign({}, entities))
170
+ }
171
+ result[typename] = newEntities
172
+ }
173
+ options.logsEnabled &&
174
+ (0, exports.logDebug)('applyEntityChanges', {
175
+ entities,
176
+ changes,
177
+ options,
178
+ result,
179
+ })
180
+ return result
181
+ }
182
+ exports.applyEntityChanges = applyEntityChanges
183
+ const isEmptyObject = (obj) => {
184
+ for (const _ in obj) {
185
+ return false
186
+ }
187
+ return true
188
+ }
189
+ exports.isEmptyObject = isEmptyObject
136
190
  const createStateComparer = (fields) => {
137
- return (x, y) => {
138
- if (x === y) {
139
- return true;
140
- }
141
- if (x === undefined || y === undefined) {
142
- return false;
143
- }
144
- for (let i = 0; i < fields.length; i += 1) {
145
- const key = fields[i];
146
- if (x[key] !== y[key]) {
147
- return false;
148
- }
149
- }
150
- return true;
151
- };
152
- };
153
- exports.createStateComparer = createStateComparer;
154
- exports.FetchPolicy = {
155
- NoCacheOrExpired: (expired, _params, state) => {
156
- return expired || state.result === undefined;
157
- },
158
- Always: () => true,
159
- };
160
- const incrementChangeKey = (mutable) => {
161
- if (mutable._changeKey === undefined) {
162
- mutable._changeKey = 0;
191
+ return (x, y) => {
192
+ if (x === y) {
193
+ return true
194
+ }
195
+ if (x === undefined || y === undefined) {
196
+ return false
163
197
  }
164
- else {
165
- mutable._changeKey += 1;
198
+ for (let i = 0; i < fields.length; i += 1) {
199
+ const key = fields[i]
200
+ if (x[key] !== y[key]) {
201
+ return false
202
+ }
166
203
  }
167
- };
168
- exports.incrementChangeKey = incrementChangeKey;
204
+ return true
205
+ }
206
+ }
207
+ exports.createStateComparer = createStateComparer
208
+ exports.FetchPolicy = {
209
+ NoCacheOrExpired: (expired, _params, state) => {
210
+ return expired || state.result === undefined
211
+ },
212
+ Always: () => true,
213
+ }
214
+ const incrementChangeKey = (mutable) => {
215
+ if (mutable._changeKey === undefined) {
216
+ mutable._changeKey = 0
217
+ } else {
218
+ mutable._changeKey += 1
219
+ }
220
+ }
221
+ exports.incrementChangeKey = incrementChangeKey
@@ -1,60 +1,61 @@
1
- import { PACKAGE_SHORT_NAME } from './utilsAndConstants';
1
+ import {PACKAGE_SHORT_NAME} from './utilsAndConstants'
2
+
2
3
  export const createActions = (name) => {
3
- const actionPrefix = `@${PACKAGE_SHORT_NAME}/${name}/`;
4
- const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`;
5
- const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChanges) => ({
6
- type: updateQueryStateAndEntitiesType,
7
- queryKey,
8
- queryCacheKey,
9
- state,
10
- entityChanges,
11
- });
12
- updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType;
13
- const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`;
14
- const updateMutationStateAndEntities = (mutationKey, state, entityChanges) => ({
15
- type: updateMutationStateAndEntitiesType,
16
- mutationKey,
17
- state,
18
- entityChanges,
19
- });
20
- updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType;
21
- const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`;
22
- const mergeEntityChanges = (changes) => ({
23
- type: mergeEntityChangesType,
24
- changes,
25
- });
26
- mergeEntityChanges.type = mergeEntityChangesType;
27
- const invalidateQueryType = `${actionPrefix}invalidateQuery`;
28
- const invalidateQuery = (queries) => ({
29
- type: invalidateQueryType,
30
- queries,
31
- });
32
- invalidateQuery.type = invalidateQueryType;
33
- const clearQueryStateType = `${actionPrefix}clearQueryState`;
34
- const clearQueryState = (queries) => ({
35
- type: clearQueryStateType,
36
- queries,
37
- });
38
- clearQueryState.type = clearQueryStateType;
39
- const clearMutationStateType = `${actionPrefix}clearMutationState`;
40
- const clearMutationState = (mutationKeys) => ({
41
- type: clearMutationStateType,
42
- mutationKeys,
43
- });
44
- clearMutationState.type = clearMutationStateType;
45
- const clearCacheType = `${actionPrefix}clearCache`;
46
- const clearCache = (stateToKeep) => ({
47
- type: clearCacheType,
48
- stateToKeep,
49
- });
50
- clearCache.type = clearCacheType;
51
- return {
52
- updateQueryStateAndEntities,
53
- updateMutationStateAndEntities,
54
- mergeEntityChanges,
55
- invalidateQuery,
56
- clearQueryState,
57
- clearMutationState,
58
- clearCache,
59
- };
60
- };
4
+ const actionPrefix = `@${PACKAGE_SHORT_NAME}/${name}/`
5
+ const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`
6
+ const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChanges) => ({
7
+ type: updateQueryStateAndEntitiesType,
8
+ queryKey,
9
+ queryCacheKey,
10
+ state,
11
+ entityChanges,
12
+ })
13
+ updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType
14
+ const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`
15
+ const updateMutationStateAndEntities = (mutationKey, state, entityChanges) => ({
16
+ type: updateMutationStateAndEntitiesType,
17
+ mutationKey,
18
+ state,
19
+ entityChanges,
20
+ })
21
+ updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType
22
+ const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`
23
+ const mergeEntityChanges = (changes) => ({
24
+ type: mergeEntityChangesType,
25
+ changes,
26
+ })
27
+ mergeEntityChanges.type = mergeEntityChangesType
28
+ const invalidateQueryType = `${actionPrefix}invalidateQuery`
29
+ const invalidateQuery = (queries) => ({
30
+ type: invalidateQueryType,
31
+ queries,
32
+ })
33
+ invalidateQuery.type = invalidateQueryType
34
+ const clearQueryStateType = `${actionPrefix}clearQueryState`
35
+ const clearQueryState = (queries) => ({
36
+ type: clearQueryStateType,
37
+ queries,
38
+ })
39
+ clearQueryState.type = clearQueryStateType
40
+ const clearMutationStateType = `${actionPrefix}clearMutationState`
41
+ const clearMutationState = (mutationKeys) => ({
42
+ type: clearMutationStateType,
43
+ mutationKeys,
44
+ })
45
+ clearMutationState.type = clearMutationStateType
46
+ const clearCacheType = `${actionPrefix}clearCache`
47
+ const clearCache = (stateToKeep) => ({
48
+ type: clearCacheType,
49
+ stateToKeep,
50
+ })
51
+ clearCache.type = clearCacheType
52
+ return {
53
+ updateQueryStateAndEntities,
54
+ updateMutationStateAndEntities,
55
+ mergeEntityChanges,
56
+ invalidateQuery,
57
+ clearQueryState,
58
+ clearMutationState,
59
+ clearCache,
60
+ }
61
+ }