cokoa
Koa meets dependency injection container
Last updated 2 years ago by alxarch .
ISC · Repository · Bugs · Original npm · Tarball
$ cnpm install cokoa 
SYNC missed versions from official npm registry.

cokoa

Koa meets dependency injection container

Usage


var app = require('cokoa')();

// Delegates to app.container.set()
app.set('db.host', 'localhost');
// Delegates to app.container.get()
app.get('db.host');
app.set('service', (c) => {
	// c is app.container
	return new Service();
});

// But real fun starts by registering a bundle
let bundle = {
	register: (c) => {
		c.set('foo.bar', 'bar');
		c.define('foo', ['foo.bar', (bar) => {
			console.log('Lazy load!');
			return {
				bar: () => bar
			};
		}]);
	},
	run: (app) => {
		return function *(next) {
			console.log('A');
			app.context.foo = app.get('foo');
			console.log(foo.bar());
			yield next;
			console.log('B');
		}
	}
};

app.register(bundle, {
	'foo.bar': 'baz'
});
// app.container.get('foo.bar') == 'baz'
app.register({
	register: (c) => {
		c.set('foo.bar', 'foo');
	},
	run: (c) => {
		return function *(next) {
			console.log('C');
			yield next;
			console.log('D');
		}
	}
});
// app.container.get('foo.bar') == 'foo'

app.run().then((app) => {
	console.log('done');
	console.log(app.context.foo.bar());
});

// Will log
// > A
// > Lazy load!
// > bar
// > C
// > D
// > B
// > done
// > foo

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (5)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |