clasync
[Node.js] CLASses ASYNChronous
Your powerful asynchronous imperative code OOP framework.
Latest version: 0.11.12
npm i -S clasync
Lazy bone
Below is a bunch of intuitive stuff and main explanations on lifecycle of this framework.
dep.js
const {$} = require('clasync');
class Dep extends $ {
async init() {
const msec = this.$.msecForInit;
await this.$.delay(msec);
this.$.log(`Dep is ready in ${msec} msec`);
}
async final() {
await this.$.doCommonFinal(this.$.msecForCommonFinal);
const msec = this.msecForFinal;
await this.doFinal(msec);
this.$.log('Dep finished');
}
static async doCommonFinal(msec) {
await this.delay(msec);
this.$.log(`Dep common final in ${msec} msec`);
}
async doFinal(msec) {
await this.$.delay(msec);
this.$.log(`Dep final in ${msec} msec`);
}
}
Dep.msecForInit = 1000;
Dep.msecForCommonFinal = 1000;
module.exports = Dep;
index.js
const {App} = require('clasync');
const Dep = require('./dep');
class Main extends App {
static async configure() {
return {
dep: {msecForFinal: 1500}
};
}
async init(deps) {
await deps({
$dep: Dep.new(this.dep)
});
this.$.log('App and all its dependencies are ready');
}
async final(reason) {
this.$.log(`App finished. Reason: ${reason}`);
}
async main() {
this.$.log('Main started');
await this.$.delay(5000);
this.$.log('Main finished');
return 0;
}
}
Main.gracefulShutdownMsec = 6000;
module.exports = Main;
node .
Dep is ready in 1000 msec
App and all its dependencies are ready
Main started
Main finished
Dep common final in 1000 msec
Dep final in 1500 msec
Dep finished
App finished. Reason: 0
Launch again and after you see Main started
press Ctrl+C:
Dep is ready in 1000 msec
App and all its dependencies are ready
Main started
^CDep common final in 1000 msec
Dep final in 1500 msec
Dep finished
App finished. Reason: SIGINT
Main finished
Note the Reason: SIGINT
and still clean app shutdown.
Change Main.gracefulShutdownMsec = 1500;
and launch again. Note app CRITICAL
termination before finalizers complete:
Dep is ready in 1000 msec
App and all its dependencies are ready
Main started
Main finished
Dep common final in 1000 msec
--- CRITICAL --- 2018-09-07T08:51:06.097Z
Timed out waiting for finalizers
Completion of Dep final
is beyond the graceful shutdown time period.
Launch again and after Main started
press Ctrl+C twice. Application will terminate immediately.
Async Events
events.js
const {Emitter} = require('../clasync');
class Main extends Emitter {
static async configure() {
return {
};
}
async init() {
this.on('start', async ({msec}) => {
this.$.log('Main started');
await this.$.delay(msec);
}, 0);
this.on('start', async ({}) => {
return {stage1_handler1: 2};
}, 1);
this.on('start', () => ({stage1_handler2: 2}), 1);
this.on('start', async ({}) => undefined, 1);
this.on('start', () => ({stage2_handler1: 3}), 2);
this.$.log('App and all its dependencies are ready');
}
async final(reason) {
this.$.log(`App finished. Reason: ${reason}`);
}
async main() {
const eventResults = await this.emit('start', {msec: 5000});
this.$.log('Event result:', eventResults);
this.$.log('Main finished');
return 0;
}
}
module.exports = Main;
node events
App and all its dependencies are ready
Main started
Event result: { stage1_handler1: 2, stage1_handler2: 2 }
Main finished
App finished. Reason: 0
MOAR?
Documentation in progress... Hubs, promises, funtionals and much more...
Thanks
Staff.com