react-redux-cache 0.19.2 → 0.19.4-rc.0
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 +25 -7
- package/dist/cjs/createActions.js +65 -0
- package/dist/cjs/createCache.js +208 -0
- package/dist/cjs/createCacheReducer.js +285 -0
- package/dist/cjs/createSelectors.js +68 -0
- package/dist/cjs/index.js +83 -0
- package/dist/cjs/mutate.js +161 -0
- package/dist/cjs/query.js +180 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/useMutation.js +82 -0
- package/dist/cjs/useQuery.js +121 -0
- package/dist/cjs/utilsAndConstants.js +189 -0
- package/dist/esm/createActions.js +61 -0
- package/dist/esm/createCache.js +203 -0
- package/dist/esm/createCacheReducer.js +271 -0
- package/dist/esm/createSelectors.js +64 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/mutate.js +157 -0
- package/dist/esm/query.js +169 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/useMutation.js +88 -0
- package/dist/esm/useQuery.js +125 -0
- package/dist/esm/utilsAndConstants.js +168 -0
- package/dist/types/createActions.d.ts +106 -0
- package/dist/types/createCache.d.ts +712 -0
- package/dist/types/createCacheReducer.d.ts +11 -0
- package/dist/types/createSelectors.d.ts +79 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/mutate.d.ts +94 -0
- package/dist/types/query.d.ts +122 -0
- package/dist/types/types.d.ts +322 -0
- package/dist/types/useMutation.d.ts +39 -0
- package/dist/types/useQuery.d.ts +40 -0
- package/dist/types/utilsAndConstants.d.ts +39 -0
- package/package.json +22 -10
- package/dist/createActions.d.ts +0 -83
- package/dist/createActions.js +0 -64
- package/dist/createCache.d.ts +0 -378
- package/dist/createCache.js +0 -185
- package/dist/createCacheReducer.d.ts +0 -3
- package/dist/createCacheReducer.js +0 -243
- package/dist/createSelectors.d.ts +0 -18
- package/dist/createSelectors.js +0 -61
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -53
- package/dist/mutate.d.ts +0 -4
- package/dist/mutate.js +0 -98
- package/dist/query.d.ts +0 -4
- package/dist/query.js +0 -107
- package/dist/types.d.ts +0 -187
- package/dist/types.js +0 -3
- package/dist/useMutation.d.ts +0 -4
- package/dist/useMutation.js +0 -61
- package/dist/useQuery.d.ts +0 -4
- package/dist/useQuery.js +0 -77
- package/dist/utilsAndConstants.d.ts +0 -20
- package/dist/utilsAndConstants.js +0 -161
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
var __rest =
|
|
2
|
+
(this && this.__rest) ||
|
|
3
|
+
function (s, e) {
|
|
4
|
+
var t = {}
|
|
5
|
+
for (var p in s) {
|
|
6
|
+
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) {
|
|
7
|
+
t[p] = s[p]
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
if (s != null && typeof Object.getOwnPropertySymbols === 'function') {
|
|
11
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
12
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) {
|
|
13
|
+
t[p[i]] = s[p[i]]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return t
|
|
18
|
+
}
|
|
19
|
+
import {applyEntityChanges, EMPTY_OBJECT, isEmptyObject, log, optionalUtils} from './utilsAndConstants'
|
|
20
|
+
|
|
21
|
+
const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
|
|
22
|
+
const optionalMutationKeys = ['error', 'result', 'params', 'loading']
|
|
23
|
+
export const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
24
|
+
const initialState = Object.freeze({
|
|
25
|
+
entities: Object.freeze({}),
|
|
26
|
+
queries: Object.freeze(
|
|
27
|
+
queryKeys.reduce((result, x) => {
|
|
28
|
+
result[x] = Object.freeze({})
|
|
29
|
+
return result
|
|
30
|
+
}, {})
|
|
31
|
+
),
|
|
32
|
+
mutations: Object.freeze({}),
|
|
33
|
+
})
|
|
34
|
+
cacheOptions.logsEnabled &&
|
|
35
|
+
log('createCacheReducer', {
|
|
36
|
+
queryKeys,
|
|
37
|
+
initialState,
|
|
38
|
+
})
|
|
39
|
+
const deepEqual = cacheOptions.deepComparisonEnabled ? optionalUtils.deepEqual : undefined
|
|
40
|
+
return (state = initialState, action) => {
|
|
41
|
+
switch (action.type) {
|
|
42
|
+
case actions.updateQueryStateAndEntities.type: {
|
|
43
|
+
const {queryKey, queryCacheKey, state: queryState, entityChanges} = action
|
|
44
|
+
const oldQueryState = state.queries[queryKey][queryCacheKey]
|
|
45
|
+
let newQueryState = queryState && Object.assign(Object.assign({}, oldQueryState), queryState)
|
|
46
|
+
if (newQueryState) {
|
|
47
|
+
if (oldQueryState && deepEqual) {
|
|
48
|
+
if (
|
|
49
|
+
newQueryState.params !== oldQueryState.params &&
|
|
50
|
+
deepEqual(newQueryState.params, oldQueryState.params)
|
|
51
|
+
) {
|
|
52
|
+
newQueryState.params = oldQueryState.params
|
|
53
|
+
}
|
|
54
|
+
if (
|
|
55
|
+
newQueryState.result !== oldQueryState.result &&
|
|
56
|
+
deepEqual(newQueryState.result, oldQueryState.result)
|
|
57
|
+
) {
|
|
58
|
+
newQueryState.result = oldQueryState.result
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const key of optionalQueryKeys) {
|
|
62
|
+
if (key in newQueryState && newQueryState[key] === undefined) {
|
|
63
|
+
delete newQueryState[key]
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (
|
|
67
|
+
deepEqual === null || deepEqual === void 0
|
|
68
|
+
? void 0
|
|
69
|
+
: deepEqual(
|
|
70
|
+
oldQueryState !== null && oldQueryState !== void 0 ? oldQueryState : EMPTY_OBJECT,
|
|
71
|
+
newQueryState
|
|
72
|
+
)
|
|
73
|
+
) {
|
|
74
|
+
newQueryState = undefined
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const newEntities = entityChanges && applyEntityChanges(state.entities, entityChanges, cacheOptions)
|
|
78
|
+
let newState
|
|
79
|
+
if (newEntities) {
|
|
80
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
81
|
+
newState.entities = newEntities
|
|
82
|
+
}
|
|
83
|
+
if (newQueryState) {
|
|
84
|
+
if (!isEmptyObject(newQueryState)) {
|
|
85
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
86
|
+
newState.queries = Object.assign(Object.assign({}, state.queries), {
|
|
87
|
+
[queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), {
|
|
88
|
+
[queryCacheKey]: newQueryState,
|
|
89
|
+
}),
|
|
90
|
+
})
|
|
91
|
+
} else if (oldQueryState !== undefined) {
|
|
92
|
+
const _a = state.queries[queryKey],
|
|
93
|
+
_b = queryCacheKey,
|
|
94
|
+
_ = _a[_b],
|
|
95
|
+
withoutCacheKey = __rest(_a, [typeof _b === 'symbol' ? _b : _b + ''])
|
|
96
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
97
|
+
newState.queries = Object.assign(Object.assign({}, state.queries), {[queryKey]: withoutCacheKey})
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return newState !== null && newState !== void 0 ? newState : state
|
|
101
|
+
}
|
|
102
|
+
case actions.updateMutationStateAndEntities.type: {
|
|
103
|
+
const {mutationKey, state: mutationState, entityChanges} = action
|
|
104
|
+
const oldMutationState = state.mutations[mutationKey]
|
|
105
|
+
let newMutationState =
|
|
106
|
+
mutationState && Object.assign(Object.assign({}, oldMutationState), mutationState)
|
|
107
|
+
if (newMutationState) {
|
|
108
|
+
if (oldMutationState && deepEqual) {
|
|
109
|
+
if (
|
|
110
|
+
newMutationState.params !== oldMutationState.params &&
|
|
111
|
+
deepEqual(newMutationState.params, oldMutationState.params)
|
|
112
|
+
) {
|
|
113
|
+
newMutationState.params = oldMutationState.params
|
|
114
|
+
}
|
|
115
|
+
if (
|
|
116
|
+
newMutationState.result !== oldMutationState.result &&
|
|
117
|
+
deepEqual(newMutationState.result, oldMutationState.result)
|
|
118
|
+
) {
|
|
119
|
+
newMutationState.result = oldMutationState.result
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
for (const key of optionalMutationKeys) {
|
|
123
|
+
if (key in newMutationState && newMutationState[key] === undefined) {
|
|
124
|
+
delete newMutationState[key]
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (
|
|
128
|
+
deepEqual === null || deepEqual === void 0
|
|
129
|
+
? void 0
|
|
130
|
+
: deepEqual(
|
|
131
|
+
oldMutationState !== null && oldMutationState !== void 0 ? oldMutationState : EMPTY_OBJECT,
|
|
132
|
+
newMutationState
|
|
133
|
+
)
|
|
134
|
+
) {
|
|
135
|
+
newMutationState = undefined
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const newEntities = entityChanges && applyEntityChanges(state.entities, entityChanges, cacheOptions)
|
|
139
|
+
let newState
|
|
140
|
+
if (newEntities) {
|
|
141
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
142
|
+
newState.entities = newEntities
|
|
143
|
+
}
|
|
144
|
+
if (newMutationState) {
|
|
145
|
+
if (!isEmptyObject(newMutationState)) {
|
|
146
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
147
|
+
newState.mutations = Object.assign(Object.assign({}, state.mutations), {
|
|
148
|
+
[mutationKey]: newMutationState,
|
|
149
|
+
})
|
|
150
|
+
} else if (oldMutationState !== undefined) {
|
|
151
|
+
const _c = state.mutations,
|
|
152
|
+
_d = mutationKey,
|
|
153
|
+
_ = _c[_d],
|
|
154
|
+
withoutMutationKey = __rest(_c, [typeof _d === 'symbol' ? _d : _d + ''])
|
|
155
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
156
|
+
newState.mutations = withoutMutationKey
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return newState !== null && newState !== void 0 ? newState : state
|
|
160
|
+
}
|
|
161
|
+
case actions.mergeEntityChanges.type: {
|
|
162
|
+
const {changes} = action
|
|
163
|
+
const newEntities = applyEntityChanges(state.entities, changes, cacheOptions)
|
|
164
|
+
return newEntities ? Object.assign(Object.assign({}, state), {entities: newEntities}) : state
|
|
165
|
+
}
|
|
166
|
+
case actions.invalidateQuery.type: {
|
|
167
|
+
const {queries: queriesToInvalidate} = action
|
|
168
|
+
if (queriesToInvalidate.length === 0) {
|
|
169
|
+
return state
|
|
170
|
+
}
|
|
171
|
+
const now = Date.now()
|
|
172
|
+
let newQueries = undefined
|
|
173
|
+
for (const {query: queryKey, cacheKey, expiresAt = now} of queriesToInvalidate) {
|
|
174
|
+
const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[
|
|
175
|
+
queryKey
|
|
176
|
+
]
|
|
177
|
+
if (cacheKey != null) {
|
|
178
|
+
if (queryStates[cacheKey]) {
|
|
179
|
+
const queryState = queryStates[cacheKey]
|
|
180
|
+
if (queryState && queryState.expiresAt !== expiresAt) {
|
|
181
|
+
newQueries !== null && newQueries !== void 0
|
|
182
|
+
? newQueries
|
|
183
|
+
: (newQueries = Object.assign({}, state.queries))
|
|
184
|
+
if (state.queries[queryKey] === newQueries[queryKey]) {
|
|
185
|
+
newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
|
|
186
|
+
}
|
|
187
|
+
newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {expiresAt})
|
|
188
|
+
if (expiresAt === undefined) {
|
|
189
|
+
delete newQueries[queryKey][cacheKey].expiresAt
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
for (const cacheKey in queryStates) {
|
|
195
|
+
const queryState = queryStates[cacheKey]
|
|
196
|
+
if (queryState && queryState.expiresAt !== expiresAt) {
|
|
197
|
+
newQueries !== null && newQueries !== void 0
|
|
198
|
+
? newQueries
|
|
199
|
+
: (newQueries = Object.assign({}, state.queries))
|
|
200
|
+
if (state.queries[queryKey] === newQueries[queryKey]) {
|
|
201
|
+
newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
|
|
202
|
+
}
|
|
203
|
+
newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {expiresAt})
|
|
204
|
+
if (expiresAt === undefined) {
|
|
205
|
+
delete newQueries[queryKey][cacheKey].expiresAt
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return newQueries === undefined
|
|
212
|
+
? state
|
|
213
|
+
: Object.assign(Object.assign({}, state), {queries: newQueries})
|
|
214
|
+
}
|
|
215
|
+
case actions.clearQueryState.type: {
|
|
216
|
+
const {queries: queriesToClear} = action
|
|
217
|
+
if (queriesToClear.length === 0) {
|
|
218
|
+
return state
|
|
219
|
+
}
|
|
220
|
+
let newQueries = undefined
|
|
221
|
+
for (const {query: queryKey, cacheKey} of queriesToClear) {
|
|
222
|
+
const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[
|
|
223
|
+
queryKey
|
|
224
|
+
]
|
|
225
|
+
if (cacheKey != null) {
|
|
226
|
+
if (queryStates[cacheKey]) {
|
|
227
|
+
newQueries !== null && newQueries !== void 0
|
|
228
|
+
? newQueries
|
|
229
|
+
: (newQueries = Object.assign({}, state.queries))
|
|
230
|
+
if (state.queries[queryKey] === newQueries[queryKey]) {
|
|
231
|
+
newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
|
|
232
|
+
}
|
|
233
|
+
delete newQueries[queryKey][cacheKey]
|
|
234
|
+
}
|
|
235
|
+
} else if (queryStates !== EMPTY_OBJECT) {
|
|
236
|
+
newQueries !== null && newQueries !== void 0
|
|
237
|
+
? newQueries
|
|
238
|
+
: (newQueries = Object.assign({}, state.queries))
|
|
239
|
+
newQueries[queryKey] = EMPTY_OBJECT
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return newQueries === undefined
|
|
243
|
+
? state
|
|
244
|
+
: Object.assign(Object.assign({}, state), {queries: newQueries})
|
|
245
|
+
}
|
|
246
|
+
case actions.clearMutationState.type: {
|
|
247
|
+
const {mutationKeys} = action
|
|
248
|
+
if (mutationKeys.length === 0) {
|
|
249
|
+
return state
|
|
250
|
+
}
|
|
251
|
+
let newMutations = undefined
|
|
252
|
+
for (const mutation of mutationKeys) {
|
|
253
|
+
if (state.mutations[mutation]) {
|
|
254
|
+
newMutations !== null && newMutations !== void 0
|
|
255
|
+
? newMutations
|
|
256
|
+
: (newMutations = Object.assign({}, state.mutations))
|
|
257
|
+
delete newMutations[mutation]
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return newMutations === undefined
|
|
261
|
+
? state
|
|
262
|
+
: Object.assign(Object.assign({}, state), {mutations: newMutations})
|
|
263
|
+
}
|
|
264
|
+
case actions.clearCache.type: {
|
|
265
|
+
const {stateToKeep} = action
|
|
266
|
+
return stateToKeep ? Object.assign(Object.assign({}, initialState), stateToKeep) : initialState
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return state
|
|
270
|
+
}
|
|
271
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {EMPTY_OBJECT} from './utilsAndConstants'
|
|
2
|
+
|
|
3
|
+
export const createSelectors = (cache) => {
|
|
4
|
+
const selectEntityById = (state, id, typename) => {
|
|
5
|
+
var _a
|
|
6
|
+
return id == null
|
|
7
|
+
? undefined
|
|
8
|
+
: (_a = cache.cacheStateSelector(state).entities[typename]) === null || _a === void 0
|
|
9
|
+
? void 0
|
|
10
|
+
: _a[id]
|
|
11
|
+
}
|
|
12
|
+
const selectQueryState = (state, query, cacheKey) => {
|
|
13
|
+
var _a
|
|
14
|
+
return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0
|
|
15
|
+
? _a
|
|
16
|
+
: EMPTY_OBJECT
|
|
17
|
+
}
|
|
18
|
+
const selectMutationState = (state, mutation) => {
|
|
19
|
+
var _a
|
|
20
|
+
return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0
|
|
21
|
+
? _a
|
|
22
|
+
: EMPTY_OBJECT
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
selectQueryState,
|
|
26
|
+
selectQueryResult: (state, query, cacheKey) => {
|
|
27
|
+
return selectQueryState(state, query, cacheKey).result
|
|
28
|
+
},
|
|
29
|
+
selectQueryLoading: (state, query, cacheKey) => {
|
|
30
|
+
var _a
|
|
31
|
+
return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false
|
|
32
|
+
},
|
|
33
|
+
selectQueryError: (state, query, cacheKey) => {
|
|
34
|
+
return selectQueryState(state, query, cacheKey).error
|
|
35
|
+
},
|
|
36
|
+
selectQueryParams: (state, query, cacheKey) => {
|
|
37
|
+
return selectQueryState(state, query, cacheKey).params
|
|
38
|
+
},
|
|
39
|
+
selectQueryExpiresAt: (state, query, cacheKey) => {
|
|
40
|
+
return selectQueryState(state, query, cacheKey).expiresAt
|
|
41
|
+
},
|
|
42
|
+
selectMutationState,
|
|
43
|
+
selectMutationResult: (state, mutation) => {
|
|
44
|
+
return selectMutationState(state, mutation).result
|
|
45
|
+
},
|
|
46
|
+
selectMutationLoading: (state, mutation) => {
|
|
47
|
+
var _a
|
|
48
|
+
return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false
|
|
49
|
+
},
|
|
50
|
+
selectMutationError: (state, mutation) => {
|
|
51
|
+
return selectMutationState(state, mutation).error
|
|
52
|
+
},
|
|
53
|
+
selectMutationParams: (state, mutation) => {
|
|
54
|
+
return selectMutationState(state, mutation).params
|
|
55
|
+
},
|
|
56
|
+
selectEntityById,
|
|
57
|
+
selectEntities: (state) => {
|
|
58
|
+
return cache.cacheStateSelector(state).entities
|
|
59
|
+
},
|
|
60
|
+
selectEntitiesByTypename: (state, typename) => {
|
|
61
|
+
return cache.cacheStateSelector(state).entities[typename]
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
var __awaiter =
|
|
2
|
+
(this && this.__awaiter) ||
|
|
3
|
+
function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) {
|
|
5
|
+
return value instanceof P
|
|
6
|
+
? value
|
|
7
|
+
: new P(function (resolve) {
|
|
8
|
+
resolve(value)
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
12
|
+
function fulfilled(value) {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.next(value))
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function rejected(value) {
|
|
20
|
+
try {
|
|
21
|
+
step(generator['throw'](value))
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function step(result) {
|
|
27
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
28
|
+
}
|
|
29
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
import {log} from './utilsAndConstants'
|
|
33
|
+
|
|
34
|
+
export const mutate = (
|
|
35
|
+
logTag_1,
|
|
36
|
+
store_1,
|
|
37
|
+
cache_1,
|
|
38
|
+
actions_1,
|
|
39
|
+
selectors_1,
|
|
40
|
+
mutationKey_1,
|
|
41
|
+
params_1,
|
|
42
|
+
abortControllers_1,
|
|
43
|
+
...args_1
|
|
44
|
+
) =>
|
|
45
|
+
__awaiter(
|
|
46
|
+
void 0,
|
|
47
|
+
[
|
|
48
|
+
logTag_1,
|
|
49
|
+
store_1,
|
|
50
|
+
cache_1,
|
|
51
|
+
actions_1,
|
|
52
|
+
selectors_1,
|
|
53
|
+
mutationKey_1,
|
|
54
|
+
params_1,
|
|
55
|
+
abortControllers_1,
|
|
56
|
+
...args_1,
|
|
57
|
+
],
|
|
58
|
+
void 0,
|
|
59
|
+
function* (
|
|
60
|
+
logTag,
|
|
61
|
+
store,
|
|
62
|
+
cache,
|
|
63
|
+
actions,
|
|
64
|
+
selectors,
|
|
65
|
+
mutationKey,
|
|
66
|
+
params,
|
|
67
|
+
abortControllers,
|
|
68
|
+
onCompleted = cache.mutations[mutationKey].onCompleted,
|
|
69
|
+
onSuccess = cache.mutations[mutationKey].onSuccess,
|
|
70
|
+
onError = cache.mutations[mutationKey].onError
|
|
71
|
+
) {
|
|
72
|
+
var _a, _b
|
|
73
|
+
const {updateMutationStateAndEntities} = actions
|
|
74
|
+
let abortControllersOfStore = abortControllers.get(store)
|
|
75
|
+
if (abortControllersOfStore === undefined) {
|
|
76
|
+
abortControllersOfStore = {}
|
|
77
|
+
abortControllers.set(store, abortControllersOfStore)
|
|
78
|
+
}
|
|
79
|
+
{
|
|
80
|
+
const abortController = abortControllersOfStore[mutationKey]
|
|
81
|
+
cache.options.logsEnabled &&
|
|
82
|
+
log(logTag, {
|
|
83
|
+
mutationKey,
|
|
84
|
+
params,
|
|
85
|
+
previousAborted: abortController !== undefined,
|
|
86
|
+
})
|
|
87
|
+
if (abortController !== undefined) {
|
|
88
|
+
abortController.abort()
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const abortController = new AbortController()
|
|
92
|
+
abortControllersOfStore[mutationKey] = abortController
|
|
93
|
+
const mutatePromise = cache.mutations[mutationKey].mutation(params, store, abortController.signal)
|
|
94
|
+
store.dispatch(
|
|
95
|
+
updateMutationStateAndEntities(mutationKey, {
|
|
96
|
+
loading: mutatePromise,
|
|
97
|
+
params,
|
|
98
|
+
result: undefined,
|
|
99
|
+
})
|
|
100
|
+
)
|
|
101
|
+
let response
|
|
102
|
+
let error
|
|
103
|
+
try {
|
|
104
|
+
response = yield mutatePromise
|
|
105
|
+
} catch (e) {
|
|
106
|
+
error = e
|
|
107
|
+
}
|
|
108
|
+
cache.options.logsEnabled &&
|
|
109
|
+
log(`${logTag} finished`, {
|
|
110
|
+
response,
|
|
111
|
+
error,
|
|
112
|
+
aborted: abortController.signal.aborted,
|
|
113
|
+
})
|
|
114
|
+
if (abortController.signal.aborted) {
|
|
115
|
+
return ABORTED_RESULT
|
|
116
|
+
}
|
|
117
|
+
delete abortControllersOfStore[mutationKey]
|
|
118
|
+
if (error) {
|
|
119
|
+
store.dispatch(
|
|
120
|
+
updateMutationStateAndEntities(mutationKey, {
|
|
121
|
+
error,
|
|
122
|
+
loading: undefined,
|
|
123
|
+
})
|
|
124
|
+
)
|
|
125
|
+
if (
|
|
126
|
+
!(onError === null || onError === void 0
|
|
127
|
+
? void 0
|
|
128
|
+
: onError(error, params, store, actions, selectors))
|
|
129
|
+
) {
|
|
130
|
+
;(_b = (_a = cache.globals).onError) === null || _b === void 0
|
|
131
|
+
? void 0
|
|
132
|
+
: _b.call(_a, error, mutationKey, params, store, actions, selectors)
|
|
133
|
+
}
|
|
134
|
+
onCompleted === null || onCompleted === void 0
|
|
135
|
+
? void 0
|
|
136
|
+
: onCompleted(response, error, params, store, actions, selectors)
|
|
137
|
+
return {error}
|
|
138
|
+
}
|
|
139
|
+
if (response) {
|
|
140
|
+
const newState = {
|
|
141
|
+
error: undefined,
|
|
142
|
+
loading: undefined,
|
|
143
|
+
result: response.result,
|
|
144
|
+
}
|
|
145
|
+
store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response))
|
|
146
|
+
onSuccess === null || onSuccess === void 0
|
|
147
|
+
? void 0
|
|
148
|
+
: onSuccess(response, params, store, actions, selectors)
|
|
149
|
+
onCompleted === null || onCompleted === void 0
|
|
150
|
+
? void 0
|
|
151
|
+
: onCompleted(response, error, params, store, actions, selectors)
|
|
152
|
+
return {result: response.result}
|
|
153
|
+
}
|
|
154
|
+
throw new Error(`${logTag}: both error and response are not defined`)
|
|
155
|
+
}
|
|
156
|
+
)
|
|
157
|
+
const ABORTED_RESULT = Object.freeze({aborted: true})
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
var __awaiter =
|
|
2
|
+
(this && this.__awaiter) ||
|
|
3
|
+
function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) {
|
|
5
|
+
return value instanceof P
|
|
6
|
+
? value
|
|
7
|
+
: new P(function (resolve) {
|
|
8
|
+
resolve(value)
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
12
|
+
function fulfilled(value) {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.next(value))
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function rejected(value) {
|
|
20
|
+
try {
|
|
21
|
+
step(generator['throw'](value))
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function step(result) {
|
|
27
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
28
|
+
}
|
|
29
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
import {log, NOOP} from './utilsAndConstants'
|
|
33
|
+
|
|
34
|
+
export const query = (
|
|
35
|
+
logTag,
|
|
36
|
+
store,
|
|
37
|
+
cache,
|
|
38
|
+
actions,
|
|
39
|
+
selectors,
|
|
40
|
+
queryKey,
|
|
41
|
+
cacheKey,
|
|
42
|
+
params,
|
|
43
|
+
secondsToLive,
|
|
44
|
+
onlyIfExpired,
|
|
45
|
+
mergeResults,
|
|
46
|
+
onCompleted,
|
|
47
|
+
onSuccess,
|
|
48
|
+
onError
|
|
49
|
+
) =>
|
|
50
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
+
var _a, _b, _c, _d
|
|
52
|
+
if (secondsToLive === void 0) {
|
|
53
|
+
secondsToLive =
|
|
54
|
+
(_a = cache.queries[queryKey].secondsToLive) !== null && _a !== void 0
|
|
55
|
+
? _a
|
|
56
|
+
: cache.globals.queries.secondsToLive
|
|
57
|
+
}
|
|
58
|
+
if (mergeResults === void 0) {
|
|
59
|
+
mergeResults = cache.queries[queryKey].mergeResults
|
|
60
|
+
}
|
|
61
|
+
if (onCompleted === void 0) {
|
|
62
|
+
onCompleted = cache.queries[queryKey].onCompleted
|
|
63
|
+
}
|
|
64
|
+
if (onSuccess === void 0) {
|
|
65
|
+
onSuccess = cache.queries[queryKey].onSuccess
|
|
66
|
+
}
|
|
67
|
+
if (onError === void 0) {
|
|
68
|
+
onError = cache.queries[queryKey].onError
|
|
69
|
+
}
|
|
70
|
+
const {selectQueryResult, selectQueryState} = selectors
|
|
71
|
+
const logsEnabled = cache.options.logsEnabled
|
|
72
|
+
const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
|
|
73
|
+
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
74
|
+
logsEnabled &&
|
|
75
|
+
log(`${logTag} fetch cancelled: already loading`, {
|
|
76
|
+
queryStateOnStart,
|
|
77
|
+
params,
|
|
78
|
+
cacheKey,
|
|
79
|
+
})
|
|
80
|
+
const error = yield queryStateOnStart.loading.then(NOOP).catch(catchAndReturn)
|
|
81
|
+
return error
|
|
82
|
+
? {
|
|
83
|
+
cancelled: 'loading',
|
|
84
|
+
error,
|
|
85
|
+
}
|
|
86
|
+
: {
|
|
87
|
+
cancelled: 'loading',
|
|
88
|
+
result: selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
onlyIfExpired &&
|
|
93
|
+
(queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.expiresAt) !=
|
|
94
|
+
null &&
|
|
95
|
+
queryStateOnStart.expiresAt > Date.now()
|
|
96
|
+
) {
|
|
97
|
+
logsEnabled &&
|
|
98
|
+
log(`${logTag} fetch cancelled: not expired yet`, {
|
|
99
|
+
queryStateOnStart,
|
|
100
|
+
params,
|
|
101
|
+
cacheKey,
|
|
102
|
+
onlyIfExpired,
|
|
103
|
+
})
|
|
104
|
+
return {
|
|
105
|
+
cancelled: 'not-expired',
|
|
106
|
+
result: queryStateOnStart.result,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const {updateQueryStateAndEntities} = actions
|
|
110
|
+
const fetchPromise = cache.queries[queryKey].query(params, store)
|
|
111
|
+
store.dispatch(
|
|
112
|
+
updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
113
|
+
loading: fetchPromise,
|
|
114
|
+
params,
|
|
115
|
+
})
|
|
116
|
+
)
|
|
117
|
+
logsEnabled && log(`${logTag} started`, {queryKey, params, cacheKey, queryStateOnStart, onlyIfExpired})
|
|
118
|
+
let response
|
|
119
|
+
try {
|
|
120
|
+
response = yield fetchPromise
|
|
121
|
+
} catch (error) {
|
|
122
|
+
store.dispatch(
|
|
123
|
+
updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
124
|
+
error,
|
|
125
|
+
loading: undefined,
|
|
126
|
+
})
|
|
127
|
+
)
|
|
128
|
+
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
129
|
+
;(_c = (_b = cache.globals).onError) === null || _c === void 0
|
|
130
|
+
? void 0
|
|
131
|
+
: _c.call(_b, error, queryKey, params, store, actions, selectors)
|
|
132
|
+
}
|
|
133
|
+
onCompleted === null || onCompleted === void 0
|
|
134
|
+
? void 0
|
|
135
|
+
: onCompleted(undefined, error, params, store, actions, selectors)
|
|
136
|
+
return {error}
|
|
137
|
+
}
|
|
138
|
+
const newState = {
|
|
139
|
+
error: undefined,
|
|
140
|
+
loading: undefined,
|
|
141
|
+
expiresAt:
|
|
142
|
+
(_d = response.expiresAt) !== null && _d !== void 0
|
|
143
|
+
? _d
|
|
144
|
+
: secondsToLive != null
|
|
145
|
+
? Date.now() + secondsToLive * 1000
|
|
146
|
+
: undefined,
|
|
147
|
+
result: mergeResults
|
|
148
|
+
? mergeResults(
|
|
149
|
+
selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
150
|
+
response,
|
|
151
|
+
params,
|
|
152
|
+
store,
|
|
153
|
+
actions,
|
|
154
|
+
selectors
|
|
155
|
+
)
|
|
156
|
+
: response.result,
|
|
157
|
+
}
|
|
158
|
+
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response))
|
|
159
|
+
onSuccess === null || onSuccess === void 0
|
|
160
|
+
? void 0
|
|
161
|
+
: onSuccess(response, params, store, actions, selectors)
|
|
162
|
+
onCompleted === null || onCompleted === void 0
|
|
163
|
+
? void 0
|
|
164
|
+
: onCompleted(response, undefined, params, store, actions, selectors)
|
|
165
|
+
return {
|
|
166
|
+
result: newState === null || newState === void 0 ? void 0 : newState.result,
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
const catchAndReturn = (x) => x
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|