@themost/events
v1.3.0
Published
Sync and async event emitters
Downloads
615
Readme
@themost/events
Sync and async event emitters for both browser and node.js
Usage
npm i @themost/events
AsyncSeriesEventEmitter
Use AsyncSeriesEventEmitter
for executing a collection of async event subscribers in series.
The following example demonstrates a before load event which executes event subscribers and continues.
const { AsyncSeriesEventEmitter } = require('@themost/events');
class UserAction {
constructor() {
this.beforeLoad = new AsyncSeriesEventEmitter();
}
async load() {
await this.beforeLoad.emit({
target: this
});
this.dateCreated = new Date();
}
}
(async function () {
const item = new UserAction();
item.beforeLoad.subscribe((event) => {
event.target.status = 'waiting';
});
item.beforeLoad.subscribe((event) => {
return new Promise((resolve) => {
// wait for something
setTimeout(() => {
event.target.status = 'active';
resolve();
}, 1000);
});
});
await item.load();
console.log('Loaded', 'status', item.status);
})().then(() => {
//
});
AsyncEventEmitter
Use AsyncEventEmitter
for executing a collection of async event subscribers in parallel.
const { AsyncEventEmitter } = require('@themost/events');
class UserAction {
constructor() {
this.beforeLoad = new AsyncEventEmitter();
}
async load() {
this.beforeLoad.emit({
target: this
});
this.dateCreated = new Date();
}
}
const item = new UserAction();
item.beforeLoad.subscribe((event) => {
event.target.status = 'waiting';
});
item.beforeLoad.subscribe((event) => {
return new Promise((resolve) => {
// wait for something
setTimeout(() => {
event.target.status = 'active';
resolve();
}, 1000);
});
});
item.load();
SyncSeriesEventEmitter
Use SyncSeriesEventEmitter
for executing a collection of sync event subscribers in series.
The following example demonstrates an after load event which executes event subscribers and continues.
const { SyncSeriesEventEmitter } = require('@themost/events');
class UserAction {
constructor() {
this.afterLoad = new SyncSeriesEventEmitter();
}
load() {
this.status = 'unknown';
this.afterLoad.emit({
target: this
});
}
}
const item = new UserAction();
item.afterLoad.subscribe((event) => {
event.target.status = 'waiting';
event.target.dateCreated = new Date();
});
item.afterLoad.subscribe((event) => {
if (event.target.status === 'waiting') {
event.target.status = 'active';
}
});
// perform load
item.load();
console.log('Loaded', 'status', item.status);
console.log('Loaded', 'dateCreated', item.dateCreated);
ProcessEventEmitter
Use ProcessEventEmitter
for sending and receiving process messages in both fork and cluster mode under node.js.
Import @themost/events/platform-server/register
in your startup script
import '@themost/events/platform-server/register'
If your application is running in cluster mode, each message received by the primary process will be forwarded to each worker of a cluster. This operation is very important when you are implementing shared services across cluster workers and enables the communication between of them.
Start sending and receiving messages:
new ProcessEventEmitter().emit(msg);
...
new ProcessEventEmitter().subscribe((value) => {
// write your code here
});