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.
package/README.md CHANGED
@@ -333,8 +333,8 @@ For huge collections (> 1000 items, see benchmark) immutable approach may be a b
333
333
 
334
334
  | Collection size | 0 | 1000 | 10000 | 100000 | 1000000 |
335
335
  |-|-|-|-|-|-|
336
- | immutable | 1.78 | 1.69 | 5.64 | 117.4 | 1284.6 |
337
- | mutable | 1.68 | 0.92 | 0.98 | 0.98 | 0.99 |
336
+ | immutable | 1.57 | 1.81 | 7.62 | 103.82 | 1457.89 |
337
+ | mutable | 1.4 | 1.15 | 0.65 | 1.03 | 0.76 |
338
338
 
339
339
  Well written code should not subcribe to whole collections, so just enabling this options most of the times should not break anything. But if it is still needed, you should subscribe to both collection (it may still change e.g. when clearing state) and to its `_changeKey`.
340
340
 
@@ -1,64 +1,64 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createActions = void 0;
4
- const utilsAndConstants_1 = require("./utilsAndConstants");
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createActions = void 0
4
+ const utilsAndConstants_1 = require('./utilsAndConstants')
5
5
  const createActions = (name) => {
6
- const actionPrefix = `@${utilsAndConstants_1.PACKAGE_SHORT_NAME}/${name}/`;
7
- const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`;
8
- const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChanges) => ({
9
- type: updateQueryStateAndEntitiesType,
10
- queryKey,
11
- queryCacheKey,
12
- state,
13
- entityChanges,
14
- });
15
- updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType;
16
- const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`;
17
- const updateMutationStateAndEntities = (mutationKey, state, entityChanges) => ({
18
- type: updateMutationStateAndEntitiesType,
19
- mutationKey,
20
- state,
21
- entityChanges,
22
- });
23
- updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType;
24
- const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`;
25
- const mergeEntityChanges = (changes) => ({
26
- type: mergeEntityChangesType,
27
- changes,
28
- });
29
- mergeEntityChanges.type = mergeEntityChangesType;
30
- const invalidateQueryType = `${actionPrefix}invalidateQuery`;
31
- const invalidateQuery = (queries) => ({
32
- type: invalidateQueryType,
33
- queries,
34
- });
35
- invalidateQuery.type = invalidateQueryType;
36
- const clearQueryStateType = `${actionPrefix}clearQueryState`;
37
- const clearQueryState = (queries) => ({
38
- type: clearQueryStateType,
39
- queries,
40
- });
41
- clearQueryState.type = clearQueryStateType;
42
- const clearMutationStateType = `${actionPrefix}clearMutationState`;
43
- const clearMutationState = (mutationKeys) => ({
44
- type: clearMutationStateType,
45
- mutationKeys,
46
- });
47
- clearMutationState.type = clearMutationStateType;
48
- const clearCacheType = `${actionPrefix}clearCache`;
49
- const clearCache = (stateToKeep) => ({
50
- type: clearCacheType,
51
- stateToKeep,
52
- });
53
- clearCache.type = clearCacheType;
54
- return {
55
- updateQueryStateAndEntities,
56
- updateMutationStateAndEntities,
57
- mergeEntityChanges,
58
- invalidateQuery,
59
- clearQueryState,
60
- clearMutationState,
61
- clearCache,
62
- };
63
- };
64
- exports.createActions = createActions;
6
+ const actionPrefix = `@${utilsAndConstants_1.PACKAGE_SHORT_NAME}/${name}/`
7
+ const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`
8
+ const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChanges) => ({
9
+ type: updateQueryStateAndEntitiesType,
10
+ queryKey,
11
+ queryCacheKey,
12
+ state,
13
+ entityChanges,
14
+ })
15
+ updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType
16
+ const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`
17
+ const updateMutationStateAndEntities = (mutationKey, state, entityChanges) => ({
18
+ type: updateMutationStateAndEntitiesType,
19
+ mutationKey,
20
+ state,
21
+ entityChanges,
22
+ })
23
+ updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType
24
+ const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`
25
+ const mergeEntityChanges = (changes) => ({
26
+ type: mergeEntityChangesType,
27
+ changes,
28
+ })
29
+ mergeEntityChanges.type = mergeEntityChangesType
30
+ const invalidateQueryType = `${actionPrefix}invalidateQuery`
31
+ const invalidateQuery = (queries) => ({
32
+ type: invalidateQueryType,
33
+ queries,
34
+ })
35
+ invalidateQuery.type = invalidateQueryType
36
+ const clearQueryStateType = `${actionPrefix}clearQueryState`
37
+ const clearQueryState = (queries) => ({
38
+ type: clearQueryStateType,
39
+ queries,
40
+ })
41
+ clearQueryState.type = clearQueryStateType
42
+ const clearMutationStateType = `${actionPrefix}clearMutationState`
43
+ const clearMutationState = (mutationKeys) => ({
44
+ type: clearMutationStateType,
45
+ mutationKeys,
46
+ })
47
+ clearMutationState.type = clearMutationStateType
48
+ const clearCacheType = `${actionPrefix}clearCache`
49
+ const clearCache = (stateToKeep) => ({
50
+ type: clearCacheType,
51
+ stateToKeep,
52
+ })
53
+ clearCache.type = clearCacheType
54
+ return {
55
+ updateQueryStateAndEntities,
56
+ updateMutationStateAndEntities,
57
+ mergeEntityChanges,
58
+ invalidateQuery,
59
+ clearQueryState,
60
+ clearMutationState,
61
+ clearCache,
62
+ }
63
+ }
64
+ exports.createActions = createActions
@@ -1,134 +1,219 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createCache = exports.withTypenames = void 0;
4
- const react_1 = require("react");
5
- const createActions_1 = require("./createActions");
6
- const createReducer_1 = require("./createReducer");
7
- const createSelectors_1 = require("./createSelectors");
8
- const mutate_1 = require("./mutate");
9
- const query_1 = require("./query");
10
- const useMutation_1 = require("./useMutation");
11
- const useQuery_1 = require("./useQuery");
12
- const utilsAndConstants_1 = require("./utilsAndConstants");
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createCache = exports.withTypenames = void 0
4
+ const react_1 = require('react')
5
+ const createActions_1 = require('./createActions')
6
+ const createReducer_1 = require('./createReducer')
7
+ const createSelectors_1 = require('./createSelectors')
8
+ const mutate_1 = require('./mutate')
9
+ const query_1 = require('./query')
10
+ const useMutation_1 = require('./useMutation')
11
+ const useQuery_1 = require('./useQuery')
12
+ const utilsAndConstants_1 = require('./utilsAndConstants')
13
13
  const withTypenames = () => {
14
- return {
15
- createCache: (partialCache) => {
16
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
17
- var _p, _q, _r, _s, _t, _u, _v;
18
- const abortControllers = new WeakMap();
19
- (_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {});
20
- (_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0 ? _b : (_p.mutableCollections = false);
21
- (_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0 ? _c : (_q.logsEnabled = false);
22
- (_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0 ? _d : (_r.additionalValidation = utilsAndConstants_1.IS_DEV);
23
- (_e = (_s = partialCache.options).deepComparisonEnabled) !== null && _e !== void 0 ? _e : (_s.deepComparisonEnabled = true);
24
- (_f = partialCache.globals) !== null && _f !== void 0 ? _f : (partialCache.globals = {});
25
- (_g = (_t = partialCache.globals).queries) !== null && _g !== void 0 ? _g : (_t.queries = {});
26
- (_h = (_u = partialCache.globals.queries).fetchPolicy) !== null && _h !== void 0 ? _h : (_u.fetchPolicy = utilsAndConstants_1.FetchPolicy.NoCacheOrExpired);
27
- (_j = (_v = partialCache.globals.queries).skipFetch) !== null && _j !== void 0 ? _j : (_v.skipFetch = false);
28
- (_k = partialCache.cacheStateSelector) !== null && _k !== void 0 ? _k : (partialCache.cacheStateSelector = (state) => state[cache.name]);
29
- (_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {});
30
- (_m = partialCache.queries) !== null && _m !== void 0 ? _m : (partialCache.queries = {});
31
- partialCache.abortControllers = abortControllers;
32
- try {
33
- (_o = partialCache.storeHooks) !== null && _o !== void 0 ? _o : (partialCache.storeHooks = {
34
- useStore: require('react-redux').useStore,
35
- useSelector: require('react-redux').useSelector,
36
- });
37
- }
38
- catch (e) {
39
- throw e;
40
- }
41
- const cache = partialCache;
42
- if (cache.options.deepComparisonEnabled && !utilsAndConstants_1.optionalUtils.deepEqual) {
43
- (0, utilsAndConstants_1.logWarn)('createCache', 'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true');
44
- }
45
- const setDefaultComparer = (target) => {
46
- if ((target === null || target === void 0 ? void 0 : target.selectorComparer) != null && typeof target.selectorComparer === 'object') {
47
- target.selectorComparer = (0, utilsAndConstants_1.createStateComparer)(target.selectorComparer);
48
- }
49
- };
50
- setDefaultComparer(cache.globals.queries);
51
- for (const queryKey in partialCache.queries) {
52
- setDefaultComparer(partialCache.queries[queryKey]);
14
+ return {
15
+ createCache: (partialCache) => {
16
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o
17
+ var _p, _q, _r, _s, _t, _u, _v
18
+ const abortControllers = new WeakMap()
19
+ ;(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {})
20
+ ;(_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0
21
+ ? _b
22
+ : (_p.mutableCollections = false)
23
+ ;(_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0
24
+ ? _c
25
+ : (_q.logsEnabled = false)
26
+ ;(_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0
27
+ ? _d
28
+ : (_r.additionalValidation = utilsAndConstants_1.IS_DEV)
29
+ ;(_e = (_s = partialCache.options).deepComparisonEnabled) !== null && _e !== void 0
30
+ ? _e
31
+ : (_s.deepComparisonEnabled = true)
32
+ ;(_f = partialCache.globals) !== null && _f !== void 0 ? _f : (partialCache.globals = {})
33
+ ;(_g = (_t = partialCache.globals).queries) !== null && _g !== void 0 ? _g : (_t.queries = {})
34
+ ;(_h = (_u = partialCache.globals.queries).fetchPolicy) !== null && _h !== void 0
35
+ ? _h
36
+ : (_u.fetchPolicy = utilsAndConstants_1.FetchPolicy.NoCacheOrExpired)
37
+ ;(_j = (_v = partialCache.globals.queries).skipFetch) !== null && _j !== void 0
38
+ ? _j
39
+ : (_v.skipFetch = false)
40
+ ;(_k = partialCache.cacheStateSelector) !== null && _k !== void 0
41
+ ? _k
42
+ : (partialCache.cacheStateSelector = (state) => state[cache.name])
43
+ ;(_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {})
44
+ ;(_m = partialCache.queries) !== null && _m !== void 0 ? _m : (partialCache.queries = {})
45
+ partialCache.abortControllers = abortControllers
46
+ try {
47
+ ;(_o = partialCache.storeHooks) !== null && _o !== void 0
48
+ ? _o
49
+ : (partialCache.storeHooks = {
50
+ useStore: require('react-redux').useStore,
51
+ useSelector: require('react-redux').useSelector,
52
+ })
53
+ } catch (e) {
54
+ throw e
55
+ }
56
+ const cache = partialCache
57
+ if (cache.options.deepComparisonEnabled && !utilsAndConstants_1.optionalUtils.deepEqual) {
58
+ ;(0, utilsAndConstants_1.logWarn)(
59
+ 'createCache',
60
+ 'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true',
61
+ )
62
+ }
63
+ const setDefaultComparer = (target) => {
64
+ if (
65
+ (target === null || target === void 0 ? void 0 : target.selectorComparer) != null &&
66
+ typeof target.selectorComparer === 'object'
67
+ ) {
68
+ target.selectorComparer = (0, utilsAndConstants_1.createStateComparer)(target.selectorComparer)
69
+ }
70
+ }
71
+ setDefaultComparer(cache.globals.queries)
72
+ for (const queryKey in partialCache.queries) {
73
+ setDefaultComparer(partialCache.queries[queryKey])
74
+ }
75
+ const selectors = Object.assign(
76
+ {selectCacheState: cache.cacheStateSelector},
77
+ (0, createSelectors_1.createSelectors)(cache),
78
+ )
79
+ const {
80
+ selectCacheState,
81
+ selectQueryState,
82
+ selectQueryResult,
83
+ selectQueryLoading,
84
+ selectQueryError,
85
+ selectQueryParams,
86
+ selectQueryExpiresAt,
87
+ selectMutationState,
88
+ selectMutationResult,
89
+ selectMutationLoading,
90
+ selectMutationError,
91
+ selectMutationParams,
92
+ selectEntityById,
93
+ selectEntities,
94
+ selectEntitiesByTypename,
95
+ } = selectors
96
+ const actions = (0, createActions_1.createActions)(cache.name)
97
+ const {
98
+ updateQueryStateAndEntities,
99
+ updateMutationStateAndEntities,
100
+ mergeEntityChanges,
101
+ invalidateQuery,
102
+ clearQueryState,
103
+ clearMutationState,
104
+ clearCache,
105
+ } = actions
106
+ const reducer = (0, createReducer_1.createReducer)(actions, Object.keys(cache.queries), cache.options)
107
+ const createClient = (store) => {
108
+ const client = {
109
+ query: (options) => {
110
+ var _a
111
+ const {query: queryKey, params} = options
112
+ const getCacheKey =
113
+ (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0
114
+ ? _a
115
+ : utilsAndConstants_1.defaultGetCacheKey
116
+ const cacheKey = getCacheKey(params)
117
+ return (0, query_1.query)(
118
+ 'query',
119
+ store,
120
+ cache,
121
+ actions,
122
+ selectors,
123
+ queryKey,
124
+ cacheKey,
125
+ params,
126
+ options.secondsToLive,
127
+ options.onlyIfExpired,
128
+ options.skipFetch,
129
+ options.mergeResults,
130
+ options.onCompleted,
131
+ options.onSuccess,
132
+ options.onError,
133
+ )
134
+ },
135
+ mutate: (options) => {
136
+ return (0, mutate_1.mutate)(
137
+ 'mutate',
138
+ store,
139
+ cache,
140
+ actions,
141
+ selectors,
142
+ options.mutation,
143
+ options.params,
144
+ abortControllers,
145
+ options.onCompleted,
146
+ options.onSuccess,
147
+ options.onError,
148
+ )
149
+ },
150
+ }
151
+ return client
152
+ }
153
+ return {
154
+ cache,
155
+ reducer,
156
+ actions: {
157
+ updateQueryStateAndEntities,
158
+ updateMutationStateAndEntities,
159
+ mergeEntityChanges,
160
+ invalidateQuery,
161
+ clearQueryState,
162
+ clearMutationState,
163
+ clearCache,
164
+ },
165
+ selectors: {
166
+ selectCacheState,
167
+ selectQueryState,
168
+ selectQueryResult,
169
+ selectQueryLoading,
170
+ selectQueryError,
171
+ selectQueryParams,
172
+ selectQueryExpiresAt,
173
+ selectMutationState,
174
+ selectMutationResult,
175
+ selectMutationLoading,
176
+ selectMutationError,
177
+ selectMutationParams,
178
+ selectEntityById,
179
+ selectEntities,
180
+ selectEntitiesByTypename,
181
+ },
182
+ hooks: {
183
+ useClient: () => {
184
+ const store = cache.storeHooks.useStore()
185
+ return (0, react_1.useMemo)(() => createClient(store), [store])
186
+ },
187
+ useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, selectors, options),
188
+ useMutation: (options) =>
189
+ (0, useMutation_1.useMutation)(cache, actions, selectors, options, abortControllers),
190
+ useSelectEntityById: (id, typename) => {
191
+ return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
192
+ },
193
+ useEntitiesByTypename: (typename) => {
194
+ if (cache.options.mutableCollections) {
195
+ cache.storeHooks.useSelector((state) => {
196
+ var _a
197
+ return (_a = selectEntitiesByTypename(state, typename)) === null || _a === void 0
198
+ ? void 0
199
+ : _a._changeKey
200
+ })
53
201
  }
54
- const selectors = Object.assign({ selectCacheState: cache.cacheStateSelector }, (0, createSelectors_1.createSelectors)(cache));
55
- const { selectCacheState, selectQueryState, selectQueryResult, selectQueryLoading, selectQueryError, selectQueryParams, selectQueryExpiresAt, selectMutationState, selectMutationResult, selectMutationLoading, selectMutationError, selectMutationParams, selectEntityById, selectEntities, selectEntitiesByTypename, } = selectors;
56
- const actions = (0, createActions_1.createActions)(cache.name);
57
- const { updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges, invalidateQuery, clearQueryState, clearMutationState, clearCache, } = actions;
58
- const reducer = (0, createReducer_1.createReducer)(actions, Object.keys(cache.queries), cache.options);
59
- const createClient = (store) => {
60
- const client = {
61
- query: (options) => {
62
- var _a;
63
- const { query: queryKey, params } = options;
64
- const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
65
- const cacheKey = getCacheKey(params);
66
- return (0, query_1.query)('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired, options.skipFetch, options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
67
- },
68
- mutate: (options) => {
69
- return (0, mutate_1.mutate)('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers, options.onCompleted, options.onSuccess, options.onError);
70
- },
71
- };
72
- return client;
73
- };
74
- return {
75
- cache,
76
- reducer,
77
- actions: {
78
- updateQueryStateAndEntities,
79
- updateMutationStateAndEntities,
80
- mergeEntityChanges,
81
- invalidateQuery,
82
- clearQueryState,
83
- clearMutationState,
84
- clearCache,
85
- },
86
- selectors: {
87
- selectCacheState,
88
- selectQueryState,
89
- selectQueryResult,
90
- selectQueryLoading,
91
- selectQueryError,
92
- selectQueryParams,
93
- selectQueryExpiresAt,
94
- selectMutationState,
95
- selectMutationResult,
96
- selectMutationLoading,
97
- selectMutationError,
98
- selectMutationParams,
99
- selectEntityById,
100
- selectEntities,
101
- selectEntitiesByTypename,
102
- },
103
- hooks: {
104
- useClient: () => {
105
- const store = cache.storeHooks.useStore();
106
- return (0, react_1.useMemo)(() => createClient(store), [store]);
107
- },
108
- useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, selectors, options),
109
- useMutation: (options) => (0, useMutation_1.useMutation)(cache, actions, selectors, options, abortControllers),
110
- useSelectEntityById: (id, typename) => {
111
- return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename));
112
- },
113
- useEntitiesByTypename: (typename) => {
114
- if (cache.options.mutableCollections) {
115
- cache.storeHooks.useSelector((state) => { var _a; return (_a = selectEntitiesByTypename(state, typename)) === null || _a === void 0 ? void 0 : _a._changeKey; });
116
- }
117
- return cache.storeHooks.useSelector((state) => selectEntitiesByTypename(state, typename));
118
- },
119
- },
120
- utils: {
121
- createClient,
122
- getInitialState: () => {
123
- return reducer(undefined, utilsAndConstants_1.EMPTY_OBJECT);
124
- },
125
- applyEntityChanges: (entities, changes) => {
126
- return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, cache.options);
127
- },
128
- },
129
- };
202
+ return cache.storeHooks.useSelector((state) => selectEntitiesByTypename(state, typename))
203
+ },
204
+ },
205
+ utils: {
206
+ createClient,
207
+ getInitialState: () => {
208
+ return reducer(undefined, utilsAndConstants_1.EMPTY_OBJECT)
209
+ },
210
+ applyEntityChanges: (entities, changes) => {
211
+ return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, cache.options)
212
+ },
130
213
  },
131
- };
132
- };
133
- exports.withTypenames = withTypenames;
134
- exports.createCache = (0, exports.withTypenames)().createCache;
214
+ }
215
+ },
216
+ }
217
+ }
218
+ exports.withTypenames = withTypenames
219
+ exports.createCache = (0, exports.withTypenames)().createCache