-
Notifications
You must be signed in to change notification settings - Fork 6
/
component.jsx
42 lines (36 loc) · 1019 Bytes
/
component.jsx
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
import useAsyncQueue from "./dist/use-async-queue";
import { useState, useEffect } from "react";
const makeTask = (id, delay) => {
return {
id,
task: () => {
return new Promise((resolve) =>
setTimeout(() => {
resolve(`${id} is done`);
}, delay)
);
},
};
};
export const Component = ({ items }) => {
const [doneItems, setDoneItems] = useState([]);
const done = (d) => {
setDoneItems((items) => [...items, d]);
};
const { add, stats } = useAsyncQueue({ concurrency: 2, done });
useEffect(() => {
items.forEach((item) => add(makeTask(item.id, item.delay)));
}, [items, add]);
const { numPending, numInFlight, numDone } = stats;
return (
<>
<span>pending: {numPending}</span>
<span>inFlight: {numInFlight}</span>
<span>done: {numDone}</span>
<span>total: {numPending + numInFlight + numDone}</span>
{doneItems.map((item) => (
<li key={item.id}>item done - {item.id}</li>
))}
</>
);
};