1 Star 1 Fork 0

sharp-feel / async-queue-progress

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
ISC

简介

这是一个有执行进度展示的异步任务队列,常用于批量抓取接口的任务执行和进度展示等... 借鉴于 single-line-log 库,在原基础上进行调整并封装了异步任务队列及执行进度展示

示例

const AsyncQueueProgress = require('async-queue-progress');
const taskQueue = AsyncQueueProgress({
  asyncMax: 5,
});

main();

function main() {
  taskQueue.progressBar.describe('加载中...');
  for (let i = 0; i < 100; i += 1) {
    taskQueue.add(task, `describe-${i}...`, `title-${i}:`, `data-${i}`);
  }

  taskQueue.start();
}

function task(data, index) {
  return new Promise((resolve) => {
    // 模拟完成任务
    setTimeout(() => {
      console.log(data);
      if (index === 30) {
        taskQueue.stop().then(() => {
          console.log('[stop ] end', new Date());
        });
      }
      resolve();
    }, 1000 * Math.random());
  });
}

文档

TaskQueue

  • progressBarProgressBar - 当前队列的进度条对象

  • config(opts: TaskQueue.Options) => void - 配置

  • resetasync () => void - 重置

  • clearasync (opts: TaskQueue.Options) => void - 清空

    • opts:配置项
  • add(promise, describe, title, data) => void - 添加一个任务

    • promise(data, index: Number) => Promise | async (data, index: Number) => void - 要执行的任务
    • describeString - 执行时显示的描述
    • titleString - 执行时显示的标题
    • dataString - 数据
  • start() => Promise - 开始/继续 执行任务

  • stop() => Promise - 暂停 执行任务

  • index() => Number - 当前执行的任务的下标

  • length() => Number -任务队列的长度

  • isStop() => Boolean - 是否未开始

  • isWillComplete() => Boolean - 是否即将完成

  • isComplete() => Boolean - 是否已经完成

TaskQueue.Options

  • asyncMaxNumber - 任务最大异步执行数,默认为 3

  • logBoolean - 是否显示日志,默认为 true

  • onComplete(isComplete: Boolean) => void - 完成回调,默认为 null

    • isCompleteBoolean - 是否完全的完成了,判断标准 index 等于 length
  • onStop(index: Number) => void - 暂停回调,默认为 null

    • indexNumber - 暂停时当前项的下标
  • onError(error: Error, task: Task) => void - 报错回调,默认为 null

    • errorError - 错误内容
    • taskTask - 报错的任务

TaskQueue.Task

  • promise(data, index: Number) => Promise | async (data, index: Number) => void - 要执行的任务

  • describeString - 执行时显示的描述

  • titleString - 执行时显示的标题

  • dataString - 数据

ProgressBar

  • config(opts: ProgressBar.Options) => this - 配置

  • title(val: String) => this - 设置标题

  • describe(val: String) => this - 设置描述

  • add(val: Number) => this - 增加进度值

    • valNumber - 要增加的进度值,默认 1
  • value(val: Number, append: Boolean) => this - 设置/增加 进度值

    • valNumber - 要 设置/增加 的进度值,默认 1
    • appendBoolean - false 设置,true 增加
  • total(val: Number, append: Boolean) => this - 设置/增加 总数值

    • valNumber - 要 设置/增加 的总数值,默认1
    • appendBoolean - false 设置,true 增加
  • isFull() => Boolean - 进度是否是 100%

  • render(force: Boolean) => void - 刷新进度条

    • forceBoolean - 是否强行刷新,否则按 Options.renderRate ms 刷新一次
  • quit() => void - 结束这个进度条,下次刷新绘制新的进度条

ProgressBar.Options

  • valueCharString - 进度填充字符,默认为 '#'

  • emptyCharString - 进度空白字符,默认为 ' '

  • lengthString - 进度条长度,默认为 50

  • titleString - 标题,默认为 'load:'

  • describeString - 描述,默认为 ''

  • totalNumber - 总值,默认为 0

  • valueNumber - 进度值,默认为 0

  • renderRateNumber - 刷新频率:ms/次,默认为 16

  • format(data: PercentData) => String - 将进度数据转换为字符串以显示,默认为:

    function format(data) {
      let title = `${data.title} ${(100 * data.percent).toFixed(3).substr(0, 5)}%`;
      if (data.describe) title = `${data.describe}\n${title}`;
      return `${title} [${data.cells}] [${data.percent * data.total}/${data.total}]\n`;
    },
The ISC License Copyright (c) @sharp-feel Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

简介

暂无描述 展开 收起
JavaScript
ISC
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/sharp-feel/async-queue-progress.git
git@gitee.com:sharp-feel/async-queue-progress.git
sharp-feel
async-queue-progress
async-queue-progress
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891