@aegenet/belt-task-flow
v2.3.0
Published
Enables loosely coupled publish/subscribe messaging. Awaiting version with FIFO (first in first out)
Downloads
364
Readme
@aegenet/belt-task-flow
Enables loosely coupled publish/subscribe messaging. Awaiting version with FIFO (first in first out)
💾 Installation
yarn add @aegenet/belt-task-flow@^2.0.0
# or
npm i @aegenet/belt-task-flow@^2.0.0
📝 Usage
TaskFlow
import { TaskFlow, type ITaskFlow } from '@aegenet/belt-task-flow';
const taskFlow: ITaskFlow = new TaskFlow();
const tabs: string[] = [];
taskFlow.subscribe('Something', async () => {
await delay(200);
tabs.push('with delay');
});
taskFlow!.subscribe('Something', () => {
tabs.push('without delay');
});
await taskFlow!.publish('Something');
// tabs => ['with delay', 'without delay']
await taskFlow!.publish('Something');
// tabs => ['with delay', 'without delay', 'with delay', 'without delay']
taskFlow.dispose();
Listener & subscription
Note: Stage 3
decorator (for stage 2, you can use an older version (< 2.0.0) of this package).
import { TaskFlow, type ITaskFlow, TaskFlowListener, taskFlowMethod } from '@aegenet/belt-task-flow';
const taskFlow: ITaskFlow = new TaskFlow();
class ListenToMe extends TaskFlowListener {
public msg: string[] = [];
constructor() {
super(taskFlow!);
}
@taskFlowMethod()
public doIt() {
this.msg.push('hello');
}
}
// [...]
const listenToMe = new ListenToMe();
// publish
await taskFlow!.publish('tf.ListenToMe:doIt');
// listenToMe.msg => ['hello']
// [...]
listenToMe.dispose();
taskFlow.dispose();