Code Monkey home page Code Monkey logo

step-cascade's Introduction

StepCascade

This is a small collection of classes which can be used to build a chain of "steps" all manipulating a common payload object.

Table of Contents

Usage

Let's say we have a payload like this:

type User = {
  firstname: string;
  lastname: string;
};

Then we can write steps that are typed to this payload:

class FirstnameStep extends AbstractCascadingStep<User> {
  run(payload: StringArrayPayload): StringArrayPayload {
    payload.firstname = "Solaire";
    return payload;
  }
}

class LastnameStep extends AbstractCascadingStep<User> {
  run(payload: StringArrayPayload): StringArrayPayload {
    payload.lastname = "of Astora";
    return payload;
  }
}

We can then use the StepCascade to add those steps and have them manipulate the payload:

const cascade = new StepCascade<User>().addStep({ step: new FirstnameStep() }).addStep({ step: new LastnameStep() });

const result = await cascade.run({} as User);

console.log(result.firstname); // Solaire
console.log(result.lastname); // of Astora

Error Handling

The default error handling behaviour is quite simple. If one of your steps throws an error, the rest of the steps don't get executed.

const cascade = new StepCascade<User>().addStep({ step: new ErrorCausingStep() }).addStep({ step: new LastnameSTep() }); // Will never get called

The error being thrown by the StepCascade will be wrapped in a StepError, which will include the current StepDescription.

However, you can identify recoverable errors, ie. errors that can be resolved by different user input, for example. If a recoverable error gets identified, the step will try again.

There are two possibilities of identifying recoverable errors.

Recoverable Errors by Step

AbstractCascadingStep provides a function identifyRecoverableError. If an error gets thrown while executing this step, the function will be called. If it returns true, the step will be repeated.

Recoverable Errors by Cascade

Alternatively, you can call the function setIdentifyRecoverableErrorFunction on the StepCascade:

const recover: TIdentifyRecoverableError = (error: any) => {
  return (error.message = "A certain error message");
};
stepCascade.setIdentifyRecoverableErrorFunction(recover);

If the function returns true, the StepCascade will retry the current step, regardless of which step it is.

Recoverable Callbacks

If the error is recoverable, the StepCascade will call a callback function you can provide with the setRecoverableCallback function. This is typically used to ask the user if they want to try again.

stepCascade.setRecoverableCallback((error: any) => {
  console.log("An error occured, do you want to try again?");
  /* user input */
});

Testing

Simply run "npm test".

Contribution

Pull Requests are more than welcome!

step-cascade's People

Contributors

joerncodes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.