step-flow

1.1.1 • Public • Published

step-flow

step-flow is a lightweight(without any libraries and less than 200 lines) business processes control library that allows to easily manage business logic by step. step-flow using the syntax of middleware which is similar to express. It provides process control, step jumps, and unified error handling.

中文版文档

Build Status Build status codecov js-semistandard-style Node.js version license

Features

  • Simple and lightweight
  • Code coverage 100%
  • Support step name
  • Support step jump
  • Support asynchronous step flow
  • Supports error handling
  • Support context

Install

npm install --save step-flow

Usage

1. require step-flow

var Flow = require('step-flow');

2. create a flow

var flow = new Flow();

3. add step function

one step with multiple functions:

flow.use(
  'step1',
  function fn1 (ctx, next) {
    ctx.fn1 = true;
    next();
  },
  function fn11 (ctx, next) {
    ctx.fn11 = true;
    next();
  }
);

one step with one function:

flow
  .use('step2', function fn2 (ctx, next) {
    ctx.fn2 = true;
    // next();
  })
  .use(function fn3 (ctx, next) {
    ctx.fn3 = true;
  });

4. error handling

flow.catch(function (err) {
  console.log('flow error:', err);
});

5. run

var context = {};
 
flow.run(context)

API

StepFlow()


stepFlow.use([stepName]) ⇒ StepFlow

Add the steps and the corresponding function. If the specified steps already exist, these functions will be appended to this step. If it does not exist, create a new one.

Each function added here will receive the parameters (context, next, nextTo, data):

  • context: context object.
  • next(err[,data]): Execute the next function in step, and if it is not called, the next function will not be executed.
  • nextTo(step[,data]): Call this method and pass the step name, you can jump to the corresponding steps.
  • data: the data that the next(null, data) pass.

Only call next() will continue to execute the next function in the step. If a non-empty parameter err is passed, and the subsequent functions will not be executed. The error handling function set with catch(fn) will be executed. If you call next()/nextTo(), and passing the parameter data, the next function will receive this data. However, the functions that after the 'next function' will not receive this data, unless the 'next function' call next()/nextTo() and pass the data.

Param Type Default Description
[stepName] String 'default' The step name, if you omit this parameter, the default use is default


stepFlow.catch(fn) ⇒ StepFlow

Add error handling functions that will be executed when next (err) is called and a non-null err arguments are passed.

In addition, if an error occurs while running the method specified by the use() method, fn will also be executed and the error object will be passed to fn.

The fn will accept the parameter(err),err for the error message.

Param Type Description
fn function Error handling function


stepFlow.run(context, stepName) ⇒ StepFlow

Start to run the step functions. If the step name is specified, it will be executed from the corresponding step. If it is not specified, it will be executed from the first step.

Param Type Description
context Any Context object, the function of each step will accept this parameter
stepName String Start step name, starting from the first step by default

Running tests

npm test

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Authors

  • zdying - HTML/JavaScript/CSS/Node.js developer zdying

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Readme

Keywords

Package Sidebar

Install

npm i step-flow

Weekly Downloads

123

Version

1.1.1

License

MIT

Unpacked Size

36.6 kB

Total Files

19

Last publish

Collaborators

  • zdying