1.1.4 • Published 9 years ago

serviceable v1.1.4

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

Serviceable Build Status

A simple service container/locator.

Installation

npm install serviceable --save

Example

var Container = require('serviceable');
var container = new Container();

container.service('foo', function() {
	return {
		bar: 'baz'
	};
});

var foo = container.get('foo');
console.log('return %s', foo.bar);

container.get('foo', function(foo) {
	console.log('callback %s', foo.bar);
});

Output:

return baz
callback baz

API

new Container([provide])

Container.create([provide])

Arguments

  1. provide Function: An optional function to pass to the .provide() method.

Returns

A new Serviceable Container instance.

.service(name, generator)

Registers a service generator which will be called once the first time that .get(name) is called. The value returned by the generator will become the service value and future calls to .get(name) will return a referentially identical value.

Arguments

  1. name Any: The name of the service.
  2. generator Function(container)|String: A function or the source string of a function which returns the service value. The generator function container argument is a reference to the container on which the .service() method was called. If a source code string is given instead of a function, then a local free variable container will be defined when the source code is evaluated.

Alternatively, a single object can be passed to the function which will recursively call the method for each owned and iterable key/value pair.

Returns

A reference to the container.

.factory(name, generator)

Registers a factory generator which will be called every time that .get(name) is called. This is different than the .service() method in that each retrieval has the potential to return a completely separate value and the generator can be called more than once.

Arguments

  1. name Any: The name of the service.
  2. generator Function(container)|String: A function or the source of a function that will be invoked to get the service value every time .get(name) is called. The generator function container argument is a reference to the container on which the .factory() method was called. If a source code string is given instead of a function, then a local free variable container will be defined when the source code is evaluated.

Alternatively, a single object can be passed to the function which will recursively call the method for each owned and iterable key/value pair.

Returns

A reference to the container.

.literal(name, value)

Registers a literal value. The value will be returned whenever .get(name) is called. If the value is a function, a reference to the function will be returned and the function will not be called.

Arguments

  1. name Any: The name of the literal.
  2. value Any: The value to be returned whenever .get(name) is called.

Alternatively, a single object can be passed to the function which will recursively call the method for each owned and iterable key/value pair.

Returns

A reference to the container.

.provide(callback)

Invoke a function which will register one or more related services, factories, and literals. The callback function will be passed a reference to the container.

Arguments

  1. callback Function(container)|String: A function or the source of a function that will be invoked to register related services, factories, and literals. The generator function container argument is a reference to the container on which the .provide() method was called. If a source code string is given instead of a function, then a local free variable container will be defined when the source code is evaluated.

Returns

A reference to the container.

.get(names..., [callback])

Get the value of a service, factory, or literal by name.

You can pass multiple names to get more than one value at a time. If more than one name is given, an array of the resolved values will be returned.

You can pass the optional callback function to have the resolved values passed as arguments. This function will be called syncronously.

Arguments

  1. names String: One or more names of the services, factories, or literals.
  2. callback Function: (optional) A function that will be passed all the resolved services as arguments.

Returns

One or more service, factory, or literal values. If the name is not defined, then an Error will be thrown.

1.1.4

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago