Skip to content

Commit 82efc9f

Browse files
Accept items to run tasks on.
1 parent a86719a commit 82efc9f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

component.jsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const makeTask = (id, delay) => {
1414
};
1515
};
1616

17-
export const Component = () => {
17+
export const Component = ({ items }) => {
1818
const [doneItems, setDoneItems] = useState([]);
1919

2020
const done = (d) => {
@@ -24,10 +24,8 @@ export const Component = () => {
2424
const { add, stats } = useAsyncQueue({ concurrency: 2, done });
2525

2626
useEffect(() => {
27-
add(makeTask(1, 100));
28-
add(makeTask(2, 200));
29-
add(makeTask(3, 300));
30-
}, [add]);
27+
items.forEach((item) => add(makeTask(item.id, item.delay)));
28+
}, [items, add]);
3129
const { numPending, numInFlight, numDone } = stats;
3230

3331
return (

use-async-queue.test.jsx

+12-3
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,18 @@ describe("useAsyncQueue", () => {
300300

301301
describe("on mount", () => {
302302
it("should execute each task once", async () => {
303-
render(<Component />, {
304-
wrapper: StrictMode,
305-
});
303+
render(
304+
<Component
305+
items={[
306+
{ id: 1, delay: 100 },
307+
{ id: 2, delay: 200 },
308+
{ id: 3, delay: 300 },
309+
]}
310+
/>,
311+
{
312+
wrapper: StrictMode,
313+
}
314+
);
306315

307316
expect(screen.getByText("total: 3"));
308317
await waitFor(() => {

0 commit comments

Comments
 (0)