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

simple-chainable

v0.2.0

Published

Simple object for queuing functions one after the other, and creating fluent interfaces

Downloads

64

Readme

node-simple-chainable

A small node module for queuing a series of functions one after another. Simple chainable allows you to queue up a bunch of functions (including nested chains!) and run each queued item after each finishes. It also works great as the building block for any fluent (chainable) object

Simple chain works very similiar to @substack's chainsaw module, but with a simpler implementation (for good or for bad). One of the major differences is that simple-chainable is merely and implementation of a chain queue and not specifically built for fluent interfaces (although i use it for that). It doesn't create an new chainable object for nested chains which means you can use this consistently for objects that inherit simple-chain.

Install

npm install simple-chainable

Options

simple-chain takes one option to its constructor: autoStart -> bool which will attempt to start the chain rolling when the first item has been added to the queue.

new SimpleChainable(true) //autoStart true

Auto start uses process.nextTick to ensure that all items are initially chained before it starts the queue (otherwise it would end before it started!).

Queuing functions

you can queue functions through the .add(item -> func) method and call the next item in the chain via .next. Generally you will want to call next() in each add() call.

var SimpleChain = require('simple-chainable')
  , chainable = new SimpleChain()
  , state = {};

// Add an item to the queue
chainable.add(function(){
    state.number = 5;
    chainable.next()
})

chainable.add(function(){
    state.number = state.number + 5;
    chainable.next()
})

chainable.add(function(){
    console.log(state.number); // => 10
})

console.log(state.number) // => undefined
chainable.next() // start chain

Nested Chains

You can also create nested chains through the .nest(cb -> func).

chainable.add(function(){
    state.number = 5
    chainable.next()
})

chainable.nest(function(){
    //no need to call .next() here

    if ( state.number === 5){
        chainable.add(function(){
            state.number = 10
        })
    }
})

chainable.next() // start chain

chainable.add(function(){
    console.log(state.number); // => 10
})

Fluent Interfaces

The most useful application of simple-chainable is in creating fluent, or chainable interfaces.

Create our chainable object

var SimpleChainable = require('./simple-chainable')
  , Chainer = function(chainable){
        this.chainable = chainable;
        this.state = {}
    }

Chainer.prototype.set = function(key, value){
    var self = this;

    this.chainable.add(function(){
        self.state[key] = value
        self.chainable.next()
    });
    return this
}
Chainer.prototype.multiple = function(key, a, b){
    var self = this;

    this.chainable.add(function(){
        self.state[key] = a * b
        self.chainable.next()
    });
    return this
}

Chainer.prototype.tap = function(cb){
    var self = this;
    self.chainable.nest(function(){
        cb.call(self, self.state)
    });
    return this
}

Now we can use our chainable object and see how it works

var chainable = new SimpleChainable()
  , chainer = new Chainer(chainable)


chainer
    .set('number', 5)
    .set('multiplier', 2)
    .tap(function(state){
        chainer
            .multiply('result', state.number, state.multiplier)
            .tap(function(state){
                console.log(state.result) // => 10
            })
    })