pQueue.js 681 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import PQueue from "p-queue";
  2. import { objAssign } from "./utils";
  3. export function usePQueue(customOption = {}) {
  4. const defaultOption = {
  5. concurrency: 6,
  6. autoStart: true,
  7. };
  8. const option = objAssign(defaultOption, customOption);
  9. const queue = new PQueue(option);
  10. async function buildQueue(tasks) {
  11. try {
  12. const results = await queue.addAll(tasks);
  13. return results;
  14. } catch (error) {
  15. console.error(error);
  16. stopQueue();
  17. throw error;
  18. }
  19. }
  20. function startQueue() {
  21. queue.start();
  22. }
  23. function stopQueue() {
  24. queue.pause();
  25. queue.clear();
  26. }
  27. return {
  28. buildQueue,
  29. startQueue,
  30. stopQueue,
  31. };
  32. }