Coder Social home page Coder Social logo

avvio's Introduction

avvio   Build Status

Asynchronous bootstrapping made easy. Wait for all components/plugins to start, and then start your whole app.

avvio is fully reentrant and graph-based. You can load components/plugins within plugins, and be still sure that things will happen in the right order.

JavaScript Style Guide

Install

To install avvio, simply use npm:

npm install avvio --save

Example

The example below can be found here and ran using node example.js. It demonstrates how to use avvio to load functions / plugins in order.

'use strict'

const boot = require('avvio')()

boot
  .use(first, { hello: 'world' })
  .after((cb) => {
    console.log('after first and second')
    cb()
  })
  .use(third, (err) => {
    if (err) {
      console.log('something bad happened')
      console.log(err)
    }

    console.log('third plugin loaded')
  })
  .ready(function () {
    console.log('application booted!')
  })

function first (instance, opts, cb) {
  console.log('first loaded', opts)
  instance.use(second, cb)
}

function second (instance, opts, cb) {
  console.log('second loaded')
  process.nextTick(cb)
}

function third (instance, opts, cb) {
  console.log('third loaded')
  cb()
}

API


boot([instance], [started])

Start a boot sequence.

instance will be used as the first argument of all plugins loaded and use, after and ready  function will be added to that object, keeping the support for the chainable API:

const server = {}

require('avvio')(server)

server.use(function first (s, opts, cb) {
  // s is the same of server
  s.use(function second (s, opts, cb) {
    cb()
  }, cb)
}).after(function (cb) {
  // after first and second are finished
  cb()
})

Options:

  • expose: a key/value property to change how use, after and ready are exposed.

Events:

  • 'error'  if something bad happens
  • 'start'  when the application starts

The boot function can be used also as a constructor to inherits from.


app.use(func, [opts], [cb])

Loads a functions asynchronously. The function must have the signature:

function plugin (server, opts, done) {
  done()
}

done must be called only once.

Returns the instance on which use is called, to support a chainable API.

If you need to add more than a function and you don't need to use a different options object or callback, you can pass an array of functions to .use.

boot.use([first, second, third], opts, cb)

The functions will be loaded in the same order as they are inside the array.


app.after(func([done]), [cb])

Calls a functon after all the previously defined plugins are loaded, including all their dependencies. The 'start' event is not emitted yet.

boot.after(function (done) {
  done()
})

done must be called only once.

Returns the instance on which after is called, to support a chainable API.


app.ready(func([done]))

Calls a functon after all the plugins and after call are completed, but befer 'start' is emitted. ready callbacks are executed one at a time.

boot.ready(function (done) {
  done()
})

done must be called only once.

Returns the instance on which ready is called, to support a chainable API.


boot.express(app)

Same as:

const app = express()

boot(app, {
  expose: {
    use: 'load'
  }
})

Acknowledgements

This project was kindly sponsored by nearForm.

License

Copyright Matteo Collina 2016, Licensed under MIT.

avvio's People

Contributors

delvedor avatar mcollina avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.