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

txjs

v0.1.1

Published

Tx: chainable rollback-able routines

Downloads

32

Readme

Tx: chainable rollback-able routines

Tx implements rollback-able transactions with a Promise-based API.

Installation

npm install txjs

Usage

Creating a step

An initial transaction step is created by calling the Tx constructor:

const Tx = require('txjs')

let tx = new Tx(
  function foward() {
    // Do something that can be undone
  },
  function backward() {
    // Undo this step, a *later* step failed
  }
)

Or you can chain to a previous step, just as you would with a Promise:

let tx = new Tx(step1Forward, step1Backward)
tx.chain(step2Forward)
.chain(step3Forward, step3Backward)

Notice that the backward routine is optional. Keep in mind that if a step doesn't need rollback you can probably merge it with the next one. However, the possibility of not having a backward step is supported for flexibility.

Running a transaction

Once you're done setting up your chain, you can run it. You get a Promise for the result of the last step in the chain.

new Tx(function () {
  return 3
})
.run()
.then(function (value) {
  console.log(value) // 3
})

Each forward step is called with the return value of the previous one. If you're used to chaining promises, it should be seamless.

new Tx(function () {
  return 3
})
.chain(function (three) {
  return three + 1
})
.run()
.then(function (value) {
  console.log(value) // 4
})

If a step returns a Promise, it will be waited upon and its resolution value will be passed to the next step. If it's rejected, rollback will be triggered.

new Tx(function () {
  return Promise.resolve(3)
})
.chain(function (three) {
  return three + 1
})
.run()
.then(function (value) {
  console.log(value) // 4
})

When a step fails, besides starting the rollback chain upwards, the error that caused the rollback will be the rejection reason of the returned Promise.

new Tx(function () {
  return 3
})
.chain(function (three) {
  throw 'something ugly'
})
.run()
.catch(function (reason) {
  console.log(reason) // 'something ugly'
})

Rolling back

You may roll back a transaction by throwing an error (or rejecting a returned Promise) in your forward routine. The backward routine will receive the return value of the forward routine for its step. Keep in mind that such value might have mutated:


function createDirectoryInFilesystem() {
  const dirname = createRandomDirname()
  fs.mkdirSync(dirname)
  return dirname
}

function deleteDirectory(dirname) {
  // Backward routine gets the dirname returned by createDirectoryInFilesystem
  fs.rmdirSync(dirname)
}

new Tx(createDirectoryInFilesystem, deleteDirectory)
.chain(createFileInDirectory, deleteFile)
.chain(function () {
  throw new Error('Changed my mind, delete everything')
})
.run()

In the example, deleteFile will be called, then deleteDirectory. If a backward step returns a Promise, the previous step will be rolled back once it resolves.

Currently, if a backward step throws or returns a rejected Promise, rollback is aborted and no previous steps' backward routines are called from then on. This will be configurable in future versions (PRs welcome!).

TO-DO

  • [ ] Fail construction if forward step is not a function.
  • [ ] Save custom resolution value for step (e.g., to prevent mutation from screwing up data needed to roll back)
  • [ ] Commit: prevent rollback of steps "upwards" in the chain.
  • [ ] Access to the backward (rollback) promise chain.
  • [ ] Catch backward (rollback) errors. Provide configuration parameter for continuing or aborting rollback.
  • [ ] Code refactor.