taskbuffer/test/test.5.task.paramflow.ts

66 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-26 12:16:33 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-03-25 11:14:49 +00:00
import * as taskbuffer from '../ts/index.js';
2017-06-17 14:56:33 +00:00
2023-07-26 12:16:33 +00:00
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartdelay from '@push.rocks/smartdelay';
2017-06-17 14:56:33 +00:00
2019-09-05 09:35:26 +00:00
const flowTask1 = new taskbuffer.Task({
2017-06-17 14:56:33 +00:00
taskFunction: (x: number) => {
2019-09-05 09:35:26 +00:00
const done = smartpromise.defer();
2018-08-04 15:53:22 +00:00
console.log('flowTask1');
console.log(x);
done.resolve(x);
return done.promise;
2020-07-12 00:48:51 +00:00
},
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2019-09-05 09:35:26 +00:00
const flowTaskBuffered = new taskbuffer.Task({
2017-06-17 14:56:33 +00:00
taskFunction: (x: number) => {
2019-09-05 09:35:26 +00:00
const done = smartpromise.defer();
2018-08-04 15:53:22 +00:00
console.log('flowTask1');
console.log(x);
done.resolve(x);
return done.promise;
2017-06-17 14:56:33 +00:00
},
buffered: true,
2020-07-12 00:48:51 +00:00
bufferMax: 1,
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2019-09-05 09:35:26 +00:00
const flowTask2 = new taskbuffer.Task({
2017-06-17 14:56:33 +00:00
taskFunction: (x: number) => {
2019-09-05 09:35:26 +00:00
const done = smartpromise.defer();
2018-08-04 15:53:22 +00:00
console.log('flowTask2');
console.log(x);
done.resolve(x);
return done.promise;
2017-06-17 14:56:33 +00:00
},
2020-07-12 00:48:51 +00:00
preTask: flowTask1,
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2019-09-05 09:35:26 +00:00
const flowTask3 = new taskbuffer.Taskchain({
2020-07-12 00:48:51 +00:00
taskArray: [flowTask1, flowTask2],
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
tap.test('should let a value flow through a task', async () => {
2019-09-05 09:35:26 +00:00
const result = await flowTask1.trigger(12);
2022-03-25 11:14:49 +00:00
expect(result).toEqual(12);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect values to flow between tasks', async () => {
2019-09-05 09:35:26 +00:00
const result = await flowTask2.trigger(12);
2022-03-25 11:14:49 +00:00
expect(result).toEqual(12);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect values to flow between tasks when buffered', async () => {
2019-09-05 09:35:26 +00:00
const result = await flowTaskBuffered.trigger(12);
2022-03-25 11:14:49 +00:00
expect(result).toEqual(12);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2017-06-17 15:29:26 +00:00
tap.test('expect values to flow between tasks in Taskchain', async () => {
2019-09-05 09:35:26 +00:00
const result = await flowTask3.trigger(12);
2022-03-25 11:14:49 +00:00
expect(result).toEqual(12);
2018-08-04 15:53:22 +00:00
});
2017-06-17 14:56:33 +00:00
2018-08-04 15:53:22 +00:00
tap.start();