-
Notifications
You must be signed in to change notification settings - Fork 2
/
producable.ts
41 lines (35 loc) · 937 Bytes
/
producable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Consumable, IConsumer } from "../src";
const sleep = (n: number) => new Promise((resolve) => setTimeout(resolve, n));
interface Task {
name: string;
desc: string;
succ?: boolean;
callback: {(succ: boolean): void}
}
class MyConsumer implements IConsumer<Task> {
async consume(tasks: Task[]) {
tasks.forEach((task) => {
setTimeout(() => task.callback(true), 1000);
});
}
};
const producable = new Consumable<Task>({
consumer: new MyConsumer(),
bufferSize: 10,
sizePerRound: 10
});
const addTasks = async() => {
while (true) {
await producable.putItem({
name: 'hello',
desc: 'world',
callback(succ) {
console.log('Recieve the result of the task:', succ);
}
});
await sleep(5);
}
}
addTasks()
.catch(e => console.error(e))
.then(() => producable.destroy());