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

@micl/task

v0.1.23

Published

micl task

Downloads

48

Readme

@micl/task

npm version npm downloads license

Micl 项目的任务管理模块,提供任务中心客户端和本地任务管理功能。

📦 安装

npm install @micl/task
# or
pnpm add @micl/task
# or
yarn add @micl/task

📚 模块导出

| 导出项 | 类型 | 描述 | |--------|------|------| | taskCenterClient | TaskCenterClient | 任务中心客户端单例实例 | | TaskManager | TaskManager | 本地任务管理器 |

🎯 使用示例

TaskCenterClient 使用示例

import { taskCenterClient } from '@micl/task';
import { TASK_STATUS_ENUM } from '@micl/constant';

// 设置任务中心基础 URL
taskCenterClient.setBaseUrl('http://task-center:3000');

// 注册任务
const result = await taskCenterClient.register({
  taskId: 'task-123',
  taskName: 'Sample Task',
}, {
  headers: { 'Authorization': 'Bearer token' }
});

// 创建任务
const task = await taskCenterClient.create({
  taskId: 'task-123',
  params: { /* 任务参数 */ }
}, {
  headers: { 'Authorization': 'Bearer token' }
});

// 查询任务状态
const status = await taskCenterClient.status('task-123', {
  headers: { 'Authorization': 'Bearer token' }
});

// 中止任务
await taskCenterClient.abort('task-123', {
  headers: { 'Authorization': 'Bearer token' }
});

TaskManager 使用示例

import { TaskManager } from '@micl/task';

// 检查任务是否存在
const exists = TaskManager.hasTask('task-123');

// 获取或创建任务
const task = TaskManager.getTask('task-123');
console.log(task.taskId);

// 监听任务消息
task.emitter.on('message', (data) => {
  console.log('进度:', data.progress);
  console.log('消息:', data.message);
});

// 监听任务错误
task.emitter.on('error', (err) => {
  console.error('任务错误:', err);
});

// 下载文件
await task.download('http://example.com/file.zip', './file.zip');

// 发起 HTTP 请求
const { get, post } = task.request();
const data = await task.get('http://api.example.com/data');
await task.post('http://api.example.com/submit', { key: 'value' });

// 取消任务
task.cancel();

// 移除任务
TaskManager.removeTask('task-123');

队列轮询

import { taskCenterClient } from '@micl/task';
import { TASK_STATUS_ENUM } from '@micl/constant';

try {
  await taskCenterClient.queueTask(
    'task-123',
    {
      interval: 1000,
      maxWait: 60000,
      headers: { 'Authorization': 'Bearer token' }
    },
    (status) => {
      console.log('任务状态:', status.status);
      console.log('进度:', status.progress);
      console.log('消息:', status.message);

      if (status.status === TASK_STATUS_ENUM.Completed) {
        console.log('任务完成!');
      } else if (status.status === TASK_STATUS_ENUM.Failed) {
        console.log('任务失败!');
      }
    }
  );
} catch (error) {
  console.error('轮询超时:', error.message);
}

🛠 API 参考

TaskCenterClient

setBaseUrl(baseUrl)

设置任务中心基础 URL。

taskCenterClient.setBaseUrl('http://task-center:3000');

setApiMap(apiMap)

自定义 API 路径映射。

taskCenterClient.setApiMap({
  register: '/api/register',
  create: '/task/create',
  abort: '/task/abort',
  status: '/task/status',
  proxy: '/task/proxy',
});

register(data, options)

注册任务。

await taskCenterClient.register({ taskId: 'task-123' }, { headers: {} });

create(data, options)

创建任务。

await taskCenterClient.create({ taskId: 'task-123', params: {} }, { headers: {} });

status(taskId, options)

查询任务状态。

await taskCenterClient.status('task-123', { headers: {} });

abort(taskId, options)

中止任务。

await taskCenterClient.abort('task-123', { headers: {} });

proxy(data, options)

代理请求。

await taskCenterClient.proxy({ url: 'http://example.com' }, { headers: {} });

queueTask(taskId, options, callback)

轮询任务状态直到完成或失败。

await taskCenterClient.queueTask(
  'task-123',
  { interval: 500, maxWait: 60000, headers: {} },
  (status) => { /* 处理状态 */ }
);

TaskManager

hasTask(taskId?)

检查任务是否存在。

TaskManager.hasTask('task-123');

removeTask(taskId?)

移除任务。

TaskManager.removeTask('task-123');

getTask(id?)

获取或创建任务,返回任务对象。

const task = TaskManager.getTask('task-123');

返回对象:

| 属性 | 类型 | 描述 | |------|------|------| | taskId | string | 任务 ID | | emitter | EventEmitter | 事件发射器 | | caches | Record<string, any> | 任务缓存数据 | | cancel() | function | 取消任务 | | download(url, filepath) | function | 下载文件 | | request() | function | HTTP 请求工具 |

caches 使用示例:

const task = TaskManager.getTask('task-123');

// 存储数据
task.caches['key1'] = { name: 'test', data: [1, 2, 3] };
task.caches['result'] = await fetchResult();

// 读取数据
console.log(task.caches['key1']);

// 清除缓存
task.caches = {};

TaskManager.getTask().emitter

EventEmitter 实例,支持以下事件:

| 事件 | 描述 | 数据 | |------|------|------| | message | 任务消息 | { file, progress, message, stdout } | | error | 任务错误 | Error |

TaskManager.getTask().request()

返回 HTTP 请求工具。

const { get, post } = task.request();
await get<T>('http://api.example.com/data');
await post<T>('http://api.example.com/submit', data);

🤝 贡献

欢迎提交 Issue 和 Pull Request 来完善这个模块。

📄 许可证

本项目采用 ISC 许可证 - 查看 LICENSE 文件了解详情。

Copyright (c) alexgogoing [email protected]

📞 支持

如有问题或建议,请提交 Issue 或联系维护者。


@micl/task - 任务管理模块,简化任务流程 🚀