npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

step-cascade

v3.1.4

Published

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

Downloads

38

Readme

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!