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,207 +1,130 @@
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
-
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';
21
10
  export const withTypenames = () => {
22
- return {
23
- createCache: (partialCache) => {
24
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m
25
- var _o, _p, _q, _r, _s, _t
26
- const abortControllers = new WeakMap()
27
- ;(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {})
28
- ;(_b = (_o = partialCache.options).logsEnabled) !== null && _b !== void 0
29
- ? _b
30
- : (_o.logsEnabled = false)
31
- ;(_c = (_p = partialCache.options).additionalValidation) !== null && _c !== void 0
32
- ? _c
33
- : (_p.additionalValidation = IS_DEV)
34
- ;(_d = (_q = partialCache.options).deepComparisonEnabled) !== null && _d !== void 0
35
- ? _d
36
- : (_q.deepComparisonEnabled = true)
37
- ;(_e = partialCache.globals) !== null && _e !== void 0 ? _e : (partialCache.globals = {})
38
- ;(_f = (_r = partialCache.globals).queries) !== null && _f !== void 0 ? _f : (_r.queries = {})
39
- ;(_g = (_s = partialCache.globals.queries).fetchPolicy) !== null && _g !== void 0
40
- ? _g
41
- : (_s.fetchPolicy = FetchPolicy.NoCacheOrExpired)
42
- ;(_h = (_t = partialCache.globals.queries).skipFetch) !== null && _h !== void 0
43
- ? _h
44
- : (_t.skipFetch = false)
45
- ;(_j = partialCache.cacheStateSelector) !== null && _j !== void 0
46
- ? _j
47
- : (partialCache.cacheStateSelector = (state) => state[cache.name])
48
- ;(_k = partialCache.mutations) !== null && _k !== void 0 ? _k : (partialCache.mutations = {})
49
- ;(_l = partialCache.queries) !== null && _l !== void 0 ? _l : (partialCache.queries = {})
50
- partialCache.abortControllers = abortControllers
51
- try {
52
- ;(_m = partialCache.storeHooks) !== null && _m !== void 0
53
- ? _m
54
- : (partialCache.storeHooks = {
55
- useStore: require('react-redux').useStore,
56
- useSelector: require('react-redux').useSelector,
57
- })
58
- } catch (e) {
59
- throw e
60
- }
61
- const cache = partialCache
62
- if (cache.options.deepComparisonEnabled && !optionalUtils.deepEqual) {
63
- logWarn(
64
- 'createCache',
65
- 'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true'
66
- )
67
- }
68
- const setDefaultComparer = (target) => {
69
- if (
70
- (target === null || target === void 0 ? void 0 : target.selectorComparer) != null &&
71
- typeof target.selectorComparer === 'object'
72
- ) {
73
- target.selectorComparer = createStateComparer(target.selectorComparer)
74
- }
75
- }
76
- setDefaultComparer(cache.globals.queries)
77
- for (const queryKey in partialCache.queries) {
78
- setDefaultComparer(partialCache.queries[queryKey])
79
- }
80
- const selectors = Object.assign({selectCacheState: cache.cacheStateSelector}, createSelectors(cache))
81
- const {
82
- selectCacheState,
83
- selectQueryState,
84
- selectQueryResult,
85
- selectQueryLoading,
86
- selectQueryError,
87
- selectQueryParams,
88
- selectQueryExpiresAt,
89
- selectMutationState,
90
- selectMutationResult,
91
- selectMutationLoading,
92
- selectMutationError,
93
- selectMutationParams,
94
- selectEntityById,
95
- selectEntities,
96
- selectEntitiesByTypename,
97
- } = selectors
98
- const actions = createActions(cache.name)
99
- const {
100
- updateQueryStateAndEntities,
101
- updateMutationStateAndEntities,
102
- mergeEntityChanges,
103
- invalidateQuery,
104
- clearQueryState,
105
- clearMutationState,
106
- clearCache,
107
- } = actions
108
- const reducer = createReducer(actions, Object.keys(cache.queries), cache.options)
109
- const createClient = (store) => {
110
- const client = {
111
- query: (options) => {
112
- var _a
113
- const {query: queryKey, params} = options
114
- const getCacheKey =
115
- (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : defaultGetCacheKey
116
- const cacheKey = getCacheKey(params)
117
- return queryImpl(
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 mutateImpl(
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,
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]);
50
+ }
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
+ };
164
127
  },
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 useMemo(() => createClient(store), [store])
186
- },
187
- useQuery: (options) => useQuery(cache, actions, selectors, options),
188
- useMutation: (options) => useMutation(cache, actions, selectors, options, abortControllers),
189
- useSelectEntityById: (id, typename) => {
190
- return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
191
- },
192
- },
193
- utils: {
194
- createClient,
195
- getInitialState: () => {
196
- return reducer(undefined, EMPTY_OBJECT)
197
- },
198
- applyEntityChanges: (entities, changes) => {
199
- return applyEntityChanges(entities, changes, cache.options)
200
- },
201
- },
202
- }
203
- },
204
- }
205
- }
206
-
207
- export const createCache = withTypenames().createCache
128
+ };
129
+ };
130
+ export const createCache = withTypenames().createCache;