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

async-ioc

v0.3.0

Published

IoC container with async support

Downloads

5

Readme

AsyncIoC for Node JS

Asyncronous IoC and dependency injection for Node JS.

Usage

First create a container:

var ioc = require('async-ioc').createContainer();

Then register services on that container:

ioc.register('service', someFunction);
ioc.register('anotherService', require('./anotherservice'));

You can specify dependencies for a service:

ioc.register('serviceThree', serviceThreeFactory, ['service', 'anotherService']);

Load an entire folder:

ioc.registerAll('/path/to/services');

...all registered services will be named by the file/folder unless the $implement attribute is set.

And start a service with dependency injection:

ioc.start('serviceThree');

You can also chain the methods:

require('async-ioc').createContainer()
	.register('service', someFunction)
	.register('anotherService', require('./anotherservice'))
	.register('serviceThree', serviceThreeFactory, ['service', 'anotherService'])
	.start('serviceThree')
	.then(function(serviceThree) {
		// do something with serviceThree
	});

Service Signature

The factory/function for the service should have the following signature:

var someService = function(services) {
	// ...
	// service initialization logic
	// ...

	return theService;
};

Typically you would expose this in a module:

module.exports = function(services) { };

... where services is an object containing the injected services

Attributes

$implement

Alias of $implements

$implements

Defines the service or interface name this module implements.

Type: String or Array[String]

Default: The file or folder name.

Notes: A module can implement multiple services or interfaces.

// implement a service or interface
module.exports = function(services) { };
module.exports.$implements = 'serviceName';
// implement multiple services or interfaces
module.exports = function(services) { };
module.exports.$implements = ['service1', 'service2'];

$inject

Defines the services to be injected for this service. These should be supplied as an array of string values containing the service names.

Default: No services ([])

module.exports = myService;
module.exports.$inject = ['dependencyOne', 'dependencyTwo'];

function myService(services) {
	services.dependencyOne.someMethod();
}

Asyncronous Services

Method 1: Promises

Simply return a promise from the service factory.

module.exports = myService;

function myService(services) {
	var deferred = Q.defer();

	// this service will finish loading in 1 second
	setTimeout(function() {
		deferred.resolve();
	},1000);

	return deferred.promise;
}

Method 2: start() method

Return an object that defines a .start() method that accepts a Node-style callback (ie: function(err)) to be called on completion. The start method will be called before injecting into the first service.

module.exports = myService;

function myService(services) {

	return {
		start: function(done) {

			// if an error occurs, call done() with the error
			if (someErrorOccurred) done(someErrorOccurred);

			// this service will finish loading in 1 second
			setTimeout(function() {
				done();
			},1000);
		}
	};

}

Shutdown / Stop process

TODO:

Method Summary

All container methods are chainable, except for get and start.

Still To Document

A lot to do!

  • debug
  • define
  • defineAll / defineFolder
  • get
  • logTo
  • recurseServices
  • recurseInterfaces
  • register
  • registerAll / registerFolder
  • reset
  • start
  • stopOn
  • stopTimeout