npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chenpeiyuan/batch-executor

v1.1.9

Published

批量任务执行器

Downloads

121

Readme

@chenpeiyuan/batch-executor

批量任务执行器,支持任务的批量添加、执行、暂停、恢复和停止等操作。

安装

# 使用 npm
npm install @chenpeiyuan/batch-executor

# 使用 yarn
yarn add @chenpeiyuan/batch-executor

# 使用 pnpm
pnpm add @chenpeiyuan/batch-executor

基本使用

快速开始

import { DefaultBatchExecutor, type Event, type ExecuteTask } from '@chenpeiyuan/batch-executor'

// 1. 定义任务类型
type MyTask = {
  id: string
  name: string
}

// 2. 定义任务执行函数
const executeTask: ExecuteTask<MyTask, string> = async (task, context) => {
  const { wrapExecute } = context
  console.log(`处理任务: ${task.name}`)
  await wrapExecute(() => new Promise(resolve => setTimeout(resolve, 500)))
  return `完成: ${task.name}`
}

// 3. 创建执行器
const executor = new DefaultBatchExecutor<MyTask, Event, string>(executeTask)

// 4. 添加任务
executor.addTask('1', { id: '1', name: '任务1' })
executor.addTask('2', { id: '2', name: '任务2' })
executor.addTask('3', { id: '3', name: '任务3' })

// 5. 监听事件
executor.addListener('start', () => console.log('执行器开始执行'))
executor.addListener('close', () => console.log('执行器执行结束'))
executor.addListener('success', (event) => {
  console.log('执行器执行成功', (event as any).results)
})
executor.addListener('failure', (event) => {
  console.log('执行器执行失败', (event as any).error)
})
executor.addListener('task:success', (event) => {
  console.log(`任务成功: ${(event as any).context.task.name}`)
})
executor.addListener('task:failure', (event) => {
  console.log(`任务失败: ${(event as any).context.task.name}`)
})

// 6. 启动执行
executor.start()

基本用法

import { DefaultBatchExecutor, type ExecuteTask } from '@chenpeiyuan/batch-executor'

type MyTask = {
  id: string
  name: string
}

const executeTask: ExecuteTask<MyTask, string> = async (task, context) => {
  console.log(`执行任务: ${task.name}`)
  // 执行任务逻辑
  return `结果: ${task.name}`
}

const executor = new DefaultBatchExecutor<MyTask, any, string>(executeTask)

// 添加任务
executor.addTask('1', { id: '1', name: '任务1' })
executor.addTask('2', { id: '2', name: '任务2' })

// 启动执行
executor.start()

嵌套任务

import { DefaultBatchExecutor, type ExecuteTask } from '@chenpeiyuan/batch-executor'

type ParentTask = {
  id: string
  name: string
  children: ChildTask[]
}

type ChildTask = {
  id: string
  name: string
}

const executeChildTask: ExecuteTask<ChildTask, string> = async (task) => {
  console.log(`执行子任务: ${task.name}`)
  await new Promise(resolve => setTimeout(resolve, 200))
  return `子任务结果: ${task.name}`
}

const executeParentTask: ExecuteTask<ParentTask, string> = async (task) => {
  console.log(`执行父任务: ${task.name}`)
  
  // 创建子任务执行器
  const childExecutor = new DefaultBatchExecutor<ChildTask, any, string>(executeChildTask)
  
  // 添加子任务
  task.children.forEach(child => {
    childExecutor.addTask(child.id, child)
  })
  
  // 启动子任务执行
  await childExecutor.start()
  
  return `父任务结果: ${task.name}`
}

const parentExecutor = new DefaultBatchExecutor<ParentTask, any, string>(executeParentTask)

// 添加父任务
parentExecutor.addTask('1', {
  id: '1',
  name: '父任务1',
  children: [
    { id: '1-1', name: '子任务1-1' },
    { id: '1-2', name: '子任务1-2' }
  ]
})

// 启动执行
parentExecutor.start()

停止执行

import { DefaultBatchExecutor, type ExecuteTask } from '@chenpeiyuan/batch-executor'

type MyTask = {
  id: string
  name: string
}

const executeTask: ExecuteTask<MyTask, string> = async (task, context) => {
  const { wrapExecute } = context
  console.log(`执行任务: ${task.name}`)
  
  // 模拟长时间运行的任务
  for (let i = 0; i < 5; i++) {
    await wrapExecute(() => new Promise(resolve => setTimeout(resolve, 200)))
    console.log(`任务 ${task.name} 执行中... ${i + 1}/5`)
  }
  
  return `结果: ${task.name}`
}

const executor = new DefaultBatchExecutor<MyTask, any, string>(executeTask)

// 添加任务
executor.addTask('1', { id: '1', name: '任务1' })
executor.addTask('2', { id: '2', name: '任务2' })
executor.addTask('3', { id: '3', name: '任务3' })

// 监听事件
executor.addListener('start', () => console.log('执行器开始执行'))
executor.addListener('close', () => console.log('执行器执行结束'))
executor.addListener('success', (event) => {
  console.log('执行器执行成功', (event as any).results)
})
executor.addListener('failure', (event) => {
  console.log('执行器执行失败', (event as any).error)
})
executor.addListener('task:success', (event) => {
  console.log(`任务成功: ${(event as any).context.task.name}`)
})
executor.addListener('task:failure', (event) => {
  console.log(`任务失败: ${(event as any).context.task.name}`)
})

// 启动执行
executor.start()

// 3秒后停止执行
setTimeout(() => {
  console.log('停止执行')
  executor.stop()
}, 3000)

高级功能

暂停和恢复

import { DefaultBatchExecutor, type ExecuteTask } from '@chenpeiyuan/batch-executor'

type MyTask = {
  id: string
  name: string
}

const executeTask: ExecuteTask<MyTask, string> = async (task, context) => {
  const { wrapExecute } = context
  console.log(`执行任务: ${task.name}`)
  
  for (let i = 0; i < 5; i++) {
    await wrapExecute(() => new Promise(resolve => setTimeout(resolve, 200)))
    console.log(`任务 ${task.name} 执行中... ${i + 1}/5`)
  }
  
  return `结果: ${task.name}`
}

const executor = new DefaultBatchExecutor<MyTask, any, string>(executeTask)

// 添加任务
executor.addTask('1', { id: '1', name: '任务1' })
executor.addTask('2', { id: '2', name: '任务2' })

// 启动执行
executor.start()

// 2秒后暂停
setTimeout(() => {
  console.log('暂停执行')
  executor.pause()
}, 2000)

// 5秒后恢复
setTimeout(() => {
  console.log('恢复执行')
  executor.resume()
}, 5000)

API 文档

DefaultBatchExecutor

构造函数

new DefaultBatchExecutor<Task, Event, Result>(
  executeTask: ExecuteTask<Task, Result>,
  options?: BatchExecutorOptions
)

方法

  • addTask(id: string, task: Task): void - 添加任务
  • start(): Promise<Map<string, Result>> - 启动执行
  • stop(): void - 停止执行
  • pause(): void - 暂停执行
  • resume(): void - 恢复执行
  • addListener(event: string, listener: (event: Event) => void): void - 添加事件监听器
  • removeListener(event: string, listener: (event: Event) => void): void - 移除事件监听器

事件

  • start - 执行器开始执行
  • close - 执行器执行结束
  • success - 执行器执行成功
  • failure - 执行器执行失败
  • stop - 执行器停止
  • pause - 执行器暂停
  • resume - 执行器恢复
  • addTask - 添加任务
  • delTask - 删除任务
  • altTask - 更新任务
  • task:start - 任务开始
  • task:success - 任务成功
  • task:failure - 任务失败
  • task:close - 任务结束

运行示例

# 克隆仓库
git clone https://github.com/chenpeiyuan/batch-executor.git
cd batch-executor

# 安装依赖
pnpm install

# 运行示例
pnpm run build
node dist/examples/quick-start.js

测试

# 运行测试
pnpm test

# 运行测试并监听文件变化
pnpm test:watch

许可证

ISC