Skip to content

Commit

Permalink
feat(scheduler): scheduler -> interrupter
Browse files Browse the repository at this point in the history
- 增加task, job, parallel
  • Loading branch information
deot committed Dec 21, 2023
1 parent 6adcb8d commit 8cee40e
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 191 deletions.
7 changes: 5 additions & 2 deletions packages/index/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as Route from '@deot/helper-route';
import * as $ from '@deot/helper-dom';
import * as Is from '@deot/helper-is';

import { Scheduler } from '@deot/helper-scheduler';
import { Interrupter, Task, Job, Parallel } from '@deot/helper-scheduler';
import { Emitter } from '@deot/helper-emitter';
import { Resize } from '@deot/helper-resize';
import { Wheel } from '@deot/helper-wheel';
Expand Down Expand Up @@ -39,5 +39,8 @@ export {
Validator,
RegExps,
RuleHelper,
Scheduler
Interrupter,
Task,
Job,
Parallel
};
45 changes: 28 additions & 17 deletions packages/scheduler/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
# @deot/helper-scheduler

调度器
调度器 - 进行调度工作的程序

### `Task`
创建任务队列,即执行顺序,他是串行的

### `Job`
创建任务轮询队列,周期性执行脚本

### `Parallel`
创建并行任务执行,可控制并发数

### `Interrupter`
中断任务(也可以理解为等待某个任务的完成)

实现的目的:
这是一段代码使用`await scheduler`调度器
永远等待它,直到它上面有任何代码执行了`scheduler.next()`
这是一段代码使用`await interrupter`中断器
永远等待它,直到它上面有任何代码执行了`interrupter.next()`
不论它是同步,微任务,异步执行,之后才会执行它后面的代码

### `Scheduler`
```js
import { Scheduler } from '@deot/helper-scheduler';
import { Interrupter } from '@deot/helper-scheduler';
```
```js
// or
import { Scheduler } from '@deot/helper';
import { Interrupter } from '@deot/helper';
```

```js
const scheduler = Scheduler.of();
const interrupter = Interrupter.of();

scheduler.next();
await scheduler;
interrupter.next();
await interrupter;

Promise.resolve().then(scheduler.next);
await scheduler;
Promise.resolve().then(interrupter.next);
await interrupter;

setTimeout(scheduler.next);
await scheduler;
setTimeout(interrupter.next);
await interrupter;

try {
setTimeout(scheduler.nextWithError);
await scheduler;
setTimeout(interrupter.nextWithError);
await interrupter;
} catch {
setTimeout(scheduler.finish);
await scheduler;
setTimeout(interrupter.finish);
await interrupter;
}
```
Loading

0 comments on commit 8cee40e

Please sign in to comment.