forked from contiamo/restful-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poll.tsx
383 lines (345 loc) · 10.4 KB
/
Poll.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import merge from "lodash/merge";
import * as React from "react";
import equal from "react-fast-compare";
import { InjectedProps, RestfulReactConsumer } from "./Context";
import { GetProps, GetState, Meta as GetComponentMeta } from "./Get";
import { composeUrl } from "./util/composeUrl";
import { processResponse } from "./util/processResponse";
import { constructUrl } from "./util/constructUrl";
import { IStringifyOptions } from "qs";
/**
* Meta information returned from the poll.
*/
interface Meta extends GetComponentMeta {
/**
* The entire response object.
*/
response: Response | null;
}
/**
* States of the current poll
*/
interface States<TData, TError> {
/**
* Is the component currently polling?
*/
polling: PollState<TData, TError>["polling"];
/**
* Is the initial request loading?
*/
loading: PollState<TData, TError>["loading"];
/**
* Has the poll concluded?
*/
finished: PollState<TData, TError>["finished"];
/**
* Is there an error? What is it?
*/
error: PollState<TData, TError>["error"];
}
/**
* Actions that can be executed within the
* component.
*/
interface Actions {
start: () => void;
stop: () => void;
}
/**
* Props that can control the Poll component.
*/
export interface PollProps<TData, TError, TQueryParams, TPathParams> {
/**
* What path are we polling on?
*/
path: GetProps<TData, TError, TQueryParams, TPathParams>["path"];
/**
* A function that gets polled data, the current
* states, meta information, and various actions
* that can be executed at the poll-level.
*/
children: (data: TData | null, states: States<TData, TError>, actions: Actions, meta: Meta) => React.ReactNode;
/**
* How long do we wait between repeating a request?
* Value in milliseconds.
*
* Defaults to 1000.
*/
interval?: number;
/**
* How long should a request stay open?
* Value in seconds.
*
* Defaults to 60.
*/
wait?: number;
/**
* A stop condition for the poll that expects
* a boolean.
*
* @param data - The data returned from the poll.
* @param response - The full response object. This could be useful in order to stop polling when !response.ok, for example.
*/
until?: (data: TData | null, response: Response | null) => boolean;
/**
* Are we going to wait to start the poll?
* Use this with { start, stop } actions.
*/
lazy?: GetProps<TData, TError, TQueryParams, TPathParams>["lazy"];
/**
* Should the data be transformed in any way?
*/
resolve?: (data: any, prevData: TData | null) => TData;
/**
* We can request foreign URLs with this prop.
*/
base?: GetProps<TData, TError, TQueryParams, TPathParams>["base"];
/**
* Any options to be passed to this request.
*/
requestOptions?: GetProps<TData, TError, TQueryParams, TPathParams>["requestOptions"];
/**
* Query parameters
*/
queryParams?: TQueryParams;
/**
* Query parameter stringify options
*/
queryParamStringifyOptions?: IStringifyOptions;
/**
* Don't send the error to the Provider
*/
localErrorOnly?: boolean;
}
/**
* The state of the Poll component. This should contain
* implementation details not necessarily exposed to
* consumers.
*/
export interface PollState<TData, TError> {
/**
* Are we currently polling?
*/
polling: boolean;
/**
* Have we finished polling?
*/
finished: boolean;
/**
* What was the last response?
*/
lastResponse: Response | null;
/**
* What data are we holding in here?
*/
data: GetState<TData, TError>["data"];
/**
* What data did we had before?
*/
previousData: GetState<TData, TError>["data"];
/**
* Are we loading?
*/
loading: GetState<TData, TError>["loading"];
/**
* Do we currently have an error?
*/
error: GetState<TData, TError>["error"];
/**
* Index of the last polled response.
*/
lastPollIndex?: string;
}
/**
* The <Poll /> component without context.
*/
class ContextlessPoll<TData, TError, TQueryParams, TPathParams = unknown> extends React.Component<
PollProps<TData, TError, TQueryParams, TPathParams> & InjectedProps,
Readonly<PollState<TData, TError>>
> {
public readonly state: Readonly<PollState<TData, TError>> = {
data: null,
previousData: null,
loading: !this.props.lazy,
lastResponse: null,
polling: !this.props.lazy,
finished: false,
error: null,
};
public static defaultProps = {
interval: 1000,
wait: 60,
base: "",
resolve: (data: any) => data,
queryParams: {},
};
private keepPolling = !this.props.lazy;
/**
* Abort controller to cancel the current fetch query
*/
private abortController = new AbortController();
private signal = this.abortController.signal;
private isModified = (response: Response, nextData: TData) => {
if (response.status === 304) {
return false;
}
if (equal(this.state.data, nextData)) {
return false;
}
return true;
};
private getRequestOptions = (url: string) =>
typeof this.props.requestOptions === "function"
? this.props.requestOptions(url, "GET")
: this.props.requestOptions || {};
// 304 is not a OK status code but is green in Chrome 🤦🏾♂️
private isResponseOk = (response: Response) => response.ok || response.status === 304;
/**
* This thing does the actual poll.
*/
public cycle = async () => {
// Have we stopped?
if (!this.keepPolling) {
return; // stop.
}
// Should we stop?
if (this.props.until && this.props.until(this.state.data, this.state.lastResponse)) {
this.stop(); // stop.
return;
}
// If we should keep going,
const { base, path, interval, wait, onError, onRequest, onResponse } = this.props;
const { lastPollIndex } = this.state;
const url = constructUrl(base!, path, this.props.queryParams, {
queryParamOptions: this.props.queryParamStringifyOptions,
stripTrailingSlash: true,
});
const requestOptions = await this.getRequestOptions(url);
const request = new Request(url, {
...requestOptions,
headers: {
Prefer: `wait=${wait}s;${lastPollIndex ? `index=${lastPollIndex}` : ""}`,
...requestOptions.headers,
},
});
if (onRequest) onRequest(request);
try {
const response = await fetch(request, { signal: this.signal });
if (onResponse) onResponse(response.clone());
const { data, responseError } = await processResponse(response);
if (!this.keepPolling || this.signal.aborted) {
// Early return if we have stopped polling or component was unmounted
// to avoid memory leaks
return;
}
if (!this.isResponseOk(response) || responseError) {
const error = {
message: `Failed to poll: ${response.status} ${response.statusText}${responseError ? " - " + data : ""}`,
data,
status: response.status,
};
this.setState({ loading: false, lastResponse: response, error });
if (!this.props.localErrorOnly && onError) {
onError(error, () => Promise.resolve(), response);
}
} else if (this.isModified(response, data)) {
this.setState(prevState => ({
loading: false,
lastResponse: response,
previousData: prevState.data,
data,
error: null,
lastPollIndex: response.headers.get("x-polling-index") || undefined,
}));
}
// Wait for interval to pass.
await new Promise(resolvePromise => setTimeout(resolvePromise, interval));
this.cycle(); // Do it all again!
} catch (e) {
// the only error not catched is the `fetch`, this means that we have cancelled the fetch
}
};
public start = () => {
this.keepPolling = true;
if (!this.state.polling) {
this.setState(() => ({ polling: true })); // let everyone know we're done here.
}
this.cycle();
};
public stop = () => {
this.keepPolling = false;
this.setState(() => ({ polling: false, finished: true })); // let everyone know we're done here.
};
public componentDidMount() {
const { path, lazy } = this.props;
if (path === undefined) {
throw new Error(
`[restful-react]: You're trying to poll something without a path. Please specify a "path" prop on your Poll component.`,
);
}
if (!lazy) {
this.start();
}
}
public componentWillUnmount() {
// Cancel the current query
this.abortController.abort();
// Stop the polling cycle
this.stop();
}
public render() {
const { lastResponse: response, previousData, data, polling, loading, error, finished } = this.state;
const { children, base, path, resolve } = this.props;
const meta: Meta = {
response,
absolutePath: composeUrl(base!, "", path),
};
const states: States<TData, TError> = {
polling,
loading,
error,
finished,
};
const actions: Actions = {
stop: this.stop,
start: this.start,
};
// data is parsed only when poll has already resolved so response is defined
const resolvedData = response && resolve ? resolve(data, previousData) : data;
return children(resolvedData, states, actions, meta);
}
}
function Poll<TData = any, TError = any, TQueryParams = { [key: string]: any }, TPathParams = unknown>(
props: PollProps<TData, TError, TQueryParams, TPathParams>,
) {
// Compose Contexts to allow for URL nesting
return (
<RestfulReactConsumer>
{contextProps => {
return (
<ContextlessPoll
{...contextProps}
{...props}
queryParams={{ ...contextProps.queryParams, ...props.queryParams }}
requestOptions={async (url: string, method: string) => {
const contextRequestOptions =
typeof contextProps.requestOptions === "function"
? await contextProps.requestOptions(url, method)
: contextProps.requestOptions || {};
const propsRequestOptions =
typeof props.requestOptions === "function"
? await props.requestOptions(url, method)
: props.requestOptions || {};
return merge(contextRequestOptions, propsRequestOptions);
}}
queryParamStringifyOptions={{
...contextProps.queryParamStringifyOptions,
...props.queryParamStringifyOptions,
}}
/>
);
}}
</RestfulReactConsumer>
);
}
export default Poll;