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

revertable

v1.0.2

Published

a data structure for allowing side effects to be reversed

Downloads

5

Readme

Revertable

// place where we can show side effects
//
// -- this generally would be the fs or some
//    outside system
var state = {};

// creating a revertable is just creating a
// function to perform an action, and the inverse
var revertableInstance = new Revertable(
  function perform() {
    var key = Math.random();
    state[key] = 1;
    return Promise.resolve(key);
  }, function rollback(key) {
    delete state[key];
    return Promise.resolve(null);
  }
);

// revertables do not automatically perform their function
// so we should invoke it and wait for it to finish
var successfulPromise = revertableInstance.attempt();
var dataNeededToRevert;
successfulPromise.then(function success($data) {
  // often a task needs to produce data on how to undo
  // the side effects that it has created such as
  // where temporary files got saved

  // NOTE: this will not be equal to the data that was resolved
  //   during perform, this is to keep the data consumption of
  //   unfinished tasks identical to consuming results from 
  //   perform.
  dataNeededToRevert = $data;
  
  // since our task is stateless, we can use it to revert
  revertableInstance.revert(dataNeededToRevert);
}).catch(function failure(error) {
  // sometimes you can have errors during a revert()
  // if that is the case we maintain a way to continue
  // from the last point that was possible rather
  // than attempt to do a full revert
  //
  // this will be empty if there are no unfinished parts
  // of the task
  dataNeededToRevert = error.unfinished;
});

Series

Sometimes we need to create a revertable that performs multiple actions. We can do this by creating an array of revertables and using Revertable.series to turn them into a single revertable.

Revertable.series([makeDir, writeFile]).attempt();

Branch

Sometimes we have a large list of tasks that can be performed in parallel. We can support this by using Revertable.branch similar to series.

Revertable.branch([writeToCache, writeToDB]).attempt();