-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreact-hooks.ts
96 lines (86 loc) · 2.47 KB
/
react-hooks.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
import { useEffect, useState } from "react";
import { Push, Repeater, RepeaterBuffer, Stop } from "@repeaterjs/repeater";
// Repeaters are lazy, hooks are eager.
// We need to return push and stop synchronously from the useRepeater hook so
// we prime the repeater by calling next immediately.
function createPrimedRepeater<T>(
buffer?: RepeaterBuffer,
): [Repeater<T>, Push<T>, Stop] {
let push: Push<T>;
let stop: Stop;
const repeater = new Repeater<T>((push1, stop1) => {
push = push1;
stop = stop1;
// this value is thrown away
push(null as any);
}, buffer);
// pull and throw away the first value so the executor above runs
repeater.next();
return [repeater, push!, stop!];
}
export function useRepeater<T>(
buffer?: RepeaterBuffer,
): [Repeater<T>, Push<T>, Stop] {
const [tuple] = useState(() => createPrimedRepeater<T>(buffer));
return tuple;
}
export function useAsyncIter<T, TDeps extends any[]>(
callback: (deps: AsyncIterableIterator<TDeps>) => AsyncIterableIterator<T>,
deps: TDeps = ([] as unknown) as TDeps,
): AsyncIterableIterator<T> {
const [repeater, push] = useRepeater<TDeps>();
const [iter] = useState(() => callback(repeater));
useEffect(() => {
push(deps);
}, [push, ...deps]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(
() => () => {
if (iter.return != null) {
// TODO: handle return errors
iter.return().catch();
}
},
[iter],
);
return iter;
}
export function useResult<T, TDeps extends any[]>(
callback: (deps: AsyncIterableIterator<TDeps>) => AsyncIterableIterator<T>,
deps?: TDeps,
): IteratorResult<T> | undefined {
const iter = useAsyncIter(callback, deps);
const [result, setResult] = useState<IteratorResult<T>>();
useEffect(() => {
let mounted = true;
(async () => {
try {
while (mounted) {
const result = await iter.next();
if (mounted) {
setResult(result);
}
if (result.done) {
break;
}
}
} catch (err) {
if (mounted) {
setResult(() => {
throw err;
});
}
}
})();
return () => {
mounted = false;
};
}, [iter]);
return result;
}
export function useValue<T, TDeps extends any[]>(
callback: (deps: AsyncIterableIterator<TDeps>) => AsyncIterableIterator<T>,
deps?: TDeps,
): T | undefined {
const result = useResult(callback, deps);
return result && result.value;
}