@data-client/core 0.13.5 → 0.14.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/dist/index.js +164 -191
- package/dist/index.umd.min.js +1 -1
- package/legacy/actions.js +1 -1
- package/legacy/controller/Controller.js +8 -17
- package/legacy/controller/actions/createExpireAll.js +8 -0
- package/legacy/controller/actions/createFetch.js +27 -0
- package/legacy/controller/actions/createInvalidate.js +10 -0
- package/legacy/controller/actions/createInvalidateAll.js +8 -0
- package/legacy/controller/actions/createMeta.js +9 -0
- package/legacy/controller/actions/createOptimistic.js +17 -0
- package/legacy/controller/actions/createReset.js +8 -0
- package/legacy/controller/actions/createSet.js +17 -0
- package/legacy/controller/actions/createSetResponse.js +26 -0
- package/legacy/controller/actions/createSubscription.js +22 -0
- package/legacy/controller/actions/index.js +11 -0
- package/legacy/index.js +3 -4
- package/legacy/manager/LogoutManager.js +2 -2
- package/legacy/manager/NetworkManager.js +27 -29
- package/legacy/manager/PollingSubscription.js +3 -3
- package/legacy/manager/SubscriptionManager.js +3 -3
- package/legacy/state/reducer/fetchReducer.js +6 -10
- package/legacy/state/reducer/invalidateReducer.js +2 -2
- package/legacy/state/reducer/setReducer.js +3 -3
- package/legacy/state/reducer/setResponseReducer.js +17 -17
- package/legacy/types.js +1 -1
- package/lib/actions.d.ts +34 -40
- package/lib/actions.d.ts.map +1 -1
- package/lib/actions.js +1 -1
- package/lib/controller/Controller.d.ts +1 -1
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +8 -17
- package/lib/controller/actions/createExpireAll.d.ts +3 -0
- package/lib/controller/actions/createExpireAll.d.ts.map +1 -0
- package/lib/controller/actions/createExpireAll.js +8 -0
- package/lib/controller/{createFetch.d.ts → actions/createFetch.d.ts} +3 -3
- package/lib/controller/actions/createFetch.d.ts.map +1 -0
- package/lib/controller/actions/createFetch.js +27 -0
- package/lib/controller/actions/createInvalidate.d.ts +6 -0
- package/lib/controller/actions/createInvalidate.d.ts.map +1 -0
- package/lib/controller/actions/createInvalidate.js +10 -0
- package/lib/controller/actions/createInvalidateAll.d.ts +3 -0
- package/lib/controller/actions/createInvalidateAll.d.ts.map +1 -0
- package/lib/controller/actions/createInvalidateAll.js +8 -0
- package/lib/controller/actions/createMeta.d.ts +3 -0
- package/lib/controller/actions/createMeta.d.ts.map +1 -0
- package/lib/controller/actions/createMeta.js +9 -0
- package/lib/controller/actions/createOptimistic.d.ts +7 -0
- package/lib/controller/actions/createOptimistic.d.ts.map +1 -0
- package/lib/controller/actions/createOptimistic.js +17 -0
- package/lib/controller/actions/createReset.d.ts +3 -0
- package/lib/controller/actions/createReset.d.ts.map +1 -0
- package/lib/controller/actions/createReset.js +8 -0
- package/lib/controller/{createSet.d.ts → actions/createSet.d.ts} +2 -2
- package/lib/controller/actions/createSet.d.ts.map +1 -0
- package/lib/controller/actions/createSet.js +17 -0
- package/lib/controller/{createSetResponse.d.ts → actions/createSetResponse.d.ts} +4 -4
- package/lib/controller/actions/createSetResponse.d.ts.map +1 -0
- package/lib/controller/actions/createSetResponse.js +26 -0
- package/lib/controller/{createSubscription.d.ts → actions/createSubscription.d.ts} +1 -1
- package/lib/controller/actions/createSubscription.d.ts.map +1 -0
- package/lib/controller/actions/createSubscription.js +22 -0
- package/lib/controller/actions/index.d.ts +11 -0
- package/lib/controller/actions/index.d.ts.map +1 -0
- package/lib/controller/actions/index.js +11 -0
- package/lib/index.d.ts +1 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -4
- package/lib/manager/LogoutManager.js +2 -2
- package/lib/manager/NetworkManager.d.ts +2 -2
- package/lib/manager/NetworkManager.d.ts.map +1 -1
- package/lib/manager/NetworkManager.js +27 -29
- package/lib/manager/PollingSubscription.js +3 -3
- package/lib/manager/SubscriptionManager.js +3 -3
- package/lib/state/reducer/expireReducer.d.ts +2 -2
- package/lib/state/reducer/fetchReducer.d.ts.map +1 -1
- package/lib/state/reducer/fetchReducer.js +8 -12
- package/lib/state/reducer/invalidateReducer.d.ts +2 -2
- package/lib/state/reducer/invalidateReducer.js +2 -2
- package/lib/state/reducer/setReducer.d.ts.map +1 -1
- package/lib/state/reducer/setReducer.js +3 -3
- package/lib/state/reducer/setResponseReducer.d.ts.map +1 -1
- package/lib/state/reducer/setResponseReducer.js +17 -17
- package/lib/types.d.ts +3 -3
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +1 -1
- package/package.json +2 -2
- package/src/actions.ts +36 -42
- package/src/controller/Controller.ts +15 -22
- package/src/controller/__tests__/Controller.ts +2 -2
- package/src/controller/actions/createExpireAll.ts +11 -0
- package/src/controller/{createFetch.ts → actions/createFetch.ts} +9 -13
- package/src/controller/actions/createInvalidate.ts +14 -0
- package/src/controller/actions/createInvalidateAll.ts +11 -0
- package/src/controller/actions/createMeta.ts +13 -0
- package/src/controller/actions/createOptimistic.ts +32 -0
- package/src/controller/actions/createReset.ts +9 -0
- package/src/controller/actions/createSet.ts +31 -0
- package/src/controller/{createSetResponse.ts → actions/createSetResponse.ts} +17 -23
- package/src/controller/{createSubscription.ts → actions/createSubscription.ts} +6 -10
- package/src/controller/actions/index.ts +13 -0
- package/src/index.ts +1 -3
- package/src/manager/LogoutManager.ts +1 -1
- package/src/manager/NetworkManager.ts +26 -27
- package/src/manager/PollingSubscription.ts +2 -2
- package/src/manager/SubscriptionManager.ts +2 -2
- package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +4 -10
- package/src/manager/__tests__/logoutManager.ts +1 -1
- package/src/manager/__tests__/networkManager.ts +37 -25
- package/src/manager/__tests__/pollingSubscription.ts +3 -3
- package/src/manager/__tests__/subscriptionManager.ts +22 -27
- package/src/state/__tests__/reducer.ts +42 -45
- package/src/state/reducer/fetchReducer.ts +12 -18
- package/src/state/reducer/invalidateReducer.ts +1 -1
- package/src/state/reducer/setReducer.ts +4 -10
- package/src/state/reducer/setResponseReducer.ts +18 -20
- package/src/types.ts +4 -4
- package/ts3.4/actions.d.ts +35 -41
- package/ts3.4/controller/Controller.d.ts +1 -1
- package/ts3.4/controller/actions/createExpireAll.d.ts +3 -0
- package/ts3.4/controller/{createFetch.d.ts → actions/createFetch.d.ts} +3 -3
- package/ts3.4/controller/{createInvalidate.d.ts → actions/createInvalidate.d.ts} +2 -2
- package/ts3.4/controller/actions/createInvalidateAll.d.ts +3 -0
- package/ts3.4/controller/actions/createMeta.d.ts +3 -0
- package/ts3.4/controller/actions/createOptimistic.d.ts +9 -0
- package/ts3.4/controller/actions/createReset.d.ts +3 -0
- package/ts3.4/controller/{createSet.d.ts → actions/createSet.d.ts} +2 -2
- package/ts3.4/controller/{createSetResponse.d.ts → actions/createSetResponse.d.ts} +4 -4
- package/ts3.4/controller/{createSubscription.d.ts → actions/createSubscription.d.ts} +1 -1
- package/ts3.4/controller/actions/index.d.ts +11 -0
- package/ts3.4/index.d.ts +2 -3
- package/ts3.4/manager/NetworkManager.d.ts +2 -2
- package/ts3.4/state/reducer/expireReducer.d.ts +2 -2
- package/ts3.4/state/reducer/invalidateReducer.d.ts +2 -2
- package/ts3.4/types.d.ts +3 -3
- package/legacy/controller/createExpireAll.js +0 -8
- package/legacy/controller/createFetch.js +0 -31
- package/legacy/controller/createInvalidate.js +0 -12
- package/legacy/controller/createInvalidateAll.js +0 -8
- package/legacy/controller/createOptimistic.js +0 -27
- package/legacy/controller/createReset.js +0 -8
- package/legacy/controller/createSet.js +0 -28
- package/legacy/controller/createSetResponse.js +0 -32
- package/legacy/controller/createSubscription.js +0 -26
- package/lib/controller/createExpireAll.d.ts +0 -3
- package/lib/controller/createExpireAll.d.ts.map +0 -1
- package/lib/controller/createExpireAll.js +0 -8
- package/lib/controller/createFetch.d.ts.map +0 -1
- package/lib/controller/createFetch.js +0 -31
- package/lib/controller/createInvalidate.d.ts +0 -6
- package/lib/controller/createInvalidate.d.ts.map +0 -1
- package/lib/controller/createInvalidate.js +0 -12
- package/lib/controller/createInvalidateAll.d.ts +0 -3
- package/lib/controller/createInvalidateAll.d.ts.map +0 -1
- package/lib/controller/createInvalidateAll.js +0 -8
- package/lib/controller/createOptimistic.d.ts +0 -10
- package/lib/controller/createOptimistic.d.ts.map +0 -1
- package/lib/controller/createOptimistic.js +0 -27
- package/lib/controller/createReset.d.ts +0 -3
- package/lib/controller/createReset.d.ts.map +0 -1
- package/lib/controller/createReset.js +0 -8
- package/lib/controller/createSet.d.ts.map +0 -1
- package/lib/controller/createSet.js +0 -28
- package/lib/controller/createSetResponse.d.ts.map +0 -1
- package/lib/controller/createSetResponse.js +0 -32
- package/lib/controller/createSubscription.d.ts.map +0 -1
- package/lib/controller/createSubscription.js +0 -26
- package/src/controller/createExpireAll.ts +0 -11
- package/src/controller/createInvalidate.ts +0 -16
- package/src/controller/createInvalidateAll.ts +0 -11
- package/src/controller/createOptimistic.ts +0 -41
- package/src/controller/createReset.ts +0 -9
- package/src/controller/createSet.ts +0 -43
- package/ts3.4/controller/createExpireAll.d.ts +0 -3
- package/ts3.4/controller/createInvalidateAll.d.ts +0 -3
- package/ts3.4/controller/createOptimistic.d.ts +0 -12
- package/ts3.4/controller/createReset.d.ts +0 -3
package/dist/index.js
CHANGED
|
@@ -23,6 +23,15 @@ function expireReducer(state, action) {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
function createMeta(expiryLength, fetchedAt) {
|
|
27
|
+
const now = Date.now();
|
|
28
|
+
return {
|
|
29
|
+
fetchedAt: fetchedAt != null ? fetchedAt : now,
|
|
30
|
+
date: now,
|
|
31
|
+
expiresAt: now + expiryLength
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
26
35
|
const FETCH_TYPE = 'rdc/fetch';
|
|
27
36
|
const SET_TYPE = 'rdc/set';
|
|
28
37
|
const SET_RESPONSE_TYPE = 'rdc/setresponse';
|
|
@@ -50,52 +59,37 @@ var actionTypes = /*#__PURE__*/Object.freeze({
|
|
|
50
59
|
GC_TYPE: GC_TYPE
|
|
51
60
|
});
|
|
52
61
|
|
|
53
|
-
function createOptimistic(endpoint, {
|
|
54
|
-
|
|
55
|
-
fetchedAt
|
|
56
|
-
}) {
|
|
57
|
-
var _endpoint$dataExpiryL;
|
|
58
|
-
const expiryLength = (_endpoint$dataExpiryL = endpoint.dataExpiryLength) != null ? _endpoint$dataExpiryL : 60000;
|
|
62
|
+
function createOptimistic(endpoint, args, fetchedAt) {
|
|
63
|
+
var _endpoint$dataExpiryL, _endpoint$dataExpiryL2;
|
|
59
64
|
/* istanbul ignore next */
|
|
60
|
-
if (process.env.NODE_ENV === 'development' &&
|
|
65
|
+
if (process.env.NODE_ENV === 'development' && ((_endpoint$dataExpiryL = endpoint.dataExpiryLength) != null ? _endpoint$dataExpiryL : 0) < 0) {
|
|
61
66
|
throw new Error('Negative expiry length are not allowed.');
|
|
62
67
|
}
|
|
63
|
-
|
|
64
|
-
const meta = {
|
|
65
|
-
args,
|
|
66
|
-
fetchedAt,
|
|
67
|
-
date: now,
|
|
68
|
-
expiresAt: now + expiryLength,
|
|
69
|
-
key: endpoint.key(...args)
|
|
70
|
-
};
|
|
71
|
-
const action = {
|
|
68
|
+
return {
|
|
72
69
|
type: OPTIMISTIC_TYPE,
|
|
73
70
|
endpoint,
|
|
74
|
-
|
|
71
|
+
args,
|
|
72
|
+
key: endpoint.key(...args),
|
|
73
|
+
meta: createMeta((_endpoint$dataExpiryL2 = endpoint.dataExpiryLength) != null ? _endpoint$dataExpiryL2 : 60000, fetchedAt)
|
|
75
74
|
};
|
|
76
|
-
return action;
|
|
77
75
|
}
|
|
78
76
|
|
|
79
77
|
function fetchReducer(state, action) {
|
|
80
|
-
let setAction;
|
|
81
78
|
if (action.endpoint.getOptimisticResponse && action.endpoint.sideEffect) {
|
|
82
|
-
setAction = createOptimistic(action.endpoint,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
const setAction = createOptimistic(action.endpoint, action.args, action.meta.fetchedAt);
|
|
80
|
+
return {
|
|
81
|
+
...state,
|
|
82
|
+
optimistic: [...state.optimistic, setAction]
|
|
83
|
+
};
|
|
86
84
|
} else {
|
|
87
85
|
// If 'fetch' action reaches the reducer there are no middlewares installed to handle it
|
|
88
86
|
/* istanbul ignore next */
|
|
89
87
|
if (process.env.NODE_ENV !== 'production') {
|
|
90
88
|
console.warn('Fetch appears unhandled - you are likely missing the NetworkManager middleware');
|
|
91
|
-
console.warn('See https://dataclient.io/docs/guides/redux
|
|
89
|
+
console.warn('See https://dataclient.io/docs/guides/redux for hooking up redux');
|
|
92
90
|
}
|
|
93
91
|
return state;
|
|
94
92
|
}
|
|
95
|
-
return {
|
|
96
|
-
...state,
|
|
97
|
-
optimistic: [...state.optimistic, setAction]
|
|
98
|
-
};
|
|
99
93
|
}
|
|
100
94
|
|
|
101
95
|
function invalidateReducer(state, action) {
|
|
@@ -116,7 +110,7 @@ function invalidateReducer(state, action) {
|
|
|
116
110
|
meta[key] = itemMeta;
|
|
117
111
|
};
|
|
118
112
|
if (action.type === INVALIDATE_TYPE) {
|
|
119
|
-
invalidateKey(action.
|
|
113
|
+
invalidateKey(action.key);
|
|
120
114
|
} else {
|
|
121
115
|
Object.keys(endpoints).forEach(key => {
|
|
122
116
|
if (action.testKey(key)) {
|
|
@@ -134,7 +128,7 @@ function invalidateReducer(state, action) {
|
|
|
134
128
|
function setReducer(state, action, controller) {
|
|
135
129
|
let value;
|
|
136
130
|
if (typeof action.value === 'function') {
|
|
137
|
-
const previousValue = controller.get(action.schema, ...action.
|
|
131
|
+
const previousValue = controller.get(action.schema, ...action.args, state);
|
|
138
132
|
if (previousValue === undefined) return state;
|
|
139
133
|
value = action.value(previousValue);
|
|
140
134
|
} else {
|
|
@@ -145,7 +139,7 @@ function setReducer(state, action, controller) {
|
|
|
145
139
|
entities,
|
|
146
140
|
indexes,
|
|
147
141
|
entityMeta
|
|
148
|
-
} = normalizr.normalize(
|
|
142
|
+
} = normalizr.normalize(action.schema, value, action.args, state, action.meta);
|
|
149
143
|
return {
|
|
150
144
|
entities,
|
|
151
145
|
indexes,
|
|
@@ -170,19 +164,19 @@ class AbortOptimistic extends Error {}
|
|
|
170
164
|
|
|
171
165
|
function setResponseReducer(state, action, controller) {
|
|
172
166
|
if (action.error) {
|
|
173
|
-
return reduceError(state, action, action.
|
|
167
|
+
return reduceError(state, action, action.response);
|
|
174
168
|
}
|
|
175
169
|
try {
|
|
176
|
-
var _state$meta$action$
|
|
177
|
-
let
|
|
178
|
-
// for true set's
|
|
170
|
+
var _state$meta$action$ke;
|
|
171
|
+
let response;
|
|
172
|
+
// for true set's response is contained in action
|
|
179
173
|
if (action.type === OPTIMISTIC_TYPE) {
|
|
180
174
|
// this should never happen
|
|
181
175
|
/* istanbul ignore if */
|
|
182
176
|
if (!action.endpoint.getOptimisticResponse) return state;
|
|
183
177
|
try {
|
|
184
178
|
// compute optimistic response based on current state
|
|
185
|
-
|
|
179
|
+
response = action.endpoint.getOptimisticResponse.call(action.endpoint, controller.snapshot(state, action.meta.fetchedAt), ...action.args);
|
|
186
180
|
} catch (e) {
|
|
187
181
|
// AbortOptimistic means 'do nothing', otherwise we count the exception as endpoint failure
|
|
188
182
|
if (e.constructor === AbortOptimistic) {
|
|
@@ -191,21 +185,21 @@ function setResponseReducer(state, action, controller) {
|
|
|
191
185
|
throw e;
|
|
192
186
|
}
|
|
193
187
|
} else {
|
|
194
|
-
|
|
188
|
+
response = action.response;
|
|
195
189
|
}
|
|
196
190
|
const {
|
|
197
191
|
result,
|
|
198
192
|
entities,
|
|
199
193
|
indexes,
|
|
200
194
|
entityMeta
|
|
201
|
-
} = normalizr.normalize(
|
|
195
|
+
} = normalizr.normalize(action.endpoint.schema, response, action.args, state, action.meta);
|
|
202
196
|
const endpoints = {
|
|
203
197
|
...state.endpoints,
|
|
204
|
-
[action.
|
|
198
|
+
[action.key]: result
|
|
205
199
|
};
|
|
206
200
|
try {
|
|
207
201
|
if (action.endpoint.update) {
|
|
208
|
-
const updaters = action.endpoint.update(result, ...action.
|
|
202
|
+
const updaters = action.endpoint.update(result, ...action.args);
|
|
209
203
|
Object.keys(updaters).forEach(key => {
|
|
210
204
|
endpoints[key] = updaters[key](endpoints[key]);
|
|
211
205
|
});
|
|
@@ -213,7 +207,7 @@ function setResponseReducer(state, action, controller) {
|
|
|
213
207
|
// no reason to completely fail because of user-code error
|
|
214
208
|
// integrity of this state update is still guaranteed
|
|
215
209
|
} catch (error) {
|
|
216
|
-
console.error(`The following error occured during Endpoint.update() for ${action.
|
|
210
|
+
console.error(`The following error occured during Endpoint.update() for ${action.key}`);
|
|
217
211
|
console.error(error);
|
|
218
212
|
}
|
|
219
213
|
return {
|
|
@@ -223,10 +217,10 @@ function setResponseReducer(state, action, controller) {
|
|
|
223
217
|
entityMeta,
|
|
224
218
|
meta: {
|
|
225
219
|
...state.meta,
|
|
226
|
-
[action.
|
|
220
|
+
[action.key]: {
|
|
227
221
|
date: action.meta.date,
|
|
228
222
|
expiresAt: action.meta.expiresAt,
|
|
229
|
-
prevExpiresAt: (_state$meta$action$
|
|
223
|
+
prevExpiresAt: (_state$meta$action$ke = state.meta[action.key]) == null ? void 0 : _state$meta$action$ke.expiresAt
|
|
230
224
|
}
|
|
231
225
|
},
|
|
232
226
|
optimistic: filterOptimistic(state, action),
|
|
@@ -235,8 +229,8 @@ function setResponseReducer(state, action, controller) {
|
|
|
235
229
|
// reducer must update the state, so in case of processing errors we simply compute the endpoints inline
|
|
236
230
|
} catch (error) {
|
|
237
231
|
if (typeof error === 'object') {
|
|
238
|
-
error.message = `Error processing ${action.
|
|
239
|
-
if ('
|
|
232
|
+
error.message = `Error processing ${action.key}\n\nFull Schema: ${JSON.stringify(action.endpoint.schema, undefined, 2)}\n\nError:\n${error.message}`;
|
|
233
|
+
if ('response' in action) error.response = action.response;
|
|
240
234
|
error.status = 400;
|
|
241
235
|
}
|
|
242
236
|
|
|
@@ -262,7 +256,7 @@ function reduceError(state, action, error) {
|
|
|
262
256
|
...state,
|
|
263
257
|
meta: {
|
|
264
258
|
...state.meta,
|
|
265
|
-
[action.
|
|
259
|
+
[action.key]: {
|
|
266
260
|
date: action.meta.date,
|
|
267
261
|
error,
|
|
268
262
|
expiresAt: action.meta.expiresAt,
|
|
@@ -274,7 +268,7 @@ function reduceError(state, action, error) {
|
|
|
274
268
|
}
|
|
275
269
|
/** Filter all requests with same serialization that did not start after the resolving request */
|
|
276
270
|
function filterOptimistic(state, resolvingAction) {
|
|
277
|
-
return state.optimistic.filter(optimisticAction => optimisticAction.
|
|
271
|
+
return state.optimistic.filter(optimisticAction => optimisticAction.key !== resolvingAction.key || (optimisticAction.type === OPTIMISTIC_TYPE ? optimisticAction.meta.fetchedAt !== resolvingAction.meta.fetchedAt : optimisticAction.meta.date > resolvingAction.meta.date));
|
|
278
272
|
}
|
|
279
273
|
|
|
280
274
|
function createReducer(controller) {
|
|
@@ -335,65 +329,24 @@ var internal = /*#__PURE__*/Object.freeze({
|
|
|
335
329
|
initialState: initialState
|
|
336
330
|
});
|
|
337
331
|
|
|
338
|
-
function
|
|
339
|
-
return {
|
|
340
|
-
type: EXPIREALL_TYPE,
|
|
341
|
-
testKey
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Requesting a fetch to begin
|
|
347
|
-
*/
|
|
348
|
-
function createFetch(endpoint, {
|
|
332
|
+
function createSubscription(endpoint, {
|
|
349
333
|
args
|
|
350
334
|
}) {
|
|
351
|
-
const key = endpoint.key(...args);
|
|
352
|
-
let resolve = 0;
|
|
353
|
-
let reject = 0;
|
|
354
|
-
const promise = new Promise((a, b) => {
|
|
355
|
-
[resolve, reject] = [a, b];
|
|
356
|
-
});
|
|
357
|
-
const meta = {
|
|
358
|
-
args,
|
|
359
|
-
key,
|
|
360
|
-
throttle: !endpoint.sideEffect,
|
|
361
|
-
resolve,
|
|
362
|
-
reject,
|
|
363
|
-
promise,
|
|
364
|
-
createdAt: Date.now(),
|
|
365
|
-
nm: false
|
|
366
|
-
};
|
|
367
335
|
return {
|
|
368
|
-
type:
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
endpoint
|
|
336
|
+
type: SUBSCRIBE_TYPE,
|
|
337
|
+
endpoint,
|
|
338
|
+
args,
|
|
339
|
+
key: endpoint.key(...args)
|
|
372
340
|
};
|
|
373
341
|
}
|
|
374
|
-
|
|
375
|
-
function createInvalidate(endpoint, {
|
|
342
|
+
function createUnsubscription(endpoint, {
|
|
376
343
|
args
|
|
377
344
|
}) {
|
|
378
345
|
return {
|
|
379
|
-
type:
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function createInvalidateAll(testKey) {
|
|
387
|
-
return {
|
|
388
|
-
type: INVALIDATEALL_TYPE,
|
|
389
|
-
testKey
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
function createReset() {
|
|
394
|
-
return {
|
|
395
|
-
type: RESET_TYPE,
|
|
396
|
-
date: Date.now()
|
|
346
|
+
type: UNSUBSCRIBE_TYPE,
|
|
347
|
+
endpoint,
|
|
348
|
+
args,
|
|
349
|
+
key: endpoint.key(...args)
|
|
397
350
|
};
|
|
398
351
|
}
|
|
399
352
|
|
|
@@ -403,87 +356,114 @@ const ensurePojo =
|
|
|
403
356
|
typeof FormData !== 'undefined' ? body => body instanceof FormData ? Object.fromEntries(body.entries()) : body : /* istanbul ignore next */body => body;
|
|
404
357
|
var ensurePojo$1 = ensurePojo;
|
|
405
358
|
|
|
406
|
-
function
|
|
359
|
+
function createSetResponse(endpoint, {
|
|
407
360
|
args,
|
|
408
361
|
fetchedAt,
|
|
409
|
-
|
|
362
|
+
response,
|
|
363
|
+
error = false
|
|
410
364
|
}) {
|
|
411
|
-
|
|
365
|
+
var _endpoint$errorExpiry, _endpoint$dataExpiryL;
|
|
366
|
+
const expiryLength = error ? (_endpoint$errorExpiry = endpoint.errorExpiryLength) != null ? _endpoint$errorExpiry : 1000 : (_endpoint$dataExpiryL = endpoint.dataExpiryLength) != null ? _endpoint$dataExpiryL : 60000;
|
|
412
367
|
/* istanbul ignore next */
|
|
413
368
|
if (process.env.NODE_ENV === 'development' && expiryLength < 0) {
|
|
414
369
|
throw new Error('Negative expiry length are not allowed.');
|
|
415
370
|
}
|
|
416
|
-
|
|
417
|
-
|
|
371
|
+
return {
|
|
372
|
+
type: SET_RESPONSE_TYPE,
|
|
373
|
+
endpoint,
|
|
374
|
+
response,
|
|
418
375
|
args: args.map(ensurePojo$1),
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
};
|
|
423
|
-
const action = {
|
|
424
|
-
type: SET_TYPE,
|
|
425
|
-
value,
|
|
426
|
-
schema,
|
|
427
|
-
meta
|
|
376
|
+
key: endpoint.key(...args),
|
|
377
|
+
meta: createMeta(expiryLength, fetchedAt),
|
|
378
|
+
error
|
|
428
379
|
};
|
|
429
|
-
return action;
|
|
430
380
|
}
|
|
431
381
|
|
|
432
|
-
function
|
|
382
|
+
function createSet(schema, {
|
|
433
383
|
args,
|
|
434
384
|
fetchedAt,
|
|
435
|
-
|
|
436
|
-
error = false
|
|
385
|
+
value
|
|
437
386
|
}) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
throw new Error('Negative expiry length are not allowed.');
|
|
443
|
-
}
|
|
444
|
-
const now = Date.now();
|
|
445
|
-
const meta = {
|
|
387
|
+
return {
|
|
388
|
+
type: SET_TYPE,
|
|
389
|
+
schema,
|
|
390
|
+
value,
|
|
446
391
|
args: args.map(ensurePojo$1),
|
|
447
|
-
|
|
448
|
-
date: now,
|
|
449
|
-
expiresAt: now + expiryLength,
|
|
450
|
-
key: endpoint.key(...args)
|
|
392
|
+
meta: createMeta(60000, fetchedAt)
|
|
451
393
|
};
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function createReset() {
|
|
397
|
+
return {
|
|
398
|
+
type: RESET_TYPE,
|
|
399
|
+
date: Date.now()
|
|
457
400
|
};
|
|
458
|
-
if (error) action.error = true;
|
|
459
|
-
return action;
|
|
460
401
|
}
|
|
461
402
|
|
|
462
|
-
function
|
|
403
|
+
function createInvalidateAll(testKey) {
|
|
404
|
+
return {
|
|
405
|
+
type: INVALIDATEALL_TYPE,
|
|
406
|
+
testKey
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function createInvalidate(endpoint, {
|
|
463
411
|
args
|
|
464
412
|
}) {
|
|
465
413
|
return {
|
|
466
|
-
type:
|
|
467
|
-
endpoint
|
|
468
|
-
meta: {
|
|
469
|
-
args,
|
|
470
|
-
key: endpoint.key(...args)
|
|
471
|
-
}
|
|
414
|
+
type: INVALIDATE_TYPE,
|
|
415
|
+
key: endpoint.key(...args)
|
|
472
416
|
};
|
|
473
417
|
}
|
|
474
|
-
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Requesting a fetch to begin
|
|
421
|
+
*/
|
|
422
|
+
function createFetch(endpoint, {
|
|
475
423
|
args
|
|
476
424
|
}) {
|
|
425
|
+
let resolve = 0;
|
|
426
|
+
let reject = 0;
|
|
427
|
+
const promise = new Promise((a, b) => {
|
|
428
|
+
[resolve, reject] = [a, b];
|
|
429
|
+
});
|
|
430
|
+
const meta = {
|
|
431
|
+
fetchedAt: Date.now(),
|
|
432
|
+
resolve,
|
|
433
|
+
reject,
|
|
434
|
+
promise
|
|
435
|
+
};
|
|
477
436
|
return {
|
|
478
|
-
type:
|
|
437
|
+
type: FETCH_TYPE,
|
|
479
438
|
endpoint,
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
}
|
|
439
|
+
args,
|
|
440
|
+
key: endpoint.key(...args),
|
|
441
|
+
meta
|
|
484
442
|
};
|
|
485
443
|
}
|
|
486
444
|
|
|
445
|
+
function createExpireAll(testKey) {
|
|
446
|
+
return {
|
|
447
|
+
type: EXPIREALL_TYPE,
|
|
448
|
+
testKey
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
453
|
+
__proto__: null,
|
|
454
|
+
createSubscription: createSubscription,
|
|
455
|
+
createUnsubscription: createUnsubscription,
|
|
456
|
+
createSetResponse: createSetResponse,
|
|
457
|
+
createSet: createSet,
|
|
458
|
+
createReset: createReset,
|
|
459
|
+
createOptimistic: createOptimistic,
|
|
460
|
+
createMeta: createMeta,
|
|
461
|
+
createInvalidateAll: createInvalidateAll,
|
|
462
|
+
createInvalidate: createInvalidate,
|
|
463
|
+
createFetch: createFetch,
|
|
464
|
+
createExpireAll: createExpireAll
|
|
465
|
+
});
|
|
466
|
+
|
|
487
467
|
function selectMeta(state, fetchKey) {
|
|
488
468
|
return state.meta[fetchKey];
|
|
489
469
|
}
|
|
@@ -536,7 +516,7 @@ class Controller {
|
|
|
536
516
|
});
|
|
537
517
|
this.dispatch(action);
|
|
538
518
|
if (endpoint.schema) {
|
|
539
|
-
return action.meta.promise.then(input => normalizr.denormalize(
|
|
519
|
+
return action.meta.promise.then(input => normalizr.denormalize(endpoint.schema, input, {}, args));
|
|
540
520
|
}
|
|
541
521
|
return action.meta.promise;
|
|
542
522
|
};
|
|
@@ -603,7 +583,7 @@ class Controller {
|
|
|
603
583
|
return this.dispatch(action);
|
|
604
584
|
};
|
|
605
585
|
/**
|
|
606
|
-
* Resolves an inflight fetch.
|
|
586
|
+
* Resolves an inflight fetch.
|
|
607
587
|
* @see https://dataclient.io/docs/api/Controller#resolve
|
|
608
588
|
*/
|
|
609
589
|
this.resolve = (endpoint, meta) => {
|
|
@@ -691,7 +671,7 @@ class Controller {
|
|
|
691
671
|
const shouldQuery = cacheEndpoints === undefined && schema !== undefined;
|
|
692
672
|
const input = shouldQuery ?
|
|
693
673
|
// nothing in endpoints cache, so try querying if we have a schema to do so
|
|
694
|
-
this.memo.buildQueryKey(
|
|
674
|
+
this.memo.buildQueryKey(schema, args, state.entities, state.indexes, key) : cacheEndpoints;
|
|
695
675
|
if (!isActive) {
|
|
696
676
|
// when not active simply return the query input without denormalizing
|
|
697
677
|
return {
|
|
@@ -716,7 +696,7 @@ class Controller {
|
|
|
716
696
|
const {
|
|
717
697
|
data,
|
|
718
698
|
paths
|
|
719
|
-
} = this.memo.denormalize(
|
|
699
|
+
} = this.memo.denormalize(schema, input, state.entities, args);
|
|
720
700
|
|
|
721
701
|
// note: isInvalid can only be true if shouldQuery is true
|
|
722
702
|
if (!expiresAt && isInvalid) expiresAt = 1;
|
|
@@ -731,10 +711,7 @@ class Controller {
|
|
|
731
711
|
const state = rest[rest.length - 1];
|
|
732
712
|
// this is typescript generics breaking
|
|
733
713
|
const args = rest.slice(0, rest.length - 1).map(ensurePojo$1);
|
|
734
|
-
|
|
735
|
-
// NOTE: different orders can result in cache busting here; but since it's just a perf penalty we will allow for now
|
|
736
|
-
const key = JSON.stringify(args);
|
|
737
|
-
return this.memo.query(key, schema, args, state.entities, state.indexes);
|
|
714
|
+
return this.memo.query(schema, args, state.entities, state.indexes);
|
|
738
715
|
}
|
|
739
716
|
getSchemaResponse(data, paths, entityMeta, expiresAt, invalidIfStale, meta = {}) {
|
|
740
717
|
const invalidDenormalize = typeof data === 'symbol';
|
|
@@ -863,14 +840,14 @@ class NetworkManager {
|
|
|
863
840
|
case SET_RESPONSE_TYPE:
|
|
864
841
|
// only set after new state is computed
|
|
865
842
|
return next(action).then(() => {
|
|
866
|
-
if (action.
|
|
843
|
+
if (action.key in this.fetched) {
|
|
867
844
|
var _controller$getState$;
|
|
868
845
|
// Note: meta *must* be set by reducer so this should be safe
|
|
869
|
-
const error = (_controller$getState$ = controller.getState().meta[action.
|
|
846
|
+
const error = (_controller$getState$ = controller.getState().meta[action.key]) == null ? void 0 : _controller$getState$.error;
|
|
870
847
|
// processing errors result in state meta having error, so we should reject the promise
|
|
871
848
|
if (error) {
|
|
872
849
|
this.handleSet(createSetResponse(action.endpoint, {
|
|
873
|
-
args: action.
|
|
850
|
+
args: action.args,
|
|
874
851
|
response: error,
|
|
875
852
|
fetchedAt: action.meta.fetchedAt,
|
|
876
853
|
error: true
|
|
@@ -904,7 +881,7 @@ class NetworkManager {
|
|
|
904
881
|
/** Used by DevtoolsManager to determine whether to log an action */
|
|
905
882
|
skipLogging(action) {
|
|
906
883
|
/* istanbul ignore next */
|
|
907
|
-
return action.type === FETCH_TYPE && action.
|
|
884
|
+
return action.type === FETCH_TYPE && action.key in this.fetched;
|
|
908
885
|
}
|
|
909
886
|
|
|
910
887
|
/** On mount */
|
|
@@ -948,20 +925,18 @@ class NetworkManager {
|
|
|
948
925
|
* Will then start a promise for a key and potentially start the network
|
|
949
926
|
* fetch.
|
|
950
927
|
*
|
|
951
|
-
* Uses throttle
|
|
928
|
+
* Uses throttle endpoints without sideEffects. This is valuable
|
|
952
929
|
* for ensures mutation requests always go through.
|
|
953
930
|
*/
|
|
954
931
|
handleFetch(action) {
|
|
955
|
-
const fetch = action.payload;
|
|
956
932
|
const {
|
|
957
|
-
key,
|
|
958
|
-
throttle,
|
|
959
933
|
resolve,
|
|
960
934
|
reject,
|
|
961
|
-
|
|
935
|
+
fetchedAt
|
|
962
936
|
} = action.meta;
|
|
937
|
+
const throttle = !action.endpoint.sideEffect;
|
|
963
938
|
const deferedFetch = () => {
|
|
964
|
-
let promise =
|
|
939
|
+
let promise = action.endpoint(...action.args);
|
|
965
940
|
const resolvePromise = promise => promise.then(data => {
|
|
966
941
|
resolve(data);
|
|
967
942
|
return data;
|
|
@@ -975,7 +950,7 @@ class NetworkManager {
|
|
|
975
950
|
if (!throttle) {
|
|
976
951
|
promise = resolvePromise(promise);
|
|
977
952
|
}
|
|
978
|
-
promise = promise.then(
|
|
953
|
+
promise = promise.then(response => {
|
|
979
954
|
let lastReset = this.getLastReset();
|
|
980
955
|
|
|
981
956
|
/* istanbul ignore else */
|
|
@@ -985,22 +960,22 @@ class NetworkManager {
|
|
|
985
960
|
}
|
|
986
961
|
|
|
987
962
|
// don't update state with promises started before last clear
|
|
988
|
-
if (
|
|
963
|
+
if (fetchedAt >= lastReset) {
|
|
989
964
|
this.controller.resolve(action.endpoint, {
|
|
990
|
-
args: action.
|
|
991
|
-
response
|
|
992
|
-
fetchedAt
|
|
965
|
+
args: action.args,
|
|
966
|
+
response,
|
|
967
|
+
fetchedAt
|
|
993
968
|
});
|
|
994
969
|
}
|
|
995
|
-
return
|
|
970
|
+
return response;
|
|
996
971
|
}).catch(error => {
|
|
997
972
|
const lastReset = this.getLastReset();
|
|
998
973
|
// don't update state with promises started before last clear
|
|
999
|
-
if (
|
|
974
|
+
if (fetchedAt >= lastReset) {
|
|
1000
975
|
this.controller.resolve(action.endpoint, {
|
|
1001
|
-
args: action.
|
|
976
|
+
args: action.args,
|
|
1002
977
|
response: error,
|
|
1003
|
-
fetchedAt
|
|
978
|
+
fetchedAt,
|
|
1004
979
|
error: true
|
|
1005
980
|
});
|
|
1006
981
|
}
|
|
@@ -1009,7 +984,7 @@ class NetworkManager {
|
|
|
1009
984
|
return promise;
|
|
1010
985
|
};
|
|
1011
986
|
if (throttle) {
|
|
1012
|
-
return this.throttle(key, deferedFetch,
|
|
987
|
+
return this.throttle(action.key, deferedFetch, fetchedAt).then(data => resolve(data)).catch(error => reject(error));
|
|
1013
988
|
} else {
|
|
1014
989
|
return deferedFetch().catch(() => {});
|
|
1015
990
|
}
|
|
@@ -1021,16 +996,16 @@ class NetworkManager {
|
|
|
1021
996
|
*/
|
|
1022
997
|
handleSet(action) {
|
|
1023
998
|
// this can still turn out to be untrue since this is async
|
|
1024
|
-
if (action.
|
|
999
|
+
if (action.key in this.fetched) {
|
|
1025
1000
|
let promiseHandler;
|
|
1026
1001
|
if (action.error) {
|
|
1027
|
-
promiseHandler = this.rejectors[action.
|
|
1002
|
+
promiseHandler = this.rejectors[action.key];
|
|
1028
1003
|
} else {
|
|
1029
|
-
promiseHandler = this.resolvers[action.
|
|
1004
|
+
promiseHandler = this.resolvers[action.key];
|
|
1030
1005
|
}
|
|
1031
|
-
promiseHandler(action.
|
|
1006
|
+
promiseHandler(action.response);
|
|
1032
1007
|
// since we're resolved we no longer need to keep track of this promise
|
|
1033
|
-
this.clear(action.
|
|
1008
|
+
this.clear(action.key);
|
|
1034
1009
|
}
|
|
1035
1010
|
}
|
|
1036
1011
|
|
|
@@ -1055,7 +1030,7 @@ class NetworkManager {
|
|
|
1055
1030
|
* This ensures promises are resolved only once their data is processed
|
|
1056
1031
|
* by the reducer.
|
|
1057
1032
|
*/
|
|
1058
|
-
throttle(key, fetch,
|
|
1033
|
+
throttle(key, fetch, fetchedAt) {
|
|
1059
1034
|
const lastReset = this.getLastReset();
|
|
1060
1035
|
// we're already fetching so reuse the promise
|
|
1061
1036
|
// fetches after reset do not count
|
|
@@ -1066,7 +1041,7 @@ class NetworkManager {
|
|
|
1066
1041
|
this.resolvers[key] = resolve;
|
|
1067
1042
|
this.rejectors[key] = reject;
|
|
1068
1043
|
});
|
|
1069
|
-
this.fetchedAt[key] =
|
|
1044
|
+
this.fetchedAt[key] = fetchedAt;
|
|
1070
1045
|
this.idleCallback(() => {
|
|
1071
1046
|
// since our real promise is resolved via the wrapReducer(),
|
|
1072
1047
|
// we should just stop all errors here.
|
|
@@ -1178,8 +1153,8 @@ class PollingSubscription {
|
|
|
1178
1153
|
if (action.endpoint.pollFrequency === undefined) throw new Error('frequency needed for polling subscription');
|
|
1179
1154
|
this.endpoint = action.endpoint;
|
|
1180
1155
|
this.frequency = action.endpoint.pollFrequency;
|
|
1181
|
-
this.args = action.
|
|
1182
|
-
this.key = action.
|
|
1156
|
+
this.args = action.args;
|
|
1157
|
+
this.key = action.key;
|
|
1183
1158
|
this.frequencyHistogram.set(this.frequency, 1);
|
|
1184
1159
|
this.controller = controller;
|
|
1185
1160
|
this.connectionListener = connectionListener || new DefaultConnectionListener$1();
|
|
@@ -1341,7 +1316,7 @@ class SubscriptionManager {
|
|
|
1341
1316
|
*
|
|
1342
1317
|
*/
|
|
1343
1318
|
handleSubscribe(action) {
|
|
1344
|
-
const key = action.
|
|
1319
|
+
const key = action.key;
|
|
1345
1320
|
if (key in this.subscriptions) {
|
|
1346
1321
|
const frequency = action.endpoint.pollFrequency;
|
|
1347
1322
|
this.subscriptions[key].add(frequency);
|
|
@@ -1354,7 +1329,7 @@ class SubscriptionManager {
|
|
|
1354
1329
|
*
|
|
1355
1330
|
*/
|
|
1356
1331
|
handleUnsubscribe(action) {
|
|
1357
|
-
const key = action.
|
|
1332
|
+
const key = action.key;
|
|
1358
1333
|
|
|
1359
1334
|
/* istanbul ignore else */
|
|
1360
1335
|
if (key in this.subscriptions) {
|
|
@@ -1537,7 +1512,7 @@ class LogoutManager {
|
|
|
1537
1512
|
if (shouldLogout) this.shouldLogout = shouldLogout;
|
|
1538
1513
|
this.middleware = controller => next => async action => {
|
|
1539
1514
|
await next(action);
|
|
1540
|
-
if (action.type === SET_RESPONSE_TYPE && action.error && this.shouldLogout(action.
|
|
1515
|
+
if (action.type === SET_RESPONSE_TYPE && action.error && this.shouldLogout(action.response)) {
|
|
1541
1516
|
this.handleLogout(controller);
|
|
1542
1517
|
}
|
|
1543
1518
|
};
|
|
@@ -1575,9 +1550,7 @@ exports.ResetError = ResetError;
|
|
|
1575
1550
|
exports.SubscriptionManager = SubscriptionManager;
|
|
1576
1551
|
exports.__INTERNAL__ = internal;
|
|
1577
1552
|
exports.actionTypes = actionTypes;
|
|
1553
|
+
exports.actions = index;
|
|
1578
1554
|
exports.applyManager = applyManager;
|
|
1579
|
-
exports.createFetch = createFetch;
|
|
1580
1555
|
exports.createReducer = createReducer;
|
|
1581
|
-
exports.createSet = createSet;
|
|
1582
|
-
exports.createSetResponse = createSetResponse;
|
|
1583
1556
|
exports.initialState = initialState;
|