Socket
Socket
Sign inDemoInstall

step-flow

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

step-flow - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

example.js

81

lib/flow.js

@@ -28,9 +28,14 @@ /**

*
* 每一个函数执行时都会接受到参数`(context, next, nextTo)`:
* 这里添加的每一个函数在执行时都会接收到参数`(context, next, nextTo, data)`:
*
* * `context`:上下文对象。
* * `next(err)`:只有调用这个方法后才能执行步骤中的下一个函数。当调用`next(err)`,并传递非空的`err`参数时,会调用错误处理函数。
* * `nextTo(step)`:调用这个方法并传递步骤名称,可以跳转到对应的步骤。
* * `next(err[,data])`:执行步骤中的下一个函数,如果不调用,不会执行下一个函数。
* * `nextTo(step[,data])`:调用这个方法并传递步骤名称,可以跳转到对应的步骤。
* * `data`:调用`next(null, data)`中传入的数据。
*
* 只有调用`next()`,才会继续执行步骤中的下一个函数。如果调用时,传入了非空的参数`err`,则后面的函数不再执行,使用`catch(fn)`设置的错误处理函数会被执行。
* 如果调用`next()`/`nextTo()`时,传递了参数`data`,**下一个**函数会接收到这个数据。
* 但是,下一个之后的的函数不会接收到这个数据,除非在下一个函数中再次调用`next()/nextTo()`时传递`data`。
*
*
* @param {String} [stepName='default'] 需要新建或者追加函数的步骤名称,如果省略这个参数,默认使用`default`

@@ -67,6 +72,7 @@ * @return {StepFlow}

* @param {Any} context 上下文对象,每个步骤的函数都会接受到这个参数
* @param {Error|String} 错误信息,如果调用`next()`的时候,第一个参数非空,则会执行错误处理函数。
* @param {Error|String} err 错误信息,如果调用`next()`的时候,第一个参数非空,则会执行错误处理函数。
* @param {Any} data 需要传递到下一个函数的数据
* @returns {StepFLow}
*/
next: function (context, err) {
next: function (context, err, data) {
if (err) {

@@ -83,3 +89,5 @@ this.runErrorHandlers(err);

if (this.flowIndex >= flow.length) {
// 当前步骤中的函数已经执行完毕
if (this.stepIndex < steps.length - 1) {
// 还有待执行的步骤
this.stepIndex += 1;

@@ -90,2 +98,3 @@ this.flowIndex = 0;

} else {
// 所有的函数已经执行完毕
return this;

@@ -96,3 +105,3 @@ }

var curr = flow[this.flowIndex++];
curr(context, this.next.bind(this, context), this.nextTo.bind(this, context));
curr(context, this.next.bind(this, context), this.nextTo.bind(this, context), data);

@@ -110,5 +119,6 @@ return this;

* @param {String} step 步骤名称
* @param {Any} [data] 需要传递到下一个函数的数据
* @returns {StepFLow}
*/
nextTo: function (context, step) {
nextTo: function (context, step, data) {
var steps = this.steps;

@@ -128,3 +138,3 @@ var stepIndex = steps.indexOf(step);

this.next(context, null);
this.next(context, null, data);

@@ -188,56 +198,1 @@ return this;

};
// var seq = new StepFlow();
// seq.use(
// 'step1',
// function fn1 (ctx, next) {
// console.log('fn1:', ctx);
// ctx.fn1 = true;
// next(null, { time: new Date() });
// },
// function fn11 (ctx, next) {
// console.log('fn11:', ctx);
// ctx.fn11 = true;
// next('fn11 has some error.');
// }
// );
// seq
// .use('step2', function fn2 (ctx, next) {
// console.log('fn2:', ctx);
// ctx.fn2 = true;
// next();
// })
// .use('step3', function fn3 (ctx, next) {
// console.log('fn3:', ctx);
// ctx.fn3 = true;
// next();
// })
// .use('step4', function fn4 (ctx, next) {
// console.log('fn4:', ctx);
// ctx.fn4 = true;
// });
// seq
// .use('step2', function fn21 (ctx, next) {
// console.log('fn21:', ctx);
// ctx.fn21 = true;
// next();
// })
// .use('step3', function fn31 (ctx, next) {
// console.log('fn31:', ctx);
// ctx.fn31 = true;
// next();
// })
// .catch(function (err) {
// console.log('steps has error: ', err);
// });
// var ctx = {
// initialized: true
// };
// seq.run(ctx, 'step1');
{
"name": "step-flow",
"version": "1.0.1",
"version": "1.0.2",
"description": "step flow",
"main": "lib/index.js",
"main": "lib/flow.js",
"scripts": {

@@ -7,0 +7,0 @@ "doc": "jsdoc2md lib/*.js",

@@ -97,9 +97,11 @@ # step-flow

* [StepFlow()](#StepFlow)
* [.use([stepName])](#StepFlow+use) ⇒ <code>[StepFlow](#StepFlow)</code>
* [.catch(fn)](#StepFlow+catch) ⇒ <code>[StepFlow](#StepFlow)</code>
* [.run(context, stepName)](#StepFlow+run) ⇒ <code>[StepFlow](#StepFlow)</code>
* [.use([stepName])](#StepFlow+use) ⇒ [<code>StepFlow</code>](#StepFlow)
* [.catch(fn)](#StepFlow+catch) ⇒ [<code>StepFlow</code>](#StepFlow)
* [.run(context, stepName)](#StepFlow+run) ⇒ [<code>StepFlow</code>](#StepFlow)
<a name="StepFlow+use"></a>
### stepFlow.use([stepName]) ⇒ <code>[StepFlow](#StepFlow)</code>
<br/>
### stepFlow.use([stepName]) ⇒ [<code>StepFlow</code>](#StepFlow)
添加步骤以及对应的函数。

@@ -109,8 +111,12 @@ 如果指定的步骤已经存在,这些函数将会追加到这个步骤中。

每一个函数执行时都会接受到参数`(context, next, nextTo)`:
这里添加的每一个函数在执行时都会接收到参数`(context, next, nextTo, data)`:
* `context`:上下文对象。
* `next(err)`:只有调用这个方法后才能执行步骤中的下一个函数。当调用`next(err)`,并传递非空的`err`参数时,会调用错误处理函数。
* `nextTo(step)`:调用这个方法并传递步骤名称,可以跳转到对应的步骤。
* `next(err[,data])`:执行步骤中的下一个函数,如果不调用,不会执行下一个函数。
* `nextTo(step[,data])`:调用这个方法并传递步骤名称,可以跳转到对应的步骤。
* `data`:调用`next(null, data)`中传入的数据。
只有调用`next()`,才会继续执行步骤中的下一个函数。如果调用时,传入了非空的参数`err`,则后面的函数不再执行,使用`catch(fn)`设置的错误处理函数会被执行。
如果调用`next()`/`nextTo()`时,传递了参数`data`,**下一个**函数会接收到这个数据。
但是,下一个之后的的函数不会接收到这个数据,除非在下一个函数中再次调用`next()/nextTo()`时传递`data`。

@@ -123,6 +129,8 @@ | Param | Type | Default | Description |

### stepFlow.catch(fn) ⇒ <code>[StepFlow](#StepFlow)</code>
<br/>
### stepFlow.catch(fn) ⇒ [<code>StepFlow</code>](#StepFlow)
添加错误处理函数,当调用`next(err)`,并传递非空的`err`参数时,会调用这些错误处理函数。
参数`fn`会接受到参数`(err)`, `err`为错误信息。
参数`fn`会接受到参数`(err)`, `err`为错误信息。

@@ -135,3 +143,5 @@ | Param | Type | Description |

### stepFlow.run(context, stepName) ⇒ <code>[StepFlow](#StepFlow)</code>
<br/>
### stepFlow.run(context, stepName) ⇒ [<code>StepFlow</code>](#StepFlow)
开始执行步骤函数。

@@ -138,0 +148,0 @@ 如果指定了步骤名称,将从对应的步骤开始执行。如果没有指定,则从第一个步骤开始执行。

@@ -13,6 +13,7 @@ var Flow = require('../lib/flow');

ctx.fn1 = true;
next();
next(null, 11);
},
function fn11 (ctx, next) {
function fn11 (ctx, next, nextTo, data) {
ctx.fn11 = true;
ctx.fn11Data = data;
next();

@@ -22,5 +23,18 @@ }

flow.use(
'step2',
function fn2 (ctx, next, nextTo, data) {
ctx.fn2Data = data;
next(null, 21);
},
function fn21 (ctx, next, nextTo, data) {
ctx.fn21Data = data;
next();
}
);
flow.run();
it('# should has default context', function () {
flow.run();
assert.notEqual(context, null);

@@ -30,2 +44,11 @@ assert.equal(context.fn1, true);

});
it('# should pass data to the next function', function () {
assert.equal(context.fn11Data, 11);
assert.equal(context.fn21Data, 21);
});
it('# should only pass data to the next function', function () {
assert.equal(context.fn2Data, undefined);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc