react-redux-cache 0.19.3 → 0.19.4
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 +1 -1
- 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 +21 -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 -192
- 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,83 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) {
|
|
7
|
+
k2 = k
|
|
8
|
+
}
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k)
|
|
10
|
+
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get() {
|
|
14
|
+
return m[k]
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
Object.defineProperty(o, k2, desc)
|
|
19
|
+
}
|
|
20
|
+
: function (o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) {
|
|
22
|
+
k2 = k
|
|
23
|
+
}
|
|
24
|
+
o[k2] = m[k]
|
|
25
|
+
})
|
|
26
|
+
var __exportStar =
|
|
27
|
+
(this && this.__exportStar) ||
|
|
28
|
+
function (m, exports) {
|
|
29
|
+
for (var p in m) {
|
|
30
|
+
if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) {
|
|
31
|
+
__createBinding(exports, m, p)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
36
|
+
exports.isEmptyObject =
|
|
37
|
+
exports.FetchPolicy =
|
|
38
|
+
exports.defaultGetCacheKey =
|
|
39
|
+
exports.createStateComparer =
|
|
40
|
+
exports.withTypenames =
|
|
41
|
+
exports.createCache =
|
|
42
|
+
void 0
|
|
43
|
+
var createCache_1 = require('./createCache')
|
|
44
|
+
|
|
45
|
+
Object.defineProperty(exports, 'createCache', {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
get() {
|
|
48
|
+
return createCache_1.createCache
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
Object.defineProperty(exports, 'withTypenames', {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
get() {
|
|
54
|
+
return createCache_1.withTypenames
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
__exportStar(require('./types'), exports)
|
|
58
|
+
var utilsAndConstants_1 = require('./utilsAndConstants')
|
|
59
|
+
|
|
60
|
+
Object.defineProperty(exports, 'createStateComparer', {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get() {
|
|
63
|
+
return utilsAndConstants_1.createStateComparer
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
Object.defineProperty(exports, 'defaultGetCacheKey', {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get() {
|
|
69
|
+
return utilsAndConstants_1.defaultGetCacheKey
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
Object.defineProperty(exports, 'FetchPolicy', {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get() {
|
|
75
|
+
return utilsAndConstants_1.FetchPolicy
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
Object.defineProperty(exports, 'isEmptyObject', {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get() {
|
|
81
|
+
return utilsAndConstants_1.isEmptyObject
|
|
82
|
+
},
|
|
83
|
+
})
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.mutate = void 0
|
|
35
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
36
|
+
|
|
37
|
+
const mutate = (
|
|
38
|
+
logTag_1,
|
|
39
|
+
store_1,
|
|
40
|
+
cache_1,
|
|
41
|
+
actions_1,
|
|
42
|
+
selectors_1,
|
|
43
|
+
mutationKey_1,
|
|
44
|
+
params_1,
|
|
45
|
+
abortControllers_1,
|
|
46
|
+
...args_1
|
|
47
|
+
) =>
|
|
48
|
+
__awaiter(
|
|
49
|
+
void 0,
|
|
50
|
+
[
|
|
51
|
+
logTag_1,
|
|
52
|
+
store_1,
|
|
53
|
+
cache_1,
|
|
54
|
+
actions_1,
|
|
55
|
+
selectors_1,
|
|
56
|
+
mutationKey_1,
|
|
57
|
+
params_1,
|
|
58
|
+
abortControllers_1,
|
|
59
|
+
...args_1,
|
|
60
|
+
],
|
|
61
|
+
void 0,
|
|
62
|
+
function* (
|
|
63
|
+
logTag,
|
|
64
|
+
store,
|
|
65
|
+
cache,
|
|
66
|
+
actions,
|
|
67
|
+
selectors,
|
|
68
|
+
mutationKey,
|
|
69
|
+
params,
|
|
70
|
+
abortControllers,
|
|
71
|
+
onCompleted = cache.mutations[mutationKey].onCompleted,
|
|
72
|
+
onSuccess = cache.mutations[mutationKey].onSuccess,
|
|
73
|
+
onError = cache.mutations[mutationKey].onError
|
|
74
|
+
) {
|
|
75
|
+
var _a, _b
|
|
76
|
+
const {updateMutationStateAndEntities} = actions
|
|
77
|
+
let abortControllersOfStore = abortControllers.get(store)
|
|
78
|
+
if (abortControllersOfStore === undefined) {
|
|
79
|
+
abortControllersOfStore = {}
|
|
80
|
+
abortControllers.set(store, abortControllersOfStore)
|
|
81
|
+
}
|
|
82
|
+
{
|
|
83
|
+
const abortController = abortControllersOfStore[mutationKey]
|
|
84
|
+
cache.options.logsEnabled &&
|
|
85
|
+
(0, utilsAndConstants_1.log)(logTag, {
|
|
86
|
+
mutationKey,
|
|
87
|
+
params,
|
|
88
|
+
previousAborted: abortController !== undefined,
|
|
89
|
+
})
|
|
90
|
+
if (abortController !== undefined) {
|
|
91
|
+
abortController.abort()
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const abortController = new AbortController()
|
|
95
|
+
abortControllersOfStore[mutationKey] = abortController
|
|
96
|
+
const mutatePromise = cache.mutations[mutationKey].mutation(params, store, abortController.signal)
|
|
97
|
+
store.dispatch(
|
|
98
|
+
updateMutationStateAndEntities(mutationKey, {
|
|
99
|
+
loading: mutatePromise,
|
|
100
|
+
params,
|
|
101
|
+
result: undefined,
|
|
102
|
+
})
|
|
103
|
+
)
|
|
104
|
+
let response
|
|
105
|
+
let error
|
|
106
|
+
try {
|
|
107
|
+
response = yield mutatePromise
|
|
108
|
+
} catch (e) {
|
|
109
|
+
error = e
|
|
110
|
+
}
|
|
111
|
+
cache.options.logsEnabled &&
|
|
112
|
+
(0, utilsAndConstants_1.log)(`${logTag} finished`, {
|
|
113
|
+
response,
|
|
114
|
+
error,
|
|
115
|
+
aborted: abortController.signal.aborted,
|
|
116
|
+
})
|
|
117
|
+
if (abortController.signal.aborted) {
|
|
118
|
+
return ABORTED_RESULT
|
|
119
|
+
}
|
|
120
|
+
delete abortControllersOfStore[mutationKey]
|
|
121
|
+
if (error) {
|
|
122
|
+
store.dispatch(
|
|
123
|
+
updateMutationStateAndEntities(mutationKey, {
|
|
124
|
+
error,
|
|
125
|
+
loading: undefined,
|
|
126
|
+
})
|
|
127
|
+
)
|
|
128
|
+
if (
|
|
129
|
+
!(onError === null || onError === void 0
|
|
130
|
+
? void 0
|
|
131
|
+
: onError(error, params, store, actions, selectors))
|
|
132
|
+
) {
|
|
133
|
+
;(_b = (_a = cache.globals).onError) === null || _b === void 0
|
|
134
|
+
? void 0
|
|
135
|
+
: _b.call(_a, error, mutationKey, params, store, actions, selectors)
|
|
136
|
+
}
|
|
137
|
+
onCompleted === null || onCompleted === void 0
|
|
138
|
+
? void 0
|
|
139
|
+
: onCompleted(response, error, params, store, actions, selectors)
|
|
140
|
+
return {error}
|
|
141
|
+
}
|
|
142
|
+
if (response) {
|
|
143
|
+
const newState = {
|
|
144
|
+
error: undefined,
|
|
145
|
+
loading: undefined,
|
|
146
|
+
result: response.result,
|
|
147
|
+
}
|
|
148
|
+
store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response))
|
|
149
|
+
onSuccess === null || onSuccess === void 0
|
|
150
|
+
? void 0
|
|
151
|
+
: onSuccess(response, params, store, actions, selectors)
|
|
152
|
+
onCompleted === null || onCompleted === void 0
|
|
153
|
+
? void 0
|
|
154
|
+
: onCompleted(response, error, params, store, actions, selectors)
|
|
155
|
+
return {result: response.result}
|
|
156
|
+
}
|
|
157
|
+
throw new Error(`${logTag}: both error and response are not defined`)
|
|
158
|
+
}
|
|
159
|
+
)
|
|
160
|
+
exports.mutate = mutate
|
|
161
|
+
const ABORTED_RESULT = Object.freeze({aborted: true})
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.query = void 0
|
|
35
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
36
|
+
|
|
37
|
+
const query = (
|
|
38
|
+
logTag,
|
|
39
|
+
store,
|
|
40
|
+
cache,
|
|
41
|
+
actions,
|
|
42
|
+
selectors,
|
|
43
|
+
queryKey,
|
|
44
|
+
cacheKey,
|
|
45
|
+
params,
|
|
46
|
+
secondsToLive,
|
|
47
|
+
onlyIfExpired,
|
|
48
|
+
mergeResults,
|
|
49
|
+
onCompleted,
|
|
50
|
+
onSuccess,
|
|
51
|
+
onError
|
|
52
|
+
) =>
|
|
53
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
+
var _a, _b, _c, _d
|
|
55
|
+
if (secondsToLive === void 0) {
|
|
56
|
+
secondsToLive =
|
|
57
|
+
(_a = cache.queries[queryKey].secondsToLive) !== null && _a !== void 0
|
|
58
|
+
? _a
|
|
59
|
+
: cache.globals.queries.secondsToLive
|
|
60
|
+
}
|
|
61
|
+
if (mergeResults === void 0) {
|
|
62
|
+
mergeResults = cache.queries[queryKey].mergeResults
|
|
63
|
+
}
|
|
64
|
+
if (onCompleted === void 0) {
|
|
65
|
+
onCompleted = cache.queries[queryKey].onCompleted
|
|
66
|
+
}
|
|
67
|
+
if (onSuccess === void 0) {
|
|
68
|
+
onSuccess = cache.queries[queryKey].onSuccess
|
|
69
|
+
}
|
|
70
|
+
if (onError === void 0) {
|
|
71
|
+
onError = cache.queries[queryKey].onError
|
|
72
|
+
}
|
|
73
|
+
const {selectQueryResult, selectQueryState} = selectors
|
|
74
|
+
const logsEnabled = cache.options.logsEnabled
|
|
75
|
+
const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
|
|
76
|
+
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
77
|
+
logsEnabled &&
|
|
78
|
+
(0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: already loading`, {
|
|
79
|
+
queryStateOnStart,
|
|
80
|
+
params,
|
|
81
|
+
cacheKey,
|
|
82
|
+
})
|
|
83
|
+
const error = yield queryStateOnStart.loading.then(utilsAndConstants_1.NOOP).catch(catchAndReturn)
|
|
84
|
+
return error
|
|
85
|
+
? {
|
|
86
|
+
cancelled: 'loading',
|
|
87
|
+
error,
|
|
88
|
+
}
|
|
89
|
+
: {
|
|
90
|
+
cancelled: 'loading',
|
|
91
|
+
result: selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (
|
|
95
|
+
onlyIfExpired &&
|
|
96
|
+
(queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.expiresAt) !=
|
|
97
|
+
null &&
|
|
98
|
+
queryStateOnStart.expiresAt > Date.now()
|
|
99
|
+
) {
|
|
100
|
+
logsEnabled &&
|
|
101
|
+
(0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: not expired yet`, {
|
|
102
|
+
queryStateOnStart,
|
|
103
|
+
params,
|
|
104
|
+
cacheKey,
|
|
105
|
+
onlyIfExpired,
|
|
106
|
+
})
|
|
107
|
+
return {
|
|
108
|
+
cancelled: 'not-expired',
|
|
109
|
+
result: queryStateOnStart.result,
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const {updateQueryStateAndEntities} = actions
|
|
113
|
+
const fetchPromise = cache.queries[queryKey].query(params, store)
|
|
114
|
+
store.dispatch(
|
|
115
|
+
updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
116
|
+
loading: fetchPromise,
|
|
117
|
+
params,
|
|
118
|
+
})
|
|
119
|
+
)
|
|
120
|
+
logsEnabled &&
|
|
121
|
+
(0, utilsAndConstants_1.log)(`${logTag} started`, {
|
|
122
|
+
queryKey,
|
|
123
|
+
params,
|
|
124
|
+
cacheKey,
|
|
125
|
+
queryStateOnStart,
|
|
126
|
+
onlyIfExpired,
|
|
127
|
+
})
|
|
128
|
+
let response
|
|
129
|
+
try {
|
|
130
|
+
response = yield fetchPromise
|
|
131
|
+
} catch (error) {
|
|
132
|
+
store.dispatch(
|
|
133
|
+
updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
134
|
+
error,
|
|
135
|
+
loading: undefined,
|
|
136
|
+
})
|
|
137
|
+
)
|
|
138
|
+
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
139
|
+
;(_c = (_b = cache.globals).onError) === null || _c === void 0
|
|
140
|
+
? void 0
|
|
141
|
+
: _c.call(_b, error, queryKey, params, store, actions, selectors)
|
|
142
|
+
}
|
|
143
|
+
onCompleted === null || onCompleted === void 0
|
|
144
|
+
? void 0
|
|
145
|
+
: onCompleted(undefined, error, params, store, actions, selectors)
|
|
146
|
+
return {error}
|
|
147
|
+
}
|
|
148
|
+
const newState = {
|
|
149
|
+
error: undefined,
|
|
150
|
+
loading: undefined,
|
|
151
|
+
expiresAt:
|
|
152
|
+
(_d = response.expiresAt) !== null && _d !== void 0
|
|
153
|
+
? _d
|
|
154
|
+
: secondsToLive != null
|
|
155
|
+
? Date.now() + secondsToLive * 1000
|
|
156
|
+
: undefined,
|
|
157
|
+
result: mergeResults
|
|
158
|
+
? mergeResults(
|
|
159
|
+
selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
160
|
+
response,
|
|
161
|
+
params,
|
|
162
|
+
store,
|
|
163
|
+
actions,
|
|
164
|
+
selectors
|
|
165
|
+
)
|
|
166
|
+
: response.result,
|
|
167
|
+
}
|
|
168
|
+
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response))
|
|
169
|
+
onSuccess === null || onSuccess === void 0
|
|
170
|
+
? void 0
|
|
171
|
+
: onSuccess(response, params, store, actions, selectors)
|
|
172
|
+
onCompleted === null || onCompleted === void 0
|
|
173
|
+
? void 0
|
|
174
|
+
: onCompleted(response, undefined, params, store, actions, selectors)
|
|
175
|
+
return {
|
|
176
|
+
result: newState === null || newState === void 0 ? void 0 : newState.result,
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
exports.query = query
|
|
180
|
+
const catchAndReturn = (x) => x
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.useMutation = void 0
|
|
35
|
+
const react_1 = require('react')
|
|
36
|
+
const mutate_1 = require('./mutate')
|
|
37
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
38
|
+
|
|
39
|
+
const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
40
|
+
var _a
|
|
41
|
+
const {mutation: mutationKey, onCompleted, onSuccess, onError} = options
|
|
42
|
+
const store = cache.storeHooks.useStore()
|
|
43
|
+
const [mutationStateSelector, mutate, abort] = (0, react_1.useMemo)(() => {
|
|
44
|
+
return [
|
|
45
|
+
(state) => {
|
|
46
|
+
cache.options.logsEnabled &&
|
|
47
|
+
(0, utilsAndConstants_1.log)('mutationStateSelector', {
|
|
48
|
+
state,
|
|
49
|
+
cacheState: cache.cacheStateSelector(state),
|
|
50
|
+
})
|
|
51
|
+
return cache.cacheStateSelector(state).mutations[mutationKey]
|
|
52
|
+
},
|
|
53
|
+
(params) =>
|
|
54
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
+
return yield (0,
|
|
56
|
+
mutate_1.mutate)('useMutation.mutate', store, cache, actions, selectors, mutationKey, params, abortControllers, onCompleted, onSuccess, onError)
|
|
57
|
+
}),
|
|
58
|
+
() => {
|
|
59
|
+
var _a
|
|
60
|
+
const abortController =
|
|
61
|
+
(_a = abortControllers.get(store)) === null || _a === void 0 ? void 0 : _a[mutationKey]
|
|
62
|
+
if (abortController === undefined || abortController.signal.aborted) {
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
abortController.abort()
|
|
66
|
+
store.dispatch(actions.updateMutationStateAndEntities(mutationKey, {loading: undefined}))
|
|
67
|
+
return true
|
|
68
|
+
},
|
|
69
|
+
]
|
|
70
|
+
}, [mutationKey, store])
|
|
71
|
+
const mutationState =
|
|
72
|
+
(_a = cache.storeHooks.useSelector(mutationStateSelector)) !== null && _a !== void 0
|
|
73
|
+
? _a
|
|
74
|
+
: utilsAndConstants_1.EMPTY_OBJECT
|
|
75
|
+
cache.options.logsEnabled &&
|
|
76
|
+
(0, utilsAndConstants_1.log)('useMutation', {
|
|
77
|
+
options,
|
|
78
|
+
mutationState,
|
|
79
|
+
})
|
|
80
|
+
return [mutate, mutationState, abort]
|
|
81
|
+
}
|
|
82
|
+
exports.useMutation = useMutation
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.useQuery = void 0
|
|
35
|
+
const react_1 = require('react')
|
|
36
|
+
const query_1 = require('./query')
|
|
37
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
38
|
+
|
|
39
|
+
const useQuery = (cache, actions, selectors, options) => {
|
|
40
|
+
var _a, _b, _c, _d, _e
|
|
41
|
+
const {
|
|
42
|
+
query: queryKey,
|
|
43
|
+
skipFetch = false,
|
|
44
|
+
params,
|
|
45
|
+
secondsToLive,
|
|
46
|
+
selectorComparer,
|
|
47
|
+
fetchPolicy = (_a = cache.queries[queryKey].fetchPolicy) !== null && _a !== void 0
|
|
48
|
+
? _a
|
|
49
|
+
: cache.globals.queries.fetchPolicy,
|
|
50
|
+
mergeResults,
|
|
51
|
+
onCompleted,
|
|
52
|
+
onSuccess,
|
|
53
|
+
onError,
|
|
54
|
+
} = options
|
|
55
|
+
const {selectQueryState} = selectors
|
|
56
|
+
const queryInfo = cache.queries[queryKey]
|
|
57
|
+
const logsEnabled = cache.options.logsEnabled
|
|
58
|
+
const getCacheKey =
|
|
59
|
+
(_b = queryInfo.getCacheKey) !== null && _b !== void 0 ? _b : utilsAndConstants_1.defaultGetCacheKey
|
|
60
|
+
const comparer =
|
|
61
|
+
selectorComparer === undefined
|
|
62
|
+
? (_d =
|
|
63
|
+
(_c = queryInfo.selectorComparer) !== null && _c !== void 0
|
|
64
|
+
? _c
|
|
65
|
+
: cache.globals.queries.selectorComparer) !== null && _d !== void 0
|
|
66
|
+
? _d
|
|
67
|
+
: defaultStateComparer
|
|
68
|
+
: typeof selectorComparer === 'function'
|
|
69
|
+
? selectorComparer
|
|
70
|
+
: (0, utilsAndConstants_1.createStateComparer)(selectorComparer)
|
|
71
|
+
const store = cache.storeHooks.useStore()
|
|
72
|
+
const cacheKey = getCacheKey(params)
|
|
73
|
+
const performFetch = (0, react_1.useCallback)(
|
|
74
|
+
(options) =>
|
|
75
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
const paramsPassed = options && 'params' in options
|
|
77
|
+
return yield (0,
|
|
78
|
+
query_1.query)('useQuery.fetch', store, cache, actions, selectors, queryKey, paramsPassed ? getCacheKey(options.params) : cacheKey, paramsPassed ? options.params : params, secondsToLive, options === null || options === void 0 ? void 0 : options.onlyIfExpired, mergeResults, onCompleted, onSuccess, onError)
|
|
79
|
+
}),
|
|
80
|
+
[store, queryKey, cacheKey]
|
|
81
|
+
)
|
|
82
|
+
const queryState =
|
|
83
|
+
(_e = cache.storeHooks.useSelector((state) => {
|
|
84
|
+
return selectQueryState(state, queryKey, cacheKey)
|
|
85
|
+
}, comparer)) !== null && _e !== void 0
|
|
86
|
+
? _e
|
|
87
|
+
: utilsAndConstants_1.EMPTY_OBJECT
|
|
88
|
+
;(0, react_1.useEffect)(() => {
|
|
89
|
+
if (skipFetch) {
|
|
90
|
+
logsEnabled &&
|
|
91
|
+
(0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch', {skipFetch, queryKey, cacheKey})
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
const expired = queryState.expiresAt != null && queryState.expiresAt <= Date.now()
|
|
95
|
+
if (!fetchPolicy(expired, params, queryState, store, selectors)) {
|
|
96
|
+
logsEnabled &&
|
|
97
|
+
(0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch due to fetch policy', {
|
|
98
|
+
queryState,
|
|
99
|
+
expired,
|
|
100
|
+
queryKey,
|
|
101
|
+
cacheKey,
|
|
102
|
+
})
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
performFetch()
|
|
106
|
+
}, [cacheKey, skipFetch])
|
|
107
|
+
logsEnabled &&
|
|
108
|
+
(0, utilsAndConstants_1.log)('useQuery', {
|
|
109
|
+
cacheKey,
|
|
110
|
+
options,
|
|
111
|
+
queryState,
|
|
112
|
+
})
|
|
113
|
+
return [queryState, performFetch]
|
|
114
|
+
}
|
|
115
|
+
exports.useQuery = useQuery
|
|
116
|
+
const defaultStateComparer = (0, utilsAndConstants_1.createStateComparer)([
|
|
117
|
+
'result',
|
|
118
|
+
'loading',
|
|
119
|
+
'params',
|
|
120
|
+
'error',
|
|
121
|
+
])
|