promise-tiny

0.2.0 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.0
    1
    • latest

Version History

Package Sidebar

Install

npm i promise-tiny

Weekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • may