12345678910111213141516171819202122232425262728293031323334353637 |
- import PQueue from "p-queue";
- import { objAssign } from "./utils";
- export function usePQueue(customOption = {}) {
- const defaultOption = {
- concurrency: 6,
- autoStart: true,
- };
- const option = objAssign(defaultOption, customOption);
- const queue = new PQueue(option);
- async function buildQueue(tasks) {
- try {
- const results = await queue.addAll(tasks);
- return results;
- } catch (error) {
- console.error(error);
- stopQueue();
- throw error;
- }
- }
- function startQueue() {
- queue.start();
- }
- function stopQueue() {
- queue.pause();
- queue.clear();
- }
- return {
- buildQueue,
- startQueue,
- stopQueue,
- };
- }
|