Skip to content

Commit 68bd59e

Browse files
committed
feat: 实现动态原子任务添加移除功能并增强任务上下文管理
新增原子任务动态添加和移除功能,支持在任务执行过程中动态调整任务队列 重构任务上下文管理,支持状态存储和函数式消息渲染 添加原子任务warning状态支持,完善任务状态流转 更新文档说明动态原子任务操作和上下文使用方法
1 parent 0f5c9c8 commit 68bd59e

File tree

14 files changed

+2438
-960
lines changed

14 files changed

+2438
-960
lines changed

README.en.md

Lines changed: 176 additions & 90 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 103 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ task-runner-plus 是一个轻量级的任务执行器库,用于实现任务的
1818
-**事件监听**:提供丰富的事件钩子,方便监听任务状态变化
1919
-**原子任务**:支持将复杂任务拆分为多个原子任务执行
2020
-**TypeScript 支持**:完整的 TypeScript 类型定义
21+
-**动态原子任务**:支持在任务执行过程中动态添加和移除原子任务
2122

2223
## 安装
2324

@@ -123,7 +124,7 @@ task.start();
123124

124125
### Task 类
125126

126-
#### 构造函数
127+
#### Task 构造函数
127128

128129
```typescript
129130
new Task(params?: Partial<TaskInfo>, options?: TaskOptions)
@@ -229,7 +230,7 @@ const task = new Task(
229230
// 自定义方法
230231
},
231232
},
232-
}
233+
},
233234
);
234235

235236
task.setAtomTasks([
@@ -243,7 +244,7 @@ task.setAtomTasks([
243244
{
244245
processMsg: "正在获取用户数据...",
245246
successMsg: "用户数据获取成功",
246-
}
247+
},
247248
),
248249
]);
249250

@@ -254,7 +255,7 @@ task.start();
254255

255256
原子任务是 Task 的执行单元,用于将复杂任务拆分为多个可管理的子任务。每个原子任务可以独立配置重试、超时等选项。
256257

257-
#### 构造函数
258+
#### AtomTask 构造函数
258259

259260
```typescript
260261
new AtomTask<Ctx>(
@@ -298,12 +299,13 @@ type AtomTaskExec<Ctx> = (input: {
298299

299300
#### 原子任务状态
300301

301-
| 状态名 | 描述 |
302-
| ----------- | -------- |
303-
| `PENDING` | 等待执行 |
304-
| `RUNNING` | 正在执行 |
305-
| `COMPLETED` | 执行完成 |
306-
| `FAILED` | 执行失败 |
302+
| 状态名 | 描述 |
303+
| ----------- | ---------------------- |
304+
| `PENDING` | 等待执行 |
305+
| `RUNNING` | 正在执行 |
306+
| `COMPLETED` | 执行完成 |
307+
| `FAILED` | 执行失败 |
308+
| `WARNING` | 警告状态,任务继续执行 |
307309

308310
#### 原子任务信息
309311

@@ -316,14 +318,51 @@ type AtomTaskExec<Ctx> = (input: {
316318
| `successMsg` | string? | 成功完成后的提示信息 |
317319
| `errorMsg` | string? | 失败时的错误提示信息 |
318320

319-
#### 方法
321+
#### AtomTask 方法
320322

321323
| 方法名 | 描述 | 参数 | 返回值 |
322324
| ------------------- | ---------------- | ------------------- | ----------------------- |
323325
| `getAtomTaskInfo()` | 获取原子任务信息 || `AtomTaskInfo` |
324326
| `run(ctx)` | 执行原子任务 | `ctx: TaskCtx<Ctx>` | `Promise<AtomTaskInfo>` |
325327

326-
### 动态添加原子任务
328+
### 任务消息支持函数调用上下文
329+
330+
任务消息支持函数形式,可访问执行上下文:
331+
332+
```typescript
333+
import { Task, AtomTask } from "task-runner-plus";
334+
335+
const task = new Task({ name: "Message Function Example" });
336+
337+
// 设置带有函数形式消息的原子任务
338+
task.setAtomTasks([
339+
new AtomTask({
340+
exec: async ({ ctx }) => {
341+
// 在上下文中存储数据
342+
ctx.set("userId", 123);
343+
ctx.set("userName", "John Doe");
344+
await new Promise((r) => setTimeout(r, 1000));
345+
},
346+
// 函数形式的processMsg,可访问ctx
347+
processMsg: (ctx) => {
348+
const userId = ctx.get("userId") || "unknown";
349+
return `正在处理用户 ${userId} 的任务...`;
350+
},
351+
// 函数形式的successMsg,可访问ctx
352+
successMsg: (ctx) => {
353+
const userName = ctx.get("userName") || "unknown";
354+
return `用户 ${userName} 的任务处理完成`;
355+
},
356+
}),
357+
]);
358+
359+
// 启动任务
360+
task.start();
361+
```
362+
363+
### 动态原子任务
364+
365+
#### 动态添加原子任务
327366

328367
支持在任务执行过程中动态添加新的原子任务:
329368

@@ -361,6 +400,51 @@ task.event.on("complete", () => {
361400
task.start();
362401
```
363402

403+
#### 动态移除原子任务
404+
405+
支持在任务执行过程中动态移除原子任务:
406+
407+
```typescript
408+
import { Task, AtomTask } from "task-runner-plus";
409+
410+
const task = new Task({ name: "动态移除任务示例" });
411+
412+
// 创建原子任务
413+
const atomTask1 = new AtomTask({
414+
exec: async ({ signal }) => {
415+
console.log("执行第一个任务");
416+
await new Promise((r) => setTimeout(r, 2000)); // 模拟耗时操作
417+
},
418+
processMsg: "正在执行第一个任务...",
419+
successMsg: "第一个任务完成",
420+
});
421+
422+
const atomTask2 = new AtomTask({
423+
exec: async ({ signal }) => {
424+
console.log("执行第二个任务");
425+
await new Promise((r) => setTimeout(r, 1000));
426+
},
427+
processMsg: "正在执行第二个任务...",
428+
successMsg: "第二个任务完成",
429+
});
430+
431+
// 设置原子任务
432+
task.setAtomTasks([atomTask1, atomTask2]);
433+
434+
// 启动任务
435+
task.start();
436+
437+
// 等待一段时间后移除第二个任务
438+
setTimeout(async () => {
439+
console.log("移除第二个任务");
440+
await task.removeAtomTasks([atomTask2.id]);
441+
}, 500);
442+
443+
// 等待任务完成
444+
await task.waitForEnd();
445+
// 只有第一个任务会执行完成,第二个任务会被移除
446+
```
447+
364448
### promiseFor
365449

366450
等待条件满足后返回结果的工具函数:
@@ -369,7 +453,7 @@ task.start();
369453
function promiseFor<T>(
370454
condition: () => boolean,
371455
result: () => T,
372-
options?: { timeout?: number; delay?: number }
456+
options?: { timeout?: number; delay?: number },
373457
): Promise<T>;
374458
```
375459

@@ -391,14 +475,14 @@ import { promiseFor } from "task-runner-plus";
391475
const element = await promiseFor(
392476
() => document.querySelector("#my-element") !== null,
393477
() => document.querySelector("#my-element"),
394-
{ timeout: 5000, delay: 100 }
478+
{ timeout: 5000, delay: 100 },
395479
);
396480

397481
// 等待异步数据加载完成
398482
const data = await promiseFor(
399483
() => store.isLoaded,
400484
() => store.data,
401-
{ timeout: 10000 }
485+
{ timeout: 10000 },
402486
);
403487
```
404488

@@ -407,25 +491,25 @@ const data = await promiseFor(
407491
### 安装依赖
408492

409493
```bash
410-
$ pnpm install
494+
pnpm install
411495
```
412496

413497
### 开发模式
414498

415499
```bash
416-
$ pnpm run dev
500+
pnpm run dev
417501
```
418502

419503
### 构建
420504

421505
```bash
422-
$ pnpm run build
506+
pnpm run build
423507
```
424508

425509
### 运行测试
426510

427511
```bash
428-
$ pnpm run test
512+
pnpm run test
429513
```
430514

431515
## 许可证

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "task-runner-plus",
3-
"version": "0.0.6",
3+
"version": "0.1.0",
44
"description": "Implement concurrent task execution, progress control, error retry, etc. 实现任务的并发执行、进度控制、错误重试等",
55
"main": "dist/cjs/index.js",
66
"module": "dist/es/index.js",

0 commit comments

Comments
 (0)