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,130 +1,221 @@
1
- import { useMemo } from 'react';
2
- import { createActions } from './createActions';
3
- import { createReducer } from './createReducer';
4
- import { createSelectors } from './createSelectors';
5
- import { mutate as mutateImpl } from './mutate';
6
- import { query as queryImpl } from './query';
7
- import { useMutation } from './useMutation';
8
- import { useQuery } from './useQuery';
9
- import { applyEntityChanges, createStateComparer, defaultGetCacheKey, EMPTY_OBJECT, FetchPolicy, IS_DEV, logWarn, optionalUtils, } from './utilsAndConstants';
1
+ import {useMemo} from 'react'
2
+
3
+ import {createActions} from './createActions'
4
+ import {createReducer} from './createReducer'
5
+ import {createSelectors} from './createSelectors'
6
+ import {mutate as mutateImpl} from './mutate'
7
+ import {query as queryImpl} from './query'
8
+ import {useMutation} from './useMutation'
9
+ import {useQuery} from './useQuery'
10
+ import {
11
+ applyEntityChanges,
12
+ createStateComparer,
13
+ defaultGetCacheKey,
14
+ EMPTY_OBJECT,
15
+ FetchPolicy,
16
+ IS_DEV,
17
+ logWarn,
18
+ optionalUtils,
19
+ } from './utilsAndConstants'
20
+
10
21
  export const withTypenames = () => {
11
- return {
12
- createCache: (partialCache) => {
13
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
14
- var _p, _q, _r, _s, _t, _u, _v;
15
- const abortControllers = new WeakMap();
16
- (_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {});
17
- (_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0 ? _b : (_p.mutableCollections = false);
18
- (_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0 ? _c : (_q.logsEnabled = false);
19
- (_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0 ? _d : (_r.additionalValidation = IS_DEV);
20
- (_e = (_s = partialCache.options).deepComparisonEnabled) !== null && _e !== void 0 ? _e : (_s.deepComparisonEnabled = true);
21
- (_f = partialCache.globals) !== null && _f !== void 0 ? _f : (partialCache.globals = {});
22
- (_g = (_t = partialCache.globals).queries) !== null && _g !== void 0 ? _g : (_t.queries = {});
23
- (_h = (_u = partialCache.globals.queries).fetchPolicy) !== null && _h !== void 0 ? _h : (_u.fetchPolicy = FetchPolicy.NoCacheOrExpired);
24
- (_j = (_v = partialCache.globals.queries).skipFetch) !== null && _j !== void 0 ? _j : (_v.skipFetch = false);
25
- (_k = partialCache.cacheStateSelector) !== null && _k !== void 0 ? _k : (partialCache.cacheStateSelector = (state) => state[cache.name]);
26
- (_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {});
27
- (_m = partialCache.queries) !== null && _m !== void 0 ? _m : (partialCache.queries = {});
28
- partialCache.abortControllers = abortControllers;
29
- try {
30
- (_o = partialCache.storeHooks) !== null && _o !== void 0 ? _o : (partialCache.storeHooks = {
31
- useStore: require('react-redux').useStore,
32
- useSelector: require('react-redux').useSelector,
33
- });
34
- }
35
- catch (e) {
36
- throw e;
37
- }
38
- const cache = partialCache;
39
- if (cache.options.deepComparisonEnabled && !optionalUtils.deepEqual) {
40
- logWarn('createCache', 'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true');
41
- }
42
- const setDefaultComparer = (target) => {
43
- if ((target === null || target === void 0 ? void 0 : target.selectorComparer) != null && typeof target.selectorComparer === 'object') {
44
- target.selectorComparer = createStateComparer(target.selectorComparer);
45
- }
46
- };
47
- setDefaultComparer(cache.globals.queries);
48
- for (const queryKey in partialCache.queries) {
49
- setDefaultComparer(partialCache.queries[queryKey]);
22
+ return {
23
+ createCache: (partialCache) => {
24
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o
25
+ var _p, _q, _r, _s, _t, _u, _v
26
+ const abortControllers = new WeakMap()
27
+ ;(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {})
28
+ ;(_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0
29
+ ? _b
30
+ : (_p.mutableCollections = false)
31
+ ;(_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0
32
+ ? _c
33
+ : (_q.logsEnabled = false)
34
+ ;(_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0
35
+ ? _d
36
+ : (_r.additionalValidation = IS_DEV)
37
+ ;(_e = (_s = partialCache.options).deepComparisonEnabled) !== null && _e !== void 0
38
+ ? _e
39
+ : (_s.deepComparisonEnabled = true)
40
+ ;(_f = partialCache.globals) !== null && _f !== void 0 ? _f : (partialCache.globals = {})
41
+ ;(_g = (_t = partialCache.globals).queries) !== null && _g !== void 0 ? _g : (_t.queries = {})
42
+ ;(_h = (_u = partialCache.globals.queries).fetchPolicy) !== null && _h !== void 0
43
+ ? _h
44
+ : (_u.fetchPolicy = FetchPolicy.NoCacheOrExpired)
45
+ ;(_j = (_v = partialCache.globals.queries).skipFetch) !== null && _j !== void 0
46
+ ? _j
47
+ : (_v.skipFetch = false)
48
+ ;(_k = partialCache.cacheStateSelector) !== null && _k !== void 0
49
+ ? _k
50
+ : (partialCache.cacheStateSelector = (state) => state[cache.name])
51
+ ;(_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {})
52
+ ;(_m = partialCache.queries) !== null && _m !== void 0 ? _m : (partialCache.queries = {})
53
+ partialCache.abortControllers = abortControllers
54
+ try {
55
+ ;(_o = partialCache.storeHooks) !== null && _o !== void 0
56
+ ? _o
57
+ : (partialCache.storeHooks = {
58
+ useStore: require('react-redux').useStore,
59
+ useSelector: require('react-redux').useSelector,
60
+ })
61
+ } catch (e) {
62
+ throw e
63
+ }
64
+ const cache = partialCache
65
+ if (cache.options.deepComparisonEnabled && !optionalUtils.deepEqual) {
66
+ logWarn(
67
+ 'createCache',
68
+ 'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true',
69
+ )
70
+ }
71
+ const setDefaultComparer = (target) => {
72
+ if (
73
+ (target === null || target === void 0 ? void 0 : target.selectorComparer) != null &&
74
+ typeof target.selectorComparer === 'object'
75
+ ) {
76
+ target.selectorComparer = createStateComparer(target.selectorComparer)
77
+ }
78
+ }
79
+ setDefaultComparer(cache.globals.queries)
80
+ for (const queryKey in partialCache.queries) {
81
+ setDefaultComparer(partialCache.queries[queryKey])
82
+ }
83
+ const selectors = Object.assign({selectCacheState: cache.cacheStateSelector}, createSelectors(cache))
84
+ const {
85
+ selectCacheState,
86
+ selectQueryState,
87
+ selectQueryResult,
88
+ selectQueryLoading,
89
+ selectQueryError,
90
+ selectQueryParams,
91
+ selectQueryExpiresAt,
92
+ selectMutationState,
93
+ selectMutationResult,
94
+ selectMutationLoading,
95
+ selectMutationError,
96
+ selectMutationParams,
97
+ selectEntityById,
98
+ selectEntities,
99
+ selectEntitiesByTypename,
100
+ } = selectors
101
+ const actions = createActions(cache.name)
102
+ const {
103
+ updateQueryStateAndEntities,
104
+ updateMutationStateAndEntities,
105
+ mergeEntityChanges,
106
+ invalidateQuery,
107
+ clearQueryState,
108
+ clearMutationState,
109
+ clearCache,
110
+ } = actions
111
+ const reducer = createReducer(actions, Object.keys(cache.queries), cache.options)
112
+ const createClient = (store) => {
113
+ const client = {
114
+ query: (options) => {
115
+ var _a
116
+ const {query: queryKey, params} = options
117
+ const getCacheKey =
118
+ (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : defaultGetCacheKey
119
+ const cacheKey = getCacheKey(params)
120
+ return queryImpl(
121
+ 'query',
122
+ store,
123
+ cache,
124
+ actions,
125
+ selectors,
126
+ queryKey,
127
+ cacheKey,
128
+ params,
129
+ options.secondsToLive,
130
+ options.onlyIfExpired,
131
+ options.skipFetch,
132
+ options.mergeResults,
133
+ options.onCompleted,
134
+ options.onSuccess,
135
+ options.onError,
136
+ )
137
+ },
138
+ mutate: (options) => {
139
+ return mutateImpl(
140
+ 'mutate',
141
+ store,
142
+ cache,
143
+ actions,
144
+ selectors,
145
+ options.mutation,
146
+ options.params,
147
+ abortControllers,
148
+ options.onCompleted,
149
+ options.onSuccess,
150
+ options.onError,
151
+ )
152
+ },
153
+ }
154
+ return client
155
+ }
156
+ return {
157
+ cache,
158
+ reducer,
159
+ actions: {
160
+ updateQueryStateAndEntities,
161
+ updateMutationStateAndEntities,
162
+ mergeEntityChanges,
163
+ invalidateQuery,
164
+ clearQueryState,
165
+ clearMutationState,
166
+ clearCache,
167
+ },
168
+ selectors: {
169
+ selectCacheState,
170
+ selectQueryState,
171
+ selectQueryResult,
172
+ selectQueryLoading,
173
+ selectQueryError,
174
+ selectQueryParams,
175
+ selectQueryExpiresAt,
176
+ selectMutationState,
177
+ selectMutationResult,
178
+ selectMutationLoading,
179
+ selectMutationError,
180
+ selectMutationParams,
181
+ selectEntityById,
182
+ selectEntities,
183
+ selectEntitiesByTypename,
184
+ },
185
+ hooks: {
186
+ useClient: () => {
187
+ const store = cache.storeHooks.useStore()
188
+ return useMemo(() => createClient(store), [store])
189
+ },
190
+ useQuery: (options) => useQuery(cache, actions, selectors, options),
191
+ useMutation: (options) => useMutation(cache, actions, selectors, options, abortControllers),
192
+ useSelectEntityById: (id, typename) => {
193
+ return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
194
+ },
195
+ useEntitiesByTypename: (typename) => {
196
+ if (cache.options.mutableCollections) {
197
+ cache.storeHooks.useSelector((state) => {
198
+ var _a
199
+ return (_a = selectEntitiesByTypename(state, typename)) === null || _a === void 0
200
+ ? void 0
201
+ : _a._changeKey
202
+ })
50
203
  }
51
- const selectors = Object.assign({ selectCacheState: cache.cacheStateSelector }, createSelectors(cache));
52
- const { selectCacheState, selectQueryState, selectQueryResult, selectQueryLoading, selectQueryError, selectQueryParams, selectQueryExpiresAt, selectMutationState, selectMutationResult, selectMutationLoading, selectMutationError, selectMutationParams, selectEntityById, selectEntities, selectEntitiesByTypename, } = selectors;
53
- const actions = createActions(cache.name);
54
- const { updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges, invalidateQuery, clearQueryState, clearMutationState, clearCache, } = actions;
55
- const reducer = createReducer(actions, Object.keys(cache.queries), cache.options);
56
- const createClient = (store) => {
57
- const client = {
58
- query: (options) => {
59
- var _a;
60
- const { query: queryKey, params } = options;
61
- const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (defaultGetCacheKey);
62
- const cacheKey = getCacheKey(params);
63
- return queryImpl('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired, options.skipFetch, options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
64
- },
65
- mutate: (options) => {
66
- return mutateImpl('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers, options.onCompleted, options.onSuccess, options.onError);
67
- },
68
- };
69
- return client;
70
- };
71
- return {
72
- cache,
73
- reducer,
74
- actions: {
75
- updateQueryStateAndEntities,
76
- updateMutationStateAndEntities,
77
- mergeEntityChanges,
78
- invalidateQuery,
79
- clearQueryState,
80
- clearMutationState,
81
- clearCache,
82
- },
83
- selectors: {
84
- selectCacheState,
85
- selectQueryState,
86
- selectQueryResult,
87
- selectQueryLoading,
88
- selectQueryError,
89
- selectQueryParams,
90
- selectQueryExpiresAt,
91
- selectMutationState,
92
- selectMutationResult,
93
- selectMutationLoading,
94
- selectMutationError,
95
- selectMutationParams,
96
- selectEntityById,
97
- selectEntities,
98
- selectEntitiesByTypename,
99
- },
100
- hooks: {
101
- useClient: () => {
102
- const store = cache.storeHooks.useStore();
103
- return useMemo(() => createClient(store), [store]);
104
- },
105
- useQuery: (options) => useQuery(cache, actions, selectors, options),
106
- useMutation: (options) => useMutation(cache, actions, selectors, options, abortControllers),
107
- useSelectEntityById: (id, typename) => {
108
- return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename));
109
- },
110
- useEntitiesByTypename: (typename) => {
111
- if (cache.options.mutableCollections) {
112
- cache.storeHooks.useSelector((state) => { var _a; return (_a = selectEntitiesByTypename(state, typename)) === null || _a === void 0 ? void 0 : _a._changeKey; });
113
- }
114
- return cache.storeHooks.useSelector((state) => selectEntitiesByTypename(state, typename));
115
- },
116
- },
117
- utils: {
118
- createClient,
119
- getInitialState: () => {
120
- return reducer(undefined, EMPTY_OBJECT);
121
- },
122
- applyEntityChanges: (entities, changes) => {
123
- return applyEntityChanges(entities, changes, cache.options);
124
- },
125
- },
126
- };
204
+ return cache.storeHooks.useSelector((state) => selectEntitiesByTypename(state, typename))
205
+ },
206
+ },
207
+ utils: {
208
+ createClient,
209
+ getInitialState: () => {
210
+ return reducer(undefined, EMPTY_OBJECT)
211
+ },
212
+ applyEntityChanges: (entities, changes) => {
213
+ return applyEntityChanges(entities, changes, cache.options)
214
+ },
127
215
  },
128
- };
129
- };
130
- export const createCache = withTypenames().createCache;
216
+ }
217
+ },
218
+ }
219
+ }
220
+
221
+ export const createCache = withTypenames().createCache