promise-tiny
v0.2.0
Published
Steps
Downloads
2
Readme
Steps
Installation
Steps can be used either in a Node.js environment or in a browser, to install for Node.js use the npm package manager:
npm install promise-tiny
Usage
class Steps {
/**
* Initial flag = 'Begin'
*/
constructor (env : Object = {}) { }
/**
* When flag === 'End',
* next : function(args : Array = []) || function(args : Array, env : Object)
*
* Otherwise,
* next : function(flag : String, args : Array = [])
*/
on (flag : String, func : function(next : Function, args : Array)) { }
}
Example
var Steps = require("promise-tiny/Steps");
class Count {
constructor() {
this._step = 0;
}
get step() {
return this._step;
}
set step(n) {
this._step = n;
}
}
new Steps(new Count)
.on('Begin', function(next) {
this.step++;
next('check', 'Begin');
})
.on('check', function(next, ...args) {
this.step++;
next('create', [].concat(args, 'check'));
})
.on('create', function(next, ...args) {
this.step++;
next('error', [].concat(args, 'create'));
})
.on('logout', function(next, ...args) {
this.step++;
next('End', [].concat(args, 'logout'));
})
.on('error', function(next, ...args) {
this.step++;
next('End', [].concat(args, 'error'));
})
.on('End', function(next, ...args) {
this.step++;
console.log('Steps: '+this.step, 'trace: '+[].concat(args, 'End').join('->'));
next('new Steps()', { id: '!Count', count: 0 });
})
.on('Begin', function(next, ...args) {
this.count++;
next('info', [].concat(args, 'Begin'));
})
.on('info', function(next, ...args) {
this.count++;
next('logout', [].concat(args, 'info'));
})
.on('logout', function(next, ...args) {
this.count++;
next('End', [].concat(args, 'logout'));
})
.on('error', function(next, ...args) {
this.count++;
next('End', [].concat(args, 'error'));
})
.on('End', function(next, ...args) {
this.count++;
console.log('Count: '+this.count, 'trace: '+[].concat(args, 'End').join('->'), this.id);
});
Result
Steps: 5 trace: Begin->check->create->error->End
Count: 4 trace: new Steps()->Begin->info->logout->End !Count
License
MIT © May xiaoya zhang