-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.ts
409 lines (362 loc) · 11.4 KB
/
index.ts
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import {
Dispatch,
SetStateAction,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react';
// See https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
const useIsomorphicLayoutEffect =
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'
? useLayoutEffect
: useEffect;
// Assign current value to a ref and returns a stable getter to get the latest value.
// This way we are sure to always get latest value provided to hook and
// avoid weird issues due to closures capturing stale values...
// See https://github.com/facebook/react/issues/16956
// See https://overreacted.io/making-setinterval-declarative-with-react-hooks/
const useGetter = <T>(t: T) => {
const ref = useRef(t);
useIsomorphicLayoutEffect(() => {
ref.current = t;
});
return useCallback(() => ref.current, [ref]);
};
type UnknownResult = unknown;
// Convenient to avoid declaring the type of args, which may help reduce type boilerplate
//type UnknownArgs = unknown[];
// TODO unfortunately it seems required for now if we want default param to work...
// See https://twitter.com/sebastienlorber/status/1170003594894106624
type UnknownArgs = any[];
export type AsyncStateStatus =
| 'not-requested'
| 'loading'
| 'success'
| 'error';
export declare type AsyncState<R> =
| {
status: 'not-requested';
loading: false;
result: undefined;
error: undefined;
}
| {
status: 'loading';
loading: true;
error: undefined;
result: undefined;
}
| {
status: 'success';
loading: false;
error: undefined;
result: R;
}
| {
status: 'error';
loading: false;
error: Error;
result: undefined;
};
type SetLoading<R> = (asyncState: AsyncState<R>) => AsyncState<R>;
type SetResult<R> = (result: R, asyncState: AsyncState<R>) => AsyncState<R>;
type SetError<R> = (error: Error, asyncState: AsyncState<R>) => AsyncState<R>;
type MaybePromise<T> = Promise<T> | T;
type PromiseCallbackOptions = {
// Permit to know if the success/error belongs to the last async call
isCurrent: () => boolean;
// TODO this can be convenient but need some refactor
// params: Args;
};
export type UseAsyncOptionsNormalized<R> = {
initialState: (options?: UseAsyncOptionsNormalized<R>) => AsyncState<R>;
executeOnMount: boolean;
executeOnUpdate: boolean;
setLoading: SetLoading<R>;
setResult: SetResult<R>;
setError: SetError<R>;
onSuccess: (r: R, options: PromiseCallbackOptions) => void;
onError: (e: Error, options: PromiseCallbackOptions) => void;
};
export type UseAsyncOptions<R> =
| Partial<UseAsyncOptionsNormalized<R>>
| undefined
| null;
const InitialAsyncState: AsyncState<any> = {
status: 'not-requested',
loading: false,
result: undefined,
error: undefined,
};
const InitialAsyncLoadingState: AsyncState<any> = {
status: 'loading',
loading: true,
result: undefined,
error: undefined,
};
const defaultSetLoading: SetLoading<any> = _asyncState =>
InitialAsyncLoadingState;
const defaultSetResult: SetResult<any> = (result, _asyncState) => ({
status: 'success',
loading: false,
result: result,
error: undefined,
});
const defaultSetError: SetError<any> = (error, _asyncState) => ({
status: 'error',
loading: false,
result: undefined,
error: error,
});
const noop = () => {};
const DefaultOptions: UseAsyncOptionsNormalized<any> = {
initialState: options =>
options && options.executeOnMount
? InitialAsyncLoadingState
: InitialAsyncState,
executeOnMount: true,
executeOnUpdate: true,
setLoading: defaultSetLoading,
setResult: defaultSetResult,
setError: defaultSetError,
onSuccess: noop,
onError: noop,
};
const normalizeOptions = <R>(
options: UseAsyncOptions<R>
): UseAsyncOptionsNormalized<R> => ({
...DefaultOptions,
...options,
});
type UseAsyncStateResult<R> = {
value: AsyncState<R>;
set: Dispatch<SetStateAction<AsyncState<R>>>;
reset: () => void;
setLoading: () => void;
setResult: (r: R) => void;
setError: (e: Error) => void;
};
const useAsyncState = <R extends {}>(
options: UseAsyncOptionsNormalized<R>
): UseAsyncStateResult<R> => {
const [value, setValue] = useState<AsyncState<R>>(() =>
options.initialState(options)
);
const reset = useCallback(() => setValue(options.initialState(options)), [
setValue,
options,
]);
const setLoading = useCallback(() => setValue(options.setLoading(value)), [
value,
setValue,
]);
const setResult = useCallback(
(result: R) => setValue(options.setResult(result, value)),
[value, setValue]
);
const setError = useCallback(
(error: Error) => setValue(options.setError(error, value)),
[value, setValue]
);
return {
value,
set: setValue,
reset,
setLoading,
setResult,
setError,
};
};
const useIsMounted = (): (() => boolean) => {
const ref = useRef<boolean>(false);
useEffect(() => {
ref.current = true;
return () => {
ref.current = false;
};
}, []);
return () => ref.current;
};
type UseCurrentPromiseReturn<R> = {
set: (promise: Promise<R>) => void;
get: () => Promise<R> | null;
is: (promise: Promise<R>) => boolean;
};
const useCurrentPromise = <R>(): UseCurrentPromiseReturn<R> => {
const ref = useRef<Promise<R> | null>(null);
return {
set: promise => (ref.current = promise),
get: () => ref.current,
is: promise => ref.current === promise,
};
};
export type UseAsyncReturn<
R = UnknownResult,
Args extends any[] = UnknownArgs
> = AsyncState<R> & {
set: (value: AsyncState<R>) => void;
reset: () => void;
execute: (...args: Args) => Promise<R>;
currentPromise: Promise<R> | null;
currentParams: Args | null;
};
// Relaxed interface which accept both async and sync functions
// Accepting sync function is convenient for useAsyncCallback
const useAsyncInternal = <R = UnknownResult, Args extends any[] = UnknownArgs>(
asyncFunction: (...args: Args) => MaybePromise<R>,
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args> => {
// Fallback missing params, only for JS users forgetting the deps array, to prevent infinite loops
// https://github.com/slorber/react-async-hook/issues/27
// @ts-ignore
!params && (params = []);
const normalizedOptions = normalizeOptions<R>(options);
const [currentParams, setCurrentParams] = useState<Args | null>(null);
const AsyncState = useAsyncState<R>(normalizedOptions);
const isMounted = useIsMounted();
const CurrentPromise = useCurrentPromise<R>();
// We only want to handle the promise result/error
// if it is the last operation and the comp is still mounted
const shouldHandlePromise = (p: Promise<R>) =>
isMounted() && CurrentPromise.is(p);
const executeAsyncOperation = (...args: Args): Promise<R> => {
// async ensures errors thrown synchronously are caught (ie, bug when formatting api payloads)
// async ensures promise-like and synchronous functions are handled correctly too
// see https://github.com/slorber/react-async-hook/issues/24
const promise: Promise<R> = (async () => asyncFunction(...args))();
setCurrentParams(args);
CurrentPromise.set(promise);
AsyncState.setLoading();
promise.then(
result => {
if (shouldHandlePromise(promise)) {
AsyncState.setResult(result);
}
normalizedOptions.onSuccess(result, {
isCurrent: () => CurrentPromise.is(promise),
});
},
error => {
if (shouldHandlePromise(promise)) {
AsyncState.setError(error);
}
normalizedOptions.onError(error, {
isCurrent: () => CurrentPromise.is(promise),
});
}
);
return promise;
};
const getLatestExecuteAsyncOperation = useGetter(executeAsyncOperation);
const executeAsyncOperationMemo: (...args: Args) => Promise<R> = useCallback(
(...args) => getLatestExecuteAsyncOperation()(...args),
[getLatestExecuteAsyncOperation]
);
// Keep this outside useEffect, because inside isMounted()
// will be true as the component is already mounted when it's run
const isMounting = !isMounted();
useEffect(() => {
const execute = () => getLatestExecuteAsyncOperation()(...params);
isMounting && normalizedOptions.executeOnMount && execute();
!isMounting && normalizedOptions.executeOnUpdate && execute();
}, params);
return {
...AsyncState.value,
set: AsyncState.set,
reset: AsyncState.reset,
execute: executeAsyncOperationMemo,
currentPromise: CurrentPromise.get(),
currentParams,
};
};
// override to allow passing an async function with no args:
// gives more user-freedom to create an inline async function
export function useAsync<R = UnknownResult, Args extends any[] = UnknownArgs>(
asyncFunction: () => Promise<R>,
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args>;
export function useAsync<R = UnknownResult, Args extends any[] = UnknownArgs>(
asyncFunction: (...args: Args) => Promise<R>,
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args>;
export function useAsync<R = UnknownResult, Args extends any[] = UnknownArgs>(
asyncFunction: (...args: Args) => Promise<R>,
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args> {
return useAsyncInternal(asyncFunction, params, options);
}
type AddArg<H, T extends any[]> = ((h: H, ...t: T) => void) extends ((
...r: infer R
) => void)
? R
: never;
export const useAsyncAbortable = <
R = UnknownResult,
Args extends any[] = UnknownArgs
>(
asyncFunction: (...args: AddArg<AbortSignal, Args>) => Promise<R>,
params: Args,
options?: UseAsyncOptions<R>
): UseAsyncReturn<R, Args> => {
const abortControllerRef = useRef<AbortController>();
// Wrap the original async function and enhance it with abortion login
const asyncFunctionWrapper: (...args: Args) => Promise<R> = async (
...p: Args
) => {
// Cancel previous async call
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
// Create/store new abort controller for next async call
const abortController = new AbortController();
abortControllerRef.current = abortController;
try {
// @ts-ignore // TODO how to type this?
return await asyncFunction(abortController.signal, ...p);
} finally {
// Unset abortController ref if response is already there,
// as it's not needed anymore to try to abort it (would it be no-op?)
if (abortControllerRef.current === abortController) {
abortControllerRef.current = undefined;
}
}
};
return useAsync(asyncFunctionWrapper, params, options);
};
// keep compat with TS < 3.5
type LegacyOmit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
// Some options are not allowed for useAsyncCallback
export type UseAsyncCallbackOptions<R> =
| LegacyOmit<
Partial<UseAsyncOptionsNormalized<R>>,
'executeOnMount' | 'executeOnUpdate' | 'initialState'
>
| undefined
| null;
export const useAsyncCallback = <
R = UnknownResult,
Args extends any[] = UnknownArgs
>(
asyncFunction: (...args: Args) => MaybePromise<R>,
options?: UseAsyncCallbackOptions<R>
): UseAsyncReturn<R, Args> => {
return useAsyncInternal(
asyncFunction,
// Hacky but in such case we don't need the params,
// because async function is only executed manually
[] as any,
{
...options,
executeOnMount: false,
executeOnUpdate: false,
}
);
};