@data-client/core 0.1.0 → 0.2.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.
- package/dist/index.js +134 -409
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +61 -101
- package/legacy/actionTypes.js +2 -2
- package/legacy/controller/Controller.js +276 -6
- package/legacy/controller/createFetch.js +3 -14
- package/legacy/controller/createInvalidate.js +1 -1
- package/legacy/controller/createInvalidateAll.js +1 -1
- package/legacy/controller/createOptimistic.js +1 -7
- package/legacy/controller/createReset.js +1 -1
- package/legacy/controller/createSet.js +31 -0
- package/legacy/controller/createSubscription.js +3 -7
- package/legacy/index.js +2 -5
- package/legacy/internal.js +2 -2
- package/legacy/manager/DevtoolsManager.js +2 -2
- package/legacy/manager/LogoutManager.js +2 -2
- package/legacy/manager/NetworkManager.js +28 -59
- package/legacy/manager/PollingSubscription.js +18 -27
- package/legacy/manager/SubscriptionManager.js +16 -42
- package/legacy/manager/applyManager.js +2 -1
- package/legacy/middlewareTypes.js +1 -1
- package/legacy/newActions.js +1 -1
- package/legacy/next/index.js +3 -3
- package/legacy/state/reducer/createReducer.js +2 -6
- package/legacy/state/reducer/fetchReducer.js +3 -13
- package/legacy/state/reducer/invalidateReducer.js +1 -1
- package/legacy/state/reducer/setReducer.js +9 -16
- package/legacy/types.js +2 -6
- package/lib/actionTypes.d.ts +1 -0
- package/lib/actionTypes.d.ts.map +1 -1
- package/lib/actionTypes.js +2 -2
- package/lib/controller/Controller.d.ts +126 -6
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +276 -6
- package/lib/controller/createFetch.d.ts +2 -2
- package/lib/controller/createFetch.d.ts.map +1 -1
- package/lib/controller/createFetch.js +3 -14
- package/lib/controller/createInvalidate.d.ts +1 -1
- package/lib/controller/createInvalidate.d.ts.map +1 -1
- package/lib/controller/createInvalidate.js +1 -1
- package/lib/controller/createInvalidateAll.d.ts +1 -1
- package/lib/controller/createInvalidateAll.d.ts.map +1 -1
- package/lib/controller/createInvalidateAll.js +1 -1
- package/lib/controller/createOptimistic.d.ts +2 -2
- package/lib/controller/createOptimistic.d.ts.map +1 -1
- package/lib/controller/createOptimistic.js +1 -7
- package/lib/controller/createReset.d.ts +1 -1
- package/lib/controller/createReset.d.ts.map +1 -1
- package/lib/controller/createReset.js +1 -1
- package/lib/controller/{createReceive.d.ts → createSet.d.ts} +6 -6
- package/lib/controller/createSet.d.ts.map +1 -0
- package/lib/controller/createSet.js +31 -0
- package/lib/controller/createSubscription.d.ts +3 -3
- package/lib/controller/createSubscription.d.ts.map +1 -1
- package/lib/controller/createSubscription.js +3 -7
- package/lib/index.d.ts +2 -6
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -5
- package/lib/internal.d.ts +1 -1
- package/lib/internal.d.ts.map +1 -1
- package/lib/internal.js +2 -2
- package/lib/manager/DevtoolsManager.d.ts +1 -1
- package/lib/manager/DevtoolsManager.js +2 -2
- package/lib/manager/LogoutManager.d.ts +4 -5
- package/lib/manager/LogoutManager.d.ts.map +1 -1
- package/lib/manager/LogoutManager.js +2 -2
- package/lib/manager/NetworkManager.d.ts +4 -5
- package/lib/manager/NetworkManager.d.ts.map +1 -1
- package/lib/manager/NetworkManager.js +28 -61
- package/lib/manager/PollingSubscription.d.ts +9 -9
- package/lib/manager/PollingSubscription.d.ts.map +1 -1
- package/lib/manager/PollingSubscription.js +18 -27
- package/lib/manager/SubscriptionManager.d.ts +10 -15
- package/lib/manager/SubscriptionManager.d.ts.map +1 -1
- package/lib/manager/SubscriptionManager.js +16 -42
- package/lib/manager/applyManager.d.ts.map +1 -1
- package/lib/manager/applyManager.js +2 -1
- package/lib/middlewareTypes.d.ts +5 -5
- package/lib/middlewareTypes.d.ts.map +1 -1
- package/lib/middlewareTypes.js +1 -1
- package/lib/newActions.d.ts +26 -18
- package/lib/newActions.d.ts.map +1 -1
- package/lib/newActions.js +1 -1
- package/lib/next/index.d.ts +2 -2
- package/lib/next/index.d.ts.map +1 -1
- package/lib/next/index.js +3 -3
- package/lib/state/reducer/createReducer.d.ts.map +1 -1
- package/lib/state/reducer/createReducer.js +2 -6
- package/lib/state/reducer/fetchReducer.d.ts +1 -2
- package/lib/state/reducer/fetchReducer.d.ts.map +1 -1
- package/lib/state/reducer/fetchReducer.js +3 -14
- package/lib/state/reducer/invalidateReducer.d.ts +3 -4
- package/lib/state/reducer/invalidateReducer.d.ts.map +1 -1
- package/lib/state/reducer/invalidateReducer.js +1 -1
- package/lib/state/reducer/setReducer.d.ts +2 -38
- package/lib/state/reducer/setReducer.d.ts.map +1 -1
- package/lib/state/reducer/setReducer.js +9 -17
- package/lib/types.d.ts +9 -30
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +2 -6
- package/package.json +10 -7
- package/src/actionTypes.ts +1 -1
- package/src/controller/Controller.ts +491 -10
- package/src/controller/createFetch.ts +4 -19
- package/src/controller/createInvalidate.ts +1 -1
- package/src/controller/createInvalidateAll.ts +1 -1
- package/src/controller/createOptimistic.ts +3 -9
- package/src/controller/createReset.ts +1 -1
- package/src/controller/{createReceive.ts → createSet.ts} +9 -17
- package/src/controller/createSubscription.ts +3 -10
- package/src/index.ts +2 -16
- package/src/internal.ts +1 -1
- package/src/manager/DevtoolsManager.ts +1 -1
- package/src/manager/LogoutManager.ts +4 -5
- package/src/manager/NetworkManager.ts +31 -80
- package/src/manager/PollingSubscription.ts +40 -38
- package/src/manager/SubscriptionManager.ts +22 -54
- package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +10 -8
- package/src/manager/__tests__/logoutManager.ts +5 -5
- package/src/manager/__tests__/manager.ts +2 -4
- package/src/manager/__tests__/networkManager.ts +8 -42
- package/src/manager/__tests__/pollingSubscription.ts +259 -151
- package/src/manager/__tests__/subscriptionManager.ts +7 -13
- package/src/manager/applyManager.ts +1 -0
- package/src/middlewareTypes.ts +5 -10
- package/src/newActions.ts +33 -23
- package/src/next/index.ts +2 -2
- package/src/state/__tests__/reducer.ts +59 -297
- package/src/state/reducer/createReducer.ts +3 -11
- package/src/state/reducer/fetchReducer.ts +5 -18
- package/src/state/reducer/invalidateReducer.ts +2 -2
- package/src/state/reducer/setReducer.ts +10 -22
- package/src/types.ts +9 -78
- package/ts3.4/actionTypes.d.ts +1 -0
- package/ts3.4/controller/Controller.d.ts +168 -6
- package/ts3.4/controller/createFetch.d.ts +2 -2
- package/ts3.4/controller/createInvalidate.d.ts +1 -1
- package/ts3.4/controller/createInvalidateAll.d.ts +1 -1
- package/ts3.4/controller/createOptimistic.d.ts +1 -1
- package/ts3.4/controller/createReset.d.ts +1 -1
- package/ts3.4/controller/{createReceive.d.ts → createSet.d.ts} +6 -6
- package/ts3.4/controller/createSubscription.d.ts +3 -3
- package/ts3.4/index.d.ts +2 -7
- package/ts3.4/internal.d.ts +1 -1
- package/ts3.4/manager/DevtoolsManager.d.ts +1 -1
- package/ts3.4/manager/LogoutManager.d.ts +4 -5
- package/ts3.4/manager/NetworkManager.d.ts +4 -5
- package/ts3.4/manager/PollingSubscription.d.ts +9 -9
- package/ts3.4/manager/SubscriptionManager.d.ts +10 -15
- package/ts3.4/middlewareTypes.d.ts +5 -5
- package/ts3.4/newActions.d.ts +26 -18
- package/ts3.4/next/index.d.ts +2 -2
- package/ts3.4/state/reducer/fetchReducer.d.ts +1 -2
- package/ts3.4/state/reducer/invalidateReducer.d.ts +3 -4
- package/ts3.4/state/reducer/setReducer.d.ts +2 -38
- package/ts3.4/types.d.ts +8 -30
- package/legacy/compatibleActions.js +0 -2
- package/legacy/controller/BaseController.js +0 -289
- package/legacy/controller/createReceive.js +0 -36
- package/legacy/endpoint/index.js +0 -2
- package/legacy/endpoint/shapes.js +0 -2
- package/legacy/endpoint/types.js +0 -2
- package/legacy/legacyActions.js +0 -2
- package/legacy/next/Controller.js +0 -24
- package/legacy/previousActions.js +0 -2
- package/legacy/state/applyUpdatersToResults.js +0 -4
- package/legacy/state/legacy-actions/createFetch.js +0 -62
- package/legacy/state/legacy-actions/createReceive.js +0 -37
- package/legacy/state/legacy-actions/createReceiveError.js +0 -28
- package/legacy/state/legacy-actions/index.js +0 -4
- package/legacy/state/reducerInstance.js +0 -9
- package/lib/compatibleActions.d.ts +0 -47
- package/lib/compatibleActions.d.ts.map +0 -1
- package/lib/compatibleActions.js +0 -2
- package/lib/controller/BaseController.d.ts +0 -128
- package/lib/controller/BaseController.d.ts.map +0 -1
- package/lib/controller/BaseController.js +0 -289
- package/lib/controller/createReceive.d.ts.map +0 -1
- package/lib/controller/createReceive.js +0 -36
- package/lib/endpoint/index.d.ts +0 -3
- package/lib/endpoint/index.d.ts.map +0 -1
- package/lib/endpoint/index.js +0 -2
- package/lib/endpoint/shapes.d.ts +0 -25
- package/lib/endpoint/shapes.d.ts.map +0 -1
- package/lib/endpoint/shapes.js +0 -2
- package/lib/endpoint/types.d.ts +0 -45
- package/lib/endpoint/types.d.ts.map +0 -1
- package/lib/endpoint/types.js +0 -2
- package/lib/legacyActions.d.ts +0 -92
- package/lib/legacyActions.d.ts.map +0 -1
- package/lib/legacyActions.js +0 -2
- package/lib/next/Controller.d.ts +0 -14
- package/lib/next/Controller.d.ts.map +0 -1
- package/lib/next/Controller.js +0 -24
- package/lib/previousActions.d.ts +0 -91
- package/lib/previousActions.d.ts.map +0 -1
- package/lib/previousActions.js +0 -2
- package/lib/state/applyUpdatersToResults.d.ts +0 -13
- package/lib/state/applyUpdatersToResults.d.ts.map +0 -1
- package/lib/state/applyUpdatersToResults.js +0 -7
- package/lib/state/legacy-actions/createFetch.d.ts +0 -19
- package/lib/state/legacy-actions/createFetch.d.ts.map +0 -1
- package/lib/state/legacy-actions/createFetch.js +0 -62
- package/lib/state/legacy-actions/createReceive.d.ts +0 -14
- package/lib/state/legacy-actions/createReceive.d.ts.map +0 -1
- package/lib/state/legacy-actions/createReceive.js +0 -37
- package/lib/state/legacy-actions/createReceiveError.d.ts +0 -9
- package/lib/state/legacy-actions/createReceiveError.d.ts.map +0 -1
- package/lib/state/legacy-actions/createReceiveError.js +0 -28
- package/lib/state/legacy-actions/index.d.ts +0 -4
- package/lib/state/legacy-actions/index.d.ts.map +0 -1
- package/lib/state/legacy-actions/index.js +0 -4
- package/lib/state/reducerInstance.d.ts +0 -7
- package/lib/state/reducerInstance.d.ts.map +0 -1
- package/lib/state/reducerInstance.js +0 -9
- package/src/compatibleActions.ts +0 -96
- package/src/controller/BaseController.ts +0 -508
- package/src/endpoint/index.ts +0 -14
- package/src/endpoint/shapes.ts +0 -53
- package/src/endpoint/types.ts +0 -72
- package/src/legacyActions.ts +0 -163
- package/src/manager/__tests__/__snapshots__/pollingSubscription-endpoint.ts.snap +0 -49
- package/src/manager/__tests__/networkManager-legacy.ts +0 -394
- package/src/manager/__tests__/pollingSubscription-endpoint.ts +0 -423
- package/src/next/Controller.ts +0 -39
- package/src/previousActions.ts +0 -159
- package/src/state/__tests__/applyUpdatersToResults.ts +0 -40
- package/src/state/applyUpdatersToResults.ts +0 -29
- package/src/state/legacy-actions/createFetch.ts +0 -95
- package/src/state/legacy-actions/createReceive.ts +0 -68
- package/src/state/legacy-actions/createReceiveError.ts +0 -43
- package/src/state/legacy-actions/index.ts +0 -3
- package/src/state/reducerInstance.ts +0 -14
- package/ts3.4/compatibleActions.d.ts +0 -47
- package/ts3.4/controller/BaseController.d.ts +0 -170
- package/ts3.4/endpoint/index.d.ts +0 -3
- package/ts3.4/endpoint/shapes.d.ts +0 -25
- package/ts3.4/endpoint/types.d.ts +0 -45
- package/ts3.4/legacyActions.d.ts +0 -95
- package/ts3.4/next/Controller.d.ts +0 -14
- package/ts3.4/previousActions.d.ts +0 -94
- package/ts3.4/state/applyUpdatersToResults.d.ts +0 -13
- package/ts3.4/state/legacy-actions/createFetch.d.ts +0 -19
- package/ts3.4/state/legacy-actions/createReceive.d.ts +0 -14
- package/ts3.4/state/legacy-actions/createReceiveError.d.ts +0 -9
- package/ts3.4/state/legacy-actions/index.d.ts +0 -4
- package/ts3.4/state/reducerInstance.d.ts +0 -7
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ const RIC = typeof requestIdleCallback === 'function' ? requestIdleCallback : cb
|
|
|
8
8
|
var RIC$1 = RIC;
|
|
9
9
|
|
|
10
10
|
const FETCH_TYPE = 'rest-hooks/fetch';
|
|
11
|
-
|
|
11
|
+
/** @deprecated use SET_TYPE instead */
|
|
12
12
|
const RECEIVE_TYPE = 'rest-hooks/receive';
|
|
13
13
|
const SET_TYPE = RECEIVE_TYPE;
|
|
14
14
|
const OPTIMISTIC_TYPE = 'rest-hooks/optimistic';
|
|
@@ -44,19 +44,13 @@ function createOptimistic(endpoint, {
|
|
|
44
44
|
throw new Error('Negative expiry length are not allowed.');
|
|
45
45
|
}
|
|
46
46
|
const now = Date.now();
|
|
47
|
-
// TODO: Use correct type once we no longer need backcompat
|
|
48
47
|
const meta = {
|
|
49
48
|
args,
|
|
50
49
|
fetchedAt,
|
|
51
50
|
date: now,
|
|
52
51
|
expiresAt: now + expiryLength,
|
|
53
|
-
// For legacy support; TODO: remove
|
|
54
|
-
schema: endpoint.schema,
|
|
55
52
|
key: endpoint.key(...args)
|
|
56
53
|
};
|
|
57
|
-
// For legacy support; TODO: remove
|
|
58
|
-
if (endpoint.update) meta.update = endpoint.update;
|
|
59
|
-
if (endpoint.errorPolicy) meta.errorPolicy = endpoint.errorPolicy;
|
|
60
54
|
const action = {
|
|
61
55
|
type: OPTIMISTIC_TYPE,
|
|
62
56
|
endpoint,
|
|
@@ -65,152 +59,12 @@ function createOptimistic(endpoint, {
|
|
|
65
59
|
return action;
|
|
66
60
|
}
|
|
67
61
|
|
|
68
|
-
// TODO(breaking): remove file - not used
|
|
69
|
-
/** Requesting a fetch to begin
|
|
70
|
-
*
|
|
71
|
-
* @param fetchShape
|
|
72
|
-
* @param param1 { params, body, throttle, updateParams }
|
|
73
|
-
*/
|
|
74
|
-
function createFetch$1(fetchShape, {
|
|
75
|
-
params,
|
|
76
|
-
body,
|
|
77
|
-
throttle,
|
|
78
|
-
updateParams
|
|
79
|
-
}) {
|
|
80
|
-
const {
|
|
81
|
-
schema,
|
|
82
|
-
type,
|
|
83
|
-
getFetchKey,
|
|
84
|
-
options
|
|
85
|
-
} = fetchShape;
|
|
86
|
-
const key = getFetchKey(params);
|
|
87
|
-
let resolve = 0;
|
|
88
|
-
let reject = 0;
|
|
89
|
-
const promise = new Promise((a, b) => {
|
|
90
|
-
[resolve, reject] = [a, b];
|
|
91
|
-
});
|
|
92
|
-
const meta = {
|
|
93
|
-
schema,
|
|
94
|
-
type,
|
|
95
|
-
args: [params, body],
|
|
96
|
-
key,
|
|
97
|
-
throttle,
|
|
98
|
-
options,
|
|
99
|
-
resolve,
|
|
100
|
-
reject,
|
|
101
|
-
promise,
|
|
102
|
-
createdAt: Date.now()
|
|
103
|
-
};
|
|
104
|
-
if (fetchShape.update) {
|
|
105
|
-
meta.update = fetchShape.update;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// for simplicity we simply override if updateParams are defined - usage together is silly to support as we are migrating
|
|
109
|
-
if (updateParams) {
|
|
110
|
-
meta.update = newresult => {
|
|
111
|
-
const updateMap = {};
|
|
112
|
-
updateParams.forEach(([toShape, toParams, updateFn]) => {
|
|
113
|
-
updateMap[toShape.getFetchKey(toParams)] = existing => updateFn(newresult, existing);
|
|
114
|
-
});
|
|
115
|
-
return updateMap;
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
if (options != null && options.optimisticUpdate) {
|
|
119
|
-
meta.optimisticResponse = options.optimisticUpdate(params, body);
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
type: FETCH_TYPE,
|
|
123
|
-
payload: () => fetchShape.fetch(params, body),
|
|
124
|
-
meta
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Update state with data
|
|
129
|
-
*
|
|
130
|
-
* @param data
|
|
131
|
-
* @param param1 { schema, key, type, updaters, dataExpiryLength }
|
|
132
|
-
*/
|
|
133
|
-
function createReceive$1(data, {
|
|
134
|
-
schema,
|
|
135
|
-
key,
|
|
136
|
-
args,
|
|
137
|
-
updaters,
|
|
138
|
-
fetchedAt = 0,
|
|
139
|
-
update,
|
|
140
|
-
dataExpiryLength
|
|
141
|
-
}) {
|
|
142
|
-
/* istanbul ignore next */
|
|
143
|
-
if (process.env.NODE_ENV === 'development' && dataExpiryLength < 0) {
|
|
144
|
-
throw new Error('Negative dataExpiryLength are not allowed.');
|
|
145
|
-
}
|
|
146
|
-
const now = Date.now();
|
|
147
|
-
const meta = {
|
|
148
|
-
schema,
|
|
149
|
-
key,
|
|
150
|
-
args,
|
|
151
|
-
date: now,
|
|
152
|
-
fetchedAt,
|
|
153
|
-
expiresAt: now + dataExpiryLength
|
|
154
|
-
};
|
|
155
|
-
meta.updaters = updaters;
|
|
156
|
-
meta.update = update;
|
|
157
|
-
return {
|
|
158
|
-
type: RECEIVE_TYPE,
|
|
159
|
-
payload: data,
|
|
160
|
-
meta
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function createReceiveError(error, {
|
|
165
|
-
schema,
|
|
166
|
-
key,
|
|
167
|
-
options,
|
|
168
|
-
errorExpiryLength = 60000,
|
|
169
|
-
fetchedAt = 0
|
|
170
|
-
}) {
|
|
171
|
-
/* istanbul ignore next */
|
|
172
|
-
if (process.env.NODE_ENV === 'development' && errorExpiryLength < 0) {
|
|
173
|
-
throw new Error('Negative errorExpiryLength are not allowed.');
|
|
174
|
-
}
|
|
175
|
-
const now = Date.now();
|
|
176
|
-
return {
|
|
177
|
-
type: RECEIVE_TYPE,
|
|
178
|
-
payload: error,
|
|
179
|
-
meta: {
|
|
180
|
-
schema,
|
|
181
|
-
key,
|
|
182
|
-
date: now,
|
|
183
|
-
fetchedAt,
|
|
184
|
-
expiresAt: now + errorExpiryLength,
|
|
185
|
-
errorPolicy: options == null ? void 0 : options.errorPolicy
|
|
186
|
-
},
|
|
187
|
-
error: true
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
var index = /*#__PURE__*/Object.freeze({
|
|
192
|
-
__proto__: null,
|
|
193
|
-
createFetch: createFetch$1,
|
|
194
|
-
createReceive: createReceive$1,
|
|
195
|
-
createReceiveError: createReceiveError
|
|
196
|
-
});
|
|
197
|
-
|
|
198
62
|
function fetchReducer(state, action) {
|
|
199
|
-
var _action$endpoint;
|
|
200
|
-
const optimisticResponse = action.meta.optimisticResponse;
|
|
201
|
-
const getOptimisticResponse = (_action$endpoint = action.endpoint) == null ? void 0 : _action$endpoint.getOptimisticResponse;
|
|
202
63
|
let receiveAction;
|
|
203
|
-
if (
|
|
64
|
+
if (action.endpoint.getOptimisticResponse && action.endpoint.sideEffect) {
|
|
204
65
|
receiveAction = createOptimistic(action.endpoint, {
|
|
205
66
|
args: action.meta.args,
|
|
206
|
-
fetchedAt:
|
|
207
|
-
});
|
|
208
|
-
} /* istanbul ignore if */else if (optimisticResponse) {
|
|
209
|
-
// TODO(breaking): this is no longer used, remove this branch
|
|
210
|
-
/* istanbul ignore next */
|
|
211
|
-
receiveAction = createReceive$1(optimisticResponse, {
|
|
212
|
-
...action.meta,
|
|
213
|
-
dataExpiryLength: Infinity
|
|
67
|
+
fetchedAt: action.meta.createdAt
|
|
214
68
|
});
|
|
215
69
|
} else {
|
|
216
70
|
// If 'fetch' action reaches the reducer there are no middlewares installed to handle it
|
|
@@ -260,13 +114,6 @@ function invalidateReducer(state, action) {
|
|
|
260
114
|
};
|
|
261
115
|
}
|
|
262
116
|
|
|
263
|
-
function applyUpdatersToResults(results, result, updaters) {
|
|
264
|
-
return {
|
|
265
|
-
...results,
|
|
266
|
-
...Object.fromEntries(Object.entries(updaters).map(([fetchKey, updater]) => [fetchKey, updater(result, results[fetchKey])]))
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
|
|
270
117
|
function setReducer(state, action, controller) {
|
|
271
118
|
if (action.error) {
|
|
272
119
|
return reduceError(state, action, action.payload);
|
|
@@ -276,12 +123,11 @@ function setReducer(state, action, controller) {
|
|
|
276
123
|
let payload;
|
|
277
124
|
// for true receives payload is contained in action
|
|
278
125
|
if (action.type === OPTIMISTIC_TYPE) {
|
|
126
|
+
// this should never happen
|
|
279
127
|
if (!action.endpoint.getOptimisticResponse) return state;
|
|
280
128
|
try {
|
|
281
129
|
// compute optimistic response based on current state
|
|
282
|
-
payload = action.endpoint.getOptimisticResponse.call(action.endpoint, controller.snapshot(state, action.meta.fetchedAt),
|
|
283
|
-
// if endpoint exists, so must args; TODO: fix typing
|
|
284
|
-
...action.meta.args);
|
|
130
|
+
payload = action.endpoint.getOptimisticResponse.call(action.endpoint, controller.snapshot(state, action.meta.fetchedAt), ...action.meta.args);
|
|
285
131
|
} catch (e) {
|
|
286
132
|
var _e$constructor;
|
|
287
133
|
// AbortOptimistic means 'do nothing', otherwise we count the exception as endpoint failure
|
|
@@ -298,20 +144,14 @@ function setReducer(state, action, controller) {
|
|
|
298
144
|
entities,
|
|
299
145
|
indexes,
|
|
300
146
|
entityMeta
|
|
301
|
-
} = normalizr.normalize(payload, action.
|
|
302
|
-
|
|
303
|
-
...action.meta
|
|
304
|
-
});
|
|
305
|
-
let results = {
|
|
147
|
+
} = normalizr.normalize(payload, action.endpoint.schema, action.meta.args, state.entities, state.indexes, state.entityMeta, action.meta);
|
|
148
|
+
const results = {
|
|
306
149
|
...state.results,
|
|
307
150
|
[action.meta.key]: result
|
|
308
151
|
};
|
|
309
152
|
try {
|
|
310
|
-
if (
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
if (action.meta.update) {
|
|
314
|
-
const updaters = action.meta.update(result, ...(action.meta.args || []));
|
|
153
|
+
if (action.endpoint.update) {
|
|
154
|
+
const updaters = action.endpoint.update(result, ...action.meta.args);
|
|
315
155
|
Object.keys(updaters).forEach(key => {
|
|
316
156
|
results[key] = updaters[key](results[key]);
|
|
317
157
|
});
|
|
@@ -341,7 +181,7 @@ function setReducer(state, action, controller) {
|
|
|
341
181
|
// reducer must update the state, so in case of processing errors we simply compute the results inline
|
|
342
182
|
} catch (error) {
|
|
343
183
|
if (typeof error === 'object') {
|
|
344
|
-
error.message = `Error processing ${action.meta.key}\n\nFull Schema: ${JSON.stringify(action.
|
|
184
|
+
error.message = `Error processing ${action.meta.key}\n\nFull Schema: ${JSON.stringify(action.endpoint.schema, undefined, 2)}\n\nError:\n${error.message}`;
|
|
345
185
|
if ('payload' in action) error.payload = action.payload;
|
|
346
186
|
error.status = 400;
|
|
347
187
|
}
|
|
@@ -372,7 +212,7 @@ function reduceError(state, action, error) {
|
|
|
372
212
|
date: action.meta.date,
|
|
373
213
|
error,
|
|
374
214
|
expiresAt: action.meta.expiresAt,
|
|
375
|
-
errorPolicy: action.
|
|
215
|
+
errorPolicy: action.endpoint.errorPolicy == null ? void 0 : action.endpoint.errorPolicy(error)
|
|
376
216
|
}
|
|
377
217
|
},
|
|
378
218
|
optimistic: filterOptimistic(state, action)
|
|
@@ -385,7 +225,6 @@ function filterOptimistic(state, resolvingAction) {
|
|
|
385
225
|
|
|
386
226
|
function createReducer(controller) {
|
|
387
227
|
return function reducer(state, action) {
|
|
388
|
-
var _action$date;
|
|
389
228
|
if (!state) state = initialState;
|
|
390
229
|
switch (action.type) {
|
|
391
230
|
case GC_TYPE:
|
|
@@ -410,12 +249,9 @@ function createReducer(controller) {
|
|
|
410
249
|
case INVALIDATE_TYPE:
|
|
411
250
|
return invalidateReducer(state, action);
|
|
412
251
|
case RESET_TYPE:
|
|
413
|
-
if (process.env.NODE_ENV !== 'production' && action.date === undefined) {
|
|
414
|
-
console.warn(`${RESET_TYPE} sent without 'date' member. This is deprecated. Please use createReset() action creator to ensure correct action shape.`);
|
|
415
|
-
}
|
|
416
252
|
return {
|
|
417
253
|
...initialState,
|
|
418
|
-
lastReset:
|
|
254
|
+
lastReset: action.date
|
|
419
255
|
};
|
|
420
256
|
default:
|
|
421
257
|
// A reducer must always return a valid state.
|
|
@@ -438,10 +274,41 @@ var internal = /*#__PURE__*/Object.freeze({
|
|
|
438
274
|
__proto__: null,
|
|
439
275
|
inferResults: normalizr.inferResults,
|
|
440
276
|
DELETED: normalizr.DELETED,
|
|
277
|
+
INVALID: normalizr.INVALID,
|
|
441
278
|
RIC: RIC$1,
|
|
442
279
|
initialState: initialState
|
|
443
280
|
});
|
|
444
281
|
|
|
282
|
+
/**
|
|
283
|
+
* Requesting a fetch to begin
|
|
284
|
+
*/
|
|
285
|
+
function createFetch(endpoint, {
|
|
286
|
+
args
|
|
287
|
+
}) {
|
|
288
|
+
const key = endpoint.key(...args);
|
|
289
|
+
let resolve = 0;
|
|
290
|
+
let reject = 0;
|
|
291
|
+
const promise = new Promise((a, b) => {
|
|
292
|
+
[resolve, reject] = [a, b];
|
|
293
|
+
});
|
|
294
|
+
const meta = {
|
|
295
|
+
args,
|
|
296
|
+
key,
|
|
297
|
+
throttle: !endpoint.sideEffect,
|
|
298
|
+
resolve,
|
|
299
|
+
reject,
|
|
300
|
+
promise,
|
|
301
|
+
createdAt: Date.now(),
|
|
302
|
+
nm: false
|
|
303
|
+
};
|
|
304
|
+
return {
|
|
305
|
+
type: FETCH_TYPE,
|
|
306
|
+
payload: () => endpoint(...args),
|
|
307
|
+
meta,
|
|
308
|
+
endpoint
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
445
312
|
function createInvalidate(endpoint, {
|
|
446
313
|
args
|
|
447
314
|
}) {
|
|
@@ -460,7 +327,14 @@ function createInvalidateAll(testKey) {
|
|
|
460
327
|
};
|
|
461
328
|
}
|
|
462
329
|
|
|
463
|
-
function
|
|
330
|
+
function createReset() {
|
|
331
|
+
return {
|
|
332
|
+
type: RESET_TYPE,
|
|
333
|
+
date: Date.now()
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function createSet(endpoint, {
|
|
464
338
|
args,
|
|
465
339
|
fetchedAt,
|
|
466
340
|
response,
|
|
@@ -478,13 +352,8 @@ function createReceive(endpoint, {
|
|
|
478
352
|
fetchedAt: fetchedAt != null ? fetchedAt : now,
|
|
479
353
|
date: now,
|
|
480
354
|
expiresAt: now + expiryLength,
|
|
481
|
-
// For legacy support; TODO: remove
|
|
482
|
-
schema: endpoint.schema,
|
|
483
355
|
key: endpoint.key(...args)
|
|
484
356
|
};
|
|
485
|
-
// For legacy support; TODO: remove
|
|
486
|
-
if (endpoint.update) meta.update = endpoint.update;
|
|
487
|
-
if (endpoint.errorPolicy) meta.errorPolicy = endpoint.errorPolicy;
|
|
488
357
|
const action = {
|
|
489
358
|
type: SET_TYPE,
|
|
490
359
|
payload: response,
|
|
@@ -495,13 +364,6 @@ function createReceive(endpoint, {
|
|
|
495
364
|
return action;
|
|
496
365
|
}
|
|
497
366
|
|
|
498
|
-
function createReset() {
|
|
499
|
-
return {
|
|
500
|
-
type: RESET_TYPE,
|
|
501
|
-
date: Date.now()
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
|
|
505
367
|
function createSubscription(endpoint, {
|
|
506
368
|
args
|
|
507
369
|
}) {
|
|
@@ -510,10 +372,7 @@ function createSubscription(endpoint, {
|
|
|
510
372
|
endpoint,
|
|
511
373
|
meta: {
|
|
512
374
|
args,
|
|
513
|
-
key: endpoint.key(...args)
|
|
514
|
-
fetch: () => endpoint(...args),
|
|
515
|
-
schema: endpoint.schema,
|
|
516
|
-
options: endpoint
|
|
375
|
+
key: endpoint.key(...args)
|
|
517
376
|
}
|
|
518
377
|
};
|
|
519
378
|
}
|
|
@@ -525,8 +384,7 @@ function createUnsubscription(endpoint, {
|
|
|
525
384
|
endpoint,
|
|
526
385
|
meta: {
|
|
527
386
|
args,
|
|
528
|
-
key: endpoint.key(...args)
|
|
529
|
-
options: endpoint
|
|
387
|
+
key: endpoint.key(...args)
|
|
530
388
|
}
|
|
531
389
|
};
|
|
532
390
|
}
|
|
@@ -548,7 +406,7 @@ const unsetState = () => {
|
|
|
548
406
|
* Imperative control of Rest Hooks store
|
|
549
407
|
* @see https://resthooks.io/docs/api/Controller
|
|
550
408
|
*/
|
|
551
|
-
class Controller
|
|
409
|
+
class Controller {
|
|
552
410
|
/**
|
|
553
411
|
* Dispatches an action to Rest Hooks reducer.
|
|
554
412
|
*
|
|
@@ -572,6 +430,20 @@ class Controller$1 {
|
|
|
572
430
|
}
|
|
573
431
|
} = {}) {
|
|
574
432
|
/*************** Action Dispatchers ***************/
|
|
433
|
+
/**
|
|
434
|
+
* Fetches the endpoint with given args, updating the Rest Hooks cache with the response or error upon completion.
|
|
435
|
+
* @see https://resthooks.io/docs/api/Controller#fetch
|
|
436
|
+
*/
|
|
437
|
+
this.fetch = (endpoint, ...args) => {
|
|
438
|
+
const action = createFetch(endpoint, {
|
|
439
|
+
args
|
|
440
|
+
});
|
|
441
|
+
this.dispatch(action);
|
|
442
|
+
if (endpoint.schema) {
|
|
443
|
+
return action.meta.promise.then(input => normalizr.denormalize(input, endpoint.schema, {}, args));
|
|
444
|
+
}
|
|
445
|
+
return action.meta.promise;
|
|
446
|
+
};
|
|
575
447
|
/**
|
|
576
448
|
* Forces refetching and suspense on useSuspense with the same Endpoint and parameters.
|
|
577
449
|
* @see https://resthooks.io/docs/api/Controller#invalidate
|
|
@@ -595,16 +467,14 @@ class Controller$1 {
|
|
|
595
467
|
*/
|
|
596
468
|
this.setResponse = (endpoint, ...rest) => {
|
|
597
469
|
const response = rest[rest.length - 1];
|
|
598
|
-
const action =
|
|
470
|
+
const action = createSet(endpoint, {
|
|
599
471
|
args: rest.slice(0, rest.length - 1),
|
|
600
472
|
response
|
|
601
473
|
});
|
|
602
474
|
return this.dispatch(action);
|
|
603
475
|
};
|
|
604
|
-
// TODO: deprecate
|
|
605
476
|
/**
|
|
606
|
-
*
|
|
607
|
-
* @see https://resthooks.io/docs/api/Controller#setResponse
|
|
477
|
+
* @deprecated use https://resthooks.io/docs/api/Controller#setResponse instead
|
|
608
478
|
*/
|
|
609
479
|
/* istanbul ignore next */
|
|
610
480
|
this.receive = (endpoint, ...rest) => {
|
|
@@ -617,17 +487,16 @@ class Controller$1 {
|
|
|
617
487
|
*/
|
|
618
488
|
this.setError = (endpoint, ...rest) => {
|
|
619
489
|
const response = rest[rest.length - 1];
|
|
620
|
-
const action =
|
|
490
|
+
const action = createSet(endpoint, {
|
|
621
491
|
args: rest.slice(0, rest.length - 1),
|
|
622
492
|
response,
|
|
623
493
|
error: true
|
|
624
494
|
});
|
|
625
495
|
return this.dispatch(action);
|
|
626
496
|
};
|
|
627
|
-
// TODO: deprecate
|
|
628
497
|
/**
|
|
629
498
|
* Another name for setError
|
|
630
|
-
* @
|
|
499
|
+
* @deprecated use https://resthooks.io/docs/api/Controller#setError instead
|
|
631
500
|
*/
|
|
632
501
|
/* istanbul ignore next */
|
|
633
502
|
this.receiveError = (endpoint, ...rest) => {
|
|
@@ -639,7 +508,7 @@ class Controller$1 {
|
|
|
639
508
|
* @see https://resthooks.io/docs/api/Controller#resolve
|
|
640
509
|
*/
|
|
641
510
|
this.resolve = (endpoint, meta) => {
|
|
642
|
-
return this.dispatch(
|
|
511
|
+
return this.dispatch(createSet(endpoint, meta));
|
|
643
512
|
};
|
|
644
513
|
/**
|
|
645
514
|
* Marks a new subscription to a given Endpoint.
|
|
@@ -721,17 +590,6 @@ class Controller$1 {
|
|
|
721
590
|
expiresAt: expiresAt || 0
|
|
722
591
|
};
|
|
723
592
|
}
|
|
724
|
-
|
|
725
|
-
// Warn users with bad configurations
|
|
726
|
-
/* istanbul ignore next */
|
|
727
|
-
if (process.env.NODE_ENV !== 'production' && schema && normalizr.isEntity(schema)) {
|
|
728
|
-
if (Array.isArray(results)) {
|
|
729
|
-
throw new Error(`fetch key ${key} has list results when single result is expected`);
|
|
730
|
-
}
|
|
731
|
-
if (typeof results === 'object') {
|
|
732
|
-
throw new Error(`fetch key ${key} has object results when entity's primary key (string) result is expected`);
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
593
|
if (!this.globalCache.results[key]) this.globalCache.results[key] = new normalizr.WeakEntityMap();
|
|
736
594
|
|
|
737
595
|
// second argument is false if any entities are missing
|
|
@@ -815,64 +673,6 @@ class Snapshot {
|
|
|
815
673
|
}
|
|
816
674
|
}
|
|
817
675
|
|
|
818
|
-
/**
|
|
819
|
-
* Requesting a fetch to begin
|
|
820
|
-
*/
|
|
821
|
-
function createFetch(endpoint, {
|
|
822
|
-
args
|
|
823
|
-
}) {
|
|
824
|
-
const key = endpoint.key(...args);
|
|
825
|
-
let resolve = 0;
|
|
826
|
-
let reject = 0;
|
|
827
|
-
const promise = new Promise((a, b) => {
|
|
828
|
-
[resolve, reject] = [a, b];
|
|
829
|
-
});
|
|
830
|
-
const meta = {
|
|
831
|
-
schema: endpoint.schema,
|
|
832
|
-
type: endpoint.sideEffect ? 'mutate' : 'read',
|
|
833
|
-
args,
|
|
834
|
-
key,
|
|
835
|
-
throttle: !endpoint.sideEffect,
|
|
836
|
-
options: endpoint,
|
|
837
|
-
resolve,
|
|
838
|
-
reject,
|
|
839
|
-
promise,
|
|
840
|
-
createdAt: Date.now()
|
|
841
|
-
};
|
|
842
|
-
if (endpoint.update) {
|
|
843
|
-
meta.update = endpoint.update;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
// TODO: Remove once EOL on this deprecated piece
|
|
847
|
-
/* istanbul ignore if */
|
|
848
|
-
if (endpoint.optimisticUpdate) {
|
|
849
|
-
meta.optimisticResponse = endpoint.optimisticUpdate(...args);
|
|
850
|
-
}
|
|
851
|
-
return {
|
|
852
|
-
type: FETCH_TYPE,
|
|
853
|
-
payload: () => endpoint(...args),
|
|
854
|
-
meta,
|
|
855
|
-
endpoint
|
|
856
|
-
};
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
class Controller extends Controller$1 {
|
|
860
|
-
constructor(..._args) {
|
|
861
|
-
super(..._args);
|
|
862
|
-
/**
|
|
863
|
-
* Fetches the endpoint with given args, updating the Rest Hooks cache with the response or error upon completion.
|
|
864
|
-
* @see https://resthooks.io/docs/api/Controller#fetch
|
|
865
|
-
*/
|
|
866
|
-
this.fetch = (endpoint, ...args) => {
|
|
867
|
-
const action = createFetch(endpoint, {
|
|
868
|
-
args
|
|
869
|
-
});
|
|
870
|
-
this.dispatch(action);
|
|
871
|
-
return action.meta.promise;
|
|
872
|
-
};
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
|
|
876
676
|
class ResetError extends Error {
|
|
877
677
|
constructor() {
|
|
878
678
|
super('Aborted due to RESET');
|
|
@@ -887,7 +687,7 @@ class ResetError extends Error {
|
|
|
887
687
|
*
|
|
888
688
|
* Interfaces with store via a redux-compatible middleware.
|
|
889
689
|
*
|
|
890
|
-
* @see https://
|
|
690
|
+
* @see https://dataclient.io/docs/api/NetworkManager
|
|
891
691
|
*/
|
|
892
692
|
class NetworkManager {
|
|
893
693
|
constructor(dataExpiryLength = 60000, errorExpiryLength = 1000) {
|
|
@@ -895,26 +695,19 @@ class NetworkManager {
|
|
|
895
695
|
this.resolvers = {};
|
|
896
696
|
this.rejectors = {};
|
|
897
697
|
this.fetchedAt = {};
|
|
898
|
-
this.getState = () => initialState;
|
|
899
698
|
this.controller = new Controller();
|
|
900
699
|
this.dataExpiryLength = dataExpiryLength;
|
|
901
700
|
this.errorExpiryLength = errorExpiryLength;
|
|
902
|
-
this.middleware =
|
|
903
|
-
dispatch,
|
|
904
|
-
getState,
|
|
905
|
-
controller
|
|
906
|
-
}) => {
|
|
907
|
-
this.getState = getState;
|
|
701
|
+
this.middleware = controller => {
|
|
908
702
|
this.controller = controller;
|
|
909
703
|
return next => action => {
|
|
910
|
-
var _action$endpoint;
|
|
911
704
|
switch (action.type) {
|
|
912
705
|
case FETCH_TYPE:
|
|
913
|
-
this.handleFetch(action
|
|
706
|
+
this.handleFetch(action);
|
|
914
707
|
// This is the only case that causes any state change
|
|
915
708
|
// It's important to intercept other fetches as we don't want to trigger reducers during
|
|
916
709
|
// render - so we need to stop 'readonly' fetches which can be triggered in render
|
|
917
|
-
if (action.
|
|
710
|
+
if (action.endpoint.getOptimisticResponse !== undefined && action.endpoint.sideEffect) {
|
|
918
711
|
return next(action);
|
|
919
712
|
}
|
|
920
713
|
return Promise.resolve();
|
|
@@ -927,8 +720,12 @@ class NetworkManager {
|
|
|
927
720
|
const error = (_controller$getState$ = controller.getState().meta[action.meta.key]) == null ? void 0 : _controller$getState$.error;
|
|
928
721
|
// processing errors result in state meta having error, so we should reject the promise
|
|
929
722
|
if (error) {
|
|
930
|
-
|
|
931
|
-
|
|
723
|
+
this.handleReceive(createSet(action.endpoint, {
|
|
724
|
+
args: action.meta.args,
|
|
725
|
+
response: error,
|
|
726
|
+
fetchedAt: action.meta.fetchedAt,
|
|
727
|
+
error: true
|
|
728
|
+
}));
|
|
932
729
|
} else {
|
|
933
730
|
this.handleReceive(action);
|
|
934
731
|
}
|
|
@@ -994,10 +791,7 @@ class NetworkManager {
|
|
|
994
791
|
}
|
|
995
792
|
getLastReset() {
|
|
996
793
|
if (this.cleanupDate) return this.cleanupDate;
|
|
997
|
-
|
|
998
|
-
if (lastReset instanceof Date) return lastReset.valueOf();
|
|
999
|
-
if (typeof lastReset !== 'number') return -Infinity;
|
|
1000
|
-
return lastReset;
|
|
794
|
+
return this.controller.getState().lastReset;
|
|
1001
795
|
}
|
|
1002
796
|
|
|
1003
797
|
/** Called when middleware intercepts 'rest-hooks/fetch' action.
|
|
@@ -1008,16 +802,15 @@ class NetworkManager {
|
|
|
1008
802
|
* Uses throttle only when instructed by action meta. This is valuable
|
|
1009
803
|
* for ensures mutation requests always go through.
|
|
1010
804
|
*/
|
|
1011
|
-
handleFetch(action
|
|
805
|
+
handleFetch(action) {
|
|
1012
806
|
const fetch = action.payload;
|
|
1013
807
|
const {
|
|
1014
808
|
key,
|
|
1015
809
|
throttle,
|
|
1016
810
|
resolve,
|
|
1017
|
-
reject
|
|
811
|
+
reject,
|
|
812
|
+
createdAt
|
|
1018
813
|
} = action.meta;
|
|
1019
|
-
// TODO(breaking): remove support for Date type in 'Receive' action
|
|
1020
|
-
const createdAt = typeof action.meta.createdAt !== 'number' ? action.meta.createdAt.getTime() : action.meta.createdAt;
|
|
1021
814
|
const deferedFetch = () => {
|
|
1022
815
|
let promise = fetch();
|
|
1023
816
|
const resolvePromise = promise => promise.then(data => {
|
|
@@ -1030,7 +823,7 @@ class NetworkManager {
|
|
|
1030
823
|
// schedule non-throttled resolutions in a microtask before receive
|
|
1031
824
|
// this enables users awaiting their fetch to trigger any react updates needed to deal
|
|
1032
825
|
// with upcoming changes because of the fetch (for instance avoiding suspense if something is deleted)
|
|
1033
|
-
if (!throttle
|
|
826
|
+
if (!throttle) {
|
|
1034
827
|
promise = resolvePromise(promise);
|
|
1035
828
|
}
|
|
1036
829
|
promise = promise.then(data => {
|
|
@@ -1044,51 +837,26 @@ class NetworkManager {
|
|
|
1044
837
|
|
|
1045
838
|
// don't update state with promises started before last clear
|
|
1046
839
|
if (createdAt >= lastReset) {
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
fetchedAt: createdAt
|
|
1053
|
-
});
|
|
1054
|
-
} else {
|
|
1055
|
-
var _action$meta$options$, _action$meta$options;
|
|
1056
|
-
// TODO(breaking): is this branch still possible? remove in next major update
|
|
1057
|
-
// does this throw if the reducer fails? - no because reducer is wrapped in try/catch
|
|
1058
|
-
this.controller.dispatch(createReceive$1(data, {
|
|
1059
|
-
...action.meta,
|
|
1060
|
-
fetchedAt: createdAt,
|
|
1061
|
-
dataExpiryLength: (_action$meta$options$ = (_action$meta$options = action.meta.options) == null ? void 0 : _action$meta$options.dataExpiryLength) != null ? _action$meta$options$ : this.dataExpiryLength
|
|
1062
|
-
}));
|
|
1063
|
-
}
|
|
840
|
+
this.controller.resolve(action.endpoint, {
|
|
841
|
+
args: action.meta.args,
|
|
842
|
+
response: data,
|
|
843
|
+
fetchedAt: createdAt
|
|
844
|
+
});
|
|
1064
845
|
}
|
|
1065
846
|
return data;
|
|
1066
847
|
}).catch(error => {
|
|
1067
848
|
const lastReset = this.getLastReset();
|
|
1068
849
|
// don't update state with promises started before last clear
|
|
1069
850
|
if (createdAt >= lastReset) {
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
});
|
|
1077
|
-
} else {
|
|
1078
|
-
var _action$meta$options$2, _action$meta$options2;
|
|
1079
|
-
this.controller.dispatch(createReceiveError(error, {
|
|
1080
|
-
...action.meta,
|
|
1081
|
-
errorExpiryLength: (_action$meta$options$2 = (_action$meta$options2 = action.meta.options) == null ? void 0 : _action$meta$options2.errorExpiryLength) != null ? _action$meta$options$2 : this.errorExpiryLength,
|
|
1082
|
-
fetchedAt: createdAt
|
|
1083
|
-
}));
|
|
1084
|
-
}
|
|
851
|
+
this.controller.resolve(action.endpoint, {
|
|
852
|
+
args: action.meta.args,
|
|
853
|
+
response: error,
|
|
854
|
+
fetchedAt: createdAt,
|
|
855
|
+
error: true
|
|
856
|
+
});
|
|
1085
857
|
}
|
|
1086
858
|
throw error;
|
|
1087
859
|
});
|
|
1088
|
-
// legacy behavior schedules resolution after dispatch
|
|
1089
|
-
if (!throttle && !action.endpoint) {
|
|
1090
|
-
promise = resolvePromise(promise);
|
|
1091
|
-
}
|
|
1092
860
|
return promise;
|
|
1093
861
|
};
|
|
1094
862
|
if (throttle) {
|
|
@@ -1163,16 +931,10 @@ class NetworkManager {
|
|
|
1163
931
|
}
|
|
1164
932
|
}
|
|
1165
933
|
|
|
1166
|
-
/* istanbul ignore file */
|
|
1167
|
-
/**
|
|
1168
|
-
* @deprecated use createReducer instead
|
|
1169
|
-
*/
|
|
1170
|
-
const reducer = createReducer(new Controller());
|
|
1171
|
-
var reducer$1 = reducer;
|
|
1172
|
-
|
|
1173
934
|
function applyManager(managers, controller) {
|
|
1174
935
|
return managers.map(manager => {
|
|
1175
936
|
const middleware = manager.getMiddleware();
|
|
937
|
+
// TODO(breaking): remove this once controller prop is no longer supported
|
|
1176
938
|
return ({
|
|
1177
939
|
dispatch,
|
|
1178
940
|
getState
|
|
@@ -1242,16 +1004,10 @@ var DefaultConnectionListener$1 = DefaultConnectionListener;
|
|
|
1242
1004
|
* dispatching a fetch at a rate equal to the minimum update
|
|
1243
1005
|
* interval requested.
|
|
1244
1006
|
*
|
|
1245
|
-
* @see https://
|
|
1007
|
+
* @see https://dataclient.io/docs/api/PollingSubscription
|
|
1246
1008
|
*/
|
|
1247
1009
|
class PollingSubscription {
|
|
1248
|
-
constructor({
|
|
1249
|
-
key,
|
|
1250
|
-
schema,
|
|
1251
|
-
fetch,
|
|
1252
|
-
frequency,
|
|
1253
|
-
getState
|
|
1254
|
-
}, dispatch, connectionListener) {
|
|
1010
|
+
constructor(action, controller, connectionListener) {
|
|
1255
1011
|
this.frequencyHistogram = new Map();
|
|
1256
1012
|
/** What happens when browser goes offline */
|
|
1257
1013
|
this.offlineListener = () => {
|
|
@@ -1274,14 +1030,13 @@ class PollingSubscription {
|
|
|
1274
1030
|
}, Math.max(0, this.lastFetchTime() - now + this.frequency));
|
|
1275
1031
|
this.connectionListener.addOfflineListener(this.offlineListener);
|
|
1276
1032
|
};
|
|
1277
|
-
if (
|
|
1278
|
-
this.
|
|
1279
|
-
this.
|
|
1280
|
-
this.
|
|
1281
|
-
this.key = key;
|
|
1033
|
+
if (action.endpoint.pollFrequency === undefined) throw new Error('frequency needed for polling subscription');
|
|
1034
|
+
this.endpoint = action.endpoint;
|
|
1035
|
+
this.frequency = action.endpoint.pollFrequency;
|
|
1036
|
+
this.args = action.meta.args;
|
|
1037
|
+
this.key = action.meta.key;
|
|
1282
1038
|
this.frequencyHistogram.set(this.frequency, 1);
|
|
1283
|
-
this.
|
|
1284
|
-
this.getState = getState;
|
|
1039
|
+
this.controller = controller;
|
|
1285
1040
|
this.connectionListener = connectionListener || new DefaultConnectionListener$1();
|
|
1286
1041
|
|
|
1287
1042
|
// Kickstart running since this is initialized after the online notif is sent
|
|
@@ -1354,18 +1109,17 @@ class PollingSubscription {
|
|
|
1354
1109
|
|
|
1355
1110
|
/** Trigger request for latest resource */
|
|
1356
1111
|
update() {
|
|
1357
|
-
const
|
|
1358
|
-
endpoint
|
|
1359
|
-
|
|
1112
|
+
const sup = this.endpoint;
|
|
1113
|
+
const endpoint = function (...args) {
|
|
1114
|
+
return sup.call(this, ...args);
|
|
1115
|
+
};
|
|
1116
|
+
Object.assign(endpoint, this.endpoint);
|
|
1360
1117
|
endpoint.dataExpiryLength = this.frequency / 2;
|
|
1361
1118
|
endpoint.errorExpiryLength = this.frequency / 10;
|
|
1362
1119
|
endpoint.errorPolicy = () => 'soft';
|
|
1363
|
-
|
|
1364
|
-
args: []
|
|
1365
|
-
});
|
|
1120
|
+
endpoint.key = () => this.key;
|
|
1366
1121
|
// stop any errors here from bubbling
|
|
1367
|
-
|
|
1368
|
-
this.dispatch(action);
|
|
1122
|
+
this.controller.fetch(endpoint, ...this.args).catch(() => null);
|
|
1369
1123
|
}
|
|
1370
1124
|
/** Run polling process with current frequency
|
|
1371
1125
|
*
|
|
@@ -1389,13 +1143,11 @@ class PollingSubscription {
|
|
|
1389
1143
|
|
|
1390
1144
|
/** Last fetch time */
|
|
1391
1145
|
lastFetchTime() {
|
|
1392
|
-
var _this$
|
|
1393
|
-
return (_this$
|
|
1146
|
+
var _this$controller$getS, _this$controller$getS2;
|
|
1147
|
+
return (_this$controller$getS = (_this$controller$getS2 = this.controller.getState().meta[this.key]) == null ? void 0 : _this$controller$getS2.date) != null ? _this$controller$getS : 0;
|
|
1394
1148
|
}
|
|
1395
1149
|
}
|
|
1396
1150
|
|
|
1397
|
-
/** Properties sent to Subscription constructor */
|
|
1398
|
-
|
|
1399
1151
|
/** Interface handling a single resource subscription */
|
|
1400
1152
|
|
|
1401
1153
|
/** The static class that constructs Subscription */
|
|
@@ -1405,27 +1157,26 @@ class PollingSubscription {
|
|
|
1405
1157
|
* Constructor takes a SubscriptionConstructable class to control how
|
|
1406
1158
|
* subscriptions are handled. (e.g., polling, websockets)
|
|
1407
1159
|
*
|
|
1408
|
-
* @see https://
|
|
1160
|
+
* @see https://dataclient.io/docs/api/SubscriptionManager
|
|
1409
1161
|
*/
|
|
1410
1162
|
class SubscriptionManager {
|
|
1411
1163
|
constructor(Subscription) {
|
|
1412
1164
|
this.subscriptions = {};
|
|
1165
|
+
this.controller = new Controller();
|
|
1413
1166
|
this.Subscription = Subscription;
|
|
1414
|
-
this.middleware =
|
|
1415
|
-
|
|
1416
|
-
getState
|
|
1417
|
-
}) => {
|
|
1167
|
+
this.middleware = controller => {
|
|
1168
|
+
this.controller = controller;
|
|
1418
1169
|
return next => action => {
|
|
1419
1170
|
switch (action.type) {
|
|
1420
1171
|
case SUBSCRIBE_TYPE:
|
|
1421
1172
|
try {
|
|
1422
|
-
this.handleSubscribe(action
|
|
1173
|
+
this.handleSubscribe(action);
|
|
1423
1174
|
} catch (e) {
|
|
1424
1175
|
console.error(e);
|
|
1425
1176
|
}
|
|
1426
1177
|
return Promise.resolve();
|
|
1427
1178
|
case UNSUBSCRIBE_TYPE:
|
|
1428
|
-
this.handleUnsubscribe(action
|
|
1179
|
+
this.handleUnsubscribe(action);
|
|
1429
1180
|
return Promise.resolve();
|
|
1430
1181
|
default:
|
|
1431
1182
|
return next(action);
|
|
@@ -1444,49 +1195,25 @@ class SubscriptionManager {
|
|
|
1444
1195
|
/** Called when middleware intercepts 'rest-hooks/subscribe' action.
|
|
1445
1196
|
*
|
|
1446
1197
|
*/
|
|
1447
|
-
handleSubscribe(action
|
|
1448
|
-
|
|
1449
|
-
if (
|
|
1450
|
-
const
|
|
1451
|
-
|
|
1452
|
-
} = action;
|
|
1453
|
-
const {
|
|
1454
|
-
args
|
|
1455
|
-
} = action.meta;
|
|
1456
|
-
options = {
|
|
1457
|
-
schema: endpoint.schema,
|
|
1458
|
-
fetch: () => endpoint(...args),
|
|
1459
|
-
frequency: endpoint.pollFrequency,
|
|
1460
|
-
key: endpoint.key(...args),
|
|
1461
|
-
getState
|
|
1462
|
-
};
|
|
1463
|
-
} else {
|
|
1464
|
-
var _action$meta$options;
|
|
1465
|
-
options = {
|
|
1466
|
-
key: action.meta.key,
|
|
1467
|
-
frequency: (_action$meta$options = action.meta.options) == null ? void 0 : _action$meta$options.pollFrequency,
|
|
1468
|
-
schema: action.meta.schema,
|
|
1469
|
-
fetch: action.meta.fetch,
|
|
1470
|
-
getState
|
|
1471
|
-
};
|
|
1472
|
-
}
|
|
1473
|
-
if (options.key in this.subscriptions) {
|
|
1474
|
-
this.subscriptions[options.key].add(options.frequency);
|
|
1198
|
+
handleSubscribe(action) {
|
|
1199
|
+
const key = action.meta.key;
|
|
1200
|
+
if (key in this.subscriptions) {
|
|
1201
|
+
const frequency = action.endpoint.pollFrequency;
|
|
1202
|
+
this.subscriptions[key].add(frequency);
|
|
1475
1203
|
} else {
|
|
1476
|
-
this.subscriptions[
|
|
1204
|
+
this.subscriptions[key] = new this.Subscription(action, this.controller);
|
|
1477
1205
|
}
|
|
1478
1206
|
}
|
|
1479
1207
|
|
|
1480
1208
|
/** Called when middleware intercepts 'rest-hooks/unsubscribe' action.
|
|
1481
1209
|
*
|
|
1482
1210
|
*/
|
|
1483
|
-
handleUnsubscribe(action
|
|
1484
|
-
var _action$meta$options2;
|
|
1211
|
+
handleUnsubscribe(action) {
|
|
1485
1212
|
const key = action.meta.key;
|
|
1486
|
-
const frequency = (_action$meta$options2 = action.meta.options) == null ? void 0 : _action$meta$options2.pollFrequency;
|
|
1487
1213
|
|
|
1488
1214
|
/* istanbul ignore else */
|
|
1489
1215
|
if (key in this.subscriptions) {
|
|
1216
|
+
const frequency = action.endpoint.pollFrequency;
|
|
1490
1217
|
const empty = this.subscriptions[key].remove(frequency);
|
|
1491
1218
|
if (empty) {
|
|
1492
1219
|
delete this.subscriptions[key];
|
|
@@ -1535,7 +1262,7 @@ const DEFAULT_CONFIG = {
|
|
|
1535
1262
|
*
|
|
1536
1263
|
* Options: https://github.com/reduxjs/redux-devtools/blob/main/extension/docs/API/Arguments.md
|
|
1537
1264
|
*
|
|
1538
|
-
* @see https://
|
|
1265
|
+
* @see https://dataclient.io/docs/api/DevToolsManager
|
|
1539
1266
|
*/
|
|
1540
1267
|
class DevToolsManager {
|
|
1541
1268
|
constructor(config, skipLogging) {
|
|
@@ -1584,7 +1311,7 @@ class DevToolsManager {
|
|
|
1584
1311
|
|
|
1585
1312
|
/** Handling network unauthorized indicators like HTTP 401
|
|
1586
1313
|
*
|
|
1587
|
-
* @see https://
|
|
1314
|
+
* @see https://dataclient.io/docs/api/LogoutManager
|
|
1588
1315
|
*/
|
|
1589
1316
|
class LogoutManager {
|
|
1590
1317
|
constructor({
|
|
@@ -1635,9 +1362,7 @@ exports.__INTERNAL__ = internal;
|
|
|
1635
1362
|
exports.actionTypes = actionTypes;
|
|
1636
1363
|
exports.applyManager = applyManager;
|
|
1637
1364
|
exports.createFetch = createFetch;
|
|
1638
|
-
exports.createReceive =
|
|
1365
|
+
exports.createReceive = createSet;
|
|
1639
1366
|
exports.createReducer = createReducer;
|
|
1640
1367
|
exports.initialState = initialState;
|
|
1641
|
-
exports.legacyActions = index;
|
|
1642
1368
|
exports.newActions = newActions;
|
|
1643
|
-
exports.reducer = reducer$1;
|