Skip to content

Asynchronous type resolution with dependencies

License

Notifications You must be signed in to change notification settings

bakerface/baggy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

baggy

npm package build code climate coverage issues dependencies devDependencies downloads

This package has been deprecated. Please use ziploc for new projects. The purpose of this package is to provide a way to resolve types asynchronously. Let's take a look at the simplest example:

var Bag = require('baggy');
var bag = new Bag();

bag.create('One')
  .from(function (done) {
    done(null, 1);
  });

bag.resolve('One', function (error, one) {
  console.log(one); // 1
});

We can also create dependencies on other types:

var Bag = require('baggy');
var bag = new Bag();

bag.create('One')
  .from(function (done) {
    done(null, 1);
  });

bag.create('Two')
  .from('One', function (one, done) {
    done(null, one + one);
  });

bag.create('Three')
  .from('One', 'Two', function (one, two, done) {
    done(null, one + two);
  });

bag.resolve('Three', function (error, three) {
  console.log(three); // 3
});

The syntax is nice if you have a collection of static functions, but what if you already have an object with methods? No worries. No need to repeat yourself.

var Bag = require('baggy');
var bag = new Bag();

bag.use({
  getOne: function (done) {
    done(null, 1);
  },

  getTwoFromOne: function (one, done) {
    done(null, one + one);
  },

  getThreeFromOneAndTwo: function (one, two, done) {
    done(null, one + two);
  }
});

bag.resolve('Three', function (error, three) {
  console.log(three); // 3
});

In some cases you will want to resolve a derived type without persisting its dependencies. This is also supported.

var Bag = require('baggy');
var bag = new Bag();

bag.create('Power')
  .from('Base', 'Exponent', function (base, exponent, done) {
    done(null, Math.pow(base, exponent));
  });

bag.given('Base', 2)
  .given('Exponent', 3)
  .resolve('Power', function (error, power) {
    console.log(power); // 8
  });

bag.given('Base', 3)
  .given('Exponent', 2)
  .resolve('Power', function (error, power) {
    console.log(power); // 9
  });

bag.resolve('Base', function (error) {
  console.error(error); // [TypeError: Base]
});

bag.resolve('Exponent', function (error) {
  console.error(error); // [TypeError: Exponent]
});

Pull requests and bug reports are welcome, as always.

About

Asynchronous type resolution with dependencies

Resources

License

Stars

Watchers

Forks

Packages

No packages published