react-redux-cache 0.21.0 → 0.22.1

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