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

fireupdown

v1.0.0

Published

apply arbitrary up and down routines serially

Downloads

56

Readme

fireupdown

Apply sequences of up and down routines one after another as they finish. Ideal for initializing and tearing down asynchronous systems.

import { up, down } from 'fireupdown'

const systems = [
  // the routing system
  {
    rc: 0,
    up: (config) => startRouter(config).then(router => ({ router })),
    down: ({ router }) => stopRouter(router)
  },

  // the UI system which needs the router to function
  {
    rc: 1,
    up: (config, { router }) => (
      mountUI(router).then(component => ({ component }))
    ),
    down: ({ component }) => (
      unmountUI(component)
    )
  }
]

Then you can bring all the systems up where "routing system" (rc = 0) will have to finish before invoking the "UI" system:

// bring all systems up
up(systems)({ startingURL: '/' }).then(appState => {
  appState.hasOwnProperty('router')    // => true
  appState.hasOwnProperty('component') // => true
}, startFailure => {

})

Later you can bring all the systems down using the down routine:

down(systems)(appState).then(() => {
  // all systems down
}, stopFailure => {

})

Installation

Get it via NPM:

npm install --save fireupdown

The source is written to work in Node6+, but you may need to transpile it for older Browsers. It does not use ES6 modules, but requires Promise to be available.

API

The package exports three symbols: up, down, and ref.

.up

Bring all systems up starting from the lowest defined RC level. The signature is as follows:

function up(
  systems: Array.<System>
): function(...Any): Promise.<Object?>

Where System is defined as:

{
  // The "RC" defines the stage in which this system should be initialized.
  // 
  // Systems defined with the same RC are run in parallel and can not access
  // each other's output.
  // 
  // Systems defined with a higher RC will receive the output from systems
  // in lower RCs. 
  // 
  // Defaults to 0.
  ?rc: Number,

  // Hook to bring the system up. (optional)
  ?up: function(...Any): ?Promise.<Object>,

  // Hook to bring the system down. (optional)
  ?down: function(...Any): ?Promise.<Object>
}

To pass arguments to the system hooks you simply pass them to the function returned by up. They will be passed in the order you specify.

const systems = [
  {
    up: (a, b) => {
      // do something with a, b, or both
    }
  }
]

up(systems)(dep1, dep2)

The hooks will also receive a "state" object as the last parameter which is what is yielded to you when the promise resolves. You can use that object to maintain references to the system output either for use in the down routines to bring them down or if you need them for your application.

const systems = [
  {
    up: (a, state) => {
      // modify the state either for systems in higher RCs or for the end
      // result:
      return { foo: '1' }
    }
  }
]

up(systems)(dep1).then(state => {
  state.foo // => "1"
})

The state object is aggregated from all the return values of the hooks you're running. If the return value is not an object, the behavior is undefined.

const systems = [
  {
    up: () => ({ foo: 1 })
  },
  {
    up: () => ({ bar: 1 })
  },
  {
    rc: 1,
    up: state => ({ bar: state.bar + 1 })
  }
]

up(systems)().then(state => {
  state.foo // => 1
  state.bar // => 2
})

.down

Bring all systems down starting from the highest defined RC level.

This is the inverse operation of .up. The signature, parameters and return values are identical.

.ref

A convenience shortcut for associating the return value of a hook with a property on the state object.

What it really does is just this:

const ref = name => value => ({ [name]: value })

The following two hooks are exactly equivalent:

const up1 = () => produceSomething().then(x => ({ something: x }))
const up2 = () => produceSomething().then(ref('something'))

License

MIT