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

@ckaatz/container

v0.2.0

Published

Dependency injection container for javascript

Downloads

7

Readme

container

Build Status

Container is a library that allows you to specify dependencies, and then invoke functions that can use any of those previously specified dependencies. Dependencies are matched through the argument names of the function that is invoked.

Table of content

Install

node.js:

npm install @ckaatz/container --save

Usage Examples

Dependencies can have dependencies of themselves, see the example below:

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

container.add('settings', {
	logFile: '/var/log/mylog.log'
});
container.add('logger', function(settings, callback) {
	var Logger = require('logger');
	return callback(null, new Logger(settings.logFile));
});

container.invoke(function(logger) {
	logger.info('inside invoked function');
});

creating a container

To start using dependency injection, the first thing to do is to create a container to add all your dependencies to.

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

adding dependencies

Once having a container, you can add dependencies using container.add(name, value).

Invoking functions

order is not important, matching of arguments is done only by name, optional arguments have the postfix _optional. This means no error will be thrown when the argument name without the _optional postfix is not present in the container.

API

Container.add(name, value)

Add a value to the container with name. value can be a creator function that returns the dependency value through a callback. The argument has to be called callback, and the creator function can specify any number of other arguments that should match other dependencies in the container. In other words, creator functions are also dependency injected, with a special extra argument called callback.

The callback accepts to arguments, the first being an error if one occured or null if not, and the second one the value of the dependency if no error occurred.

container.add('a', function(anotherDependency, callback) {
	callback(null, 4);
});

If value is not a function, the given value will be returned when the dependency is requested.

container.add('port', 8080);
container.add('encryptionKey', 'asd8f9asf787s8dff9s8d');
container.add('today', new Date(1918, 10, 11));

Container.addAll(obj)

Add multiple values to the container giving an object where each property is added to the container with the given name and value. Example:

container.addAll({
	a: 5,
	b: 3
});

Container.addPath(name, path)

Add a given filepath to be hooked up to the container

container.addPath("module", "path/to/module.js");

Container.get(name, callback)

retrieve a previously added dependency before running a callback function Example:

container.get('neededDependency', function(err, neededDependency) {
    if(err) {
        return err;
    }
    container.on('error', function(err) {
        console.log('ERROR in DI container: ' + err.stack);
    });
});

Container.invoke(fn [, extraArguments], callback)

invoke a function handing in a needed dependency Example:

container.invoke(function(neededDependency) {
    a(neededDependency);
}, callback);

Container.invokeAll(obj [, extraArguments], callback)

invoke several dependencies at once to be able to use it in the callback

container.invokeAll({
		a: 5,
		b: 3
	},callback);

Container.bind(fn [, extraArguments], callback)

Container.bindAll(obj [, extraArguments], callback)

Container.create(path, callback)

Container.createAll(obj, callback)

error event on container

circular reference detection

Detects circular references and throws an error then

Future plans

  • add way to obtain dependency graph so it can be drawn in a diagram.