0.2.0 • Published 9 years ago

promise-tiny v0.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
9 years ago

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

0.2.0

9 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago