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

node-ioc

v2.1.0

Published

a simple ioc container

Downloads

396

Readme

NODE-IOC - Inversion of control Travis npm

Example

A container is a object which holds the relationships between various components and their life cycle. Create a container as follows ~

var Container = require('node-ioc')
var container = new Container()

Component Registration

Once a container is created we must register all the components and their factories. Registration is an important part of dependency injection and should all be at one place. So whenever there is a change in the component dependencies, or your container itself, you can just change it here.

API

  • registerInstance: Registers a value for a class. So whenever that class is being resolved, that value would be provided instead.

    class jQuery {}
    container.registerInstance(window.$).as(jQuery)
    
    container.resolve(jQuery) === window.$ // true
  • registerType: This would be the most common use case, where a single class depends on multiple classes. In the following case if you try to resolve SampleClass0, the container will instantiate SampleClass1 and SampleClass2 and then provide their instances to the constructor of SampleClass0.

    class SampleClass0{
      constructor (sc1, sc2){
        this.sc1 = sc1
        this.sc2 = sc2
      }
    }
    class SampleClass1{}
    class SampleClass2{}
    
    container.registerType(SampleClass1, SampleClass2).as(SampleClass0)
    
    const sc0 = container.resolve(SampleClass0)
    sc0.sc1 instanceof SampleClass1 // true
    sc0.sc2 instanceof SampleClass2 // true
    
  • register: This allows you to create a custom factory. Sometimes registerType and registerInstance are just not good enough. For instance —

    class CurrentDate {}
    class SampleClass {
      constructor (date) {
        console.log(date)
      }
    }
    container.register(() => new Date()).as(CurrentDate)
    container.registerType(CurrentDate).as(SampleClass)
    container.resolve(SampleClass) // Sat Jan 30 2016 12:57:29 GMT+0530 (IST)
    

    The register function's first param is the current instance of the container.

  • singleton: This implements the standard singleton pattern.

    class SampleClass {}
    container.register(new SampleClass()).as(SampleClass).singleton()
    
    const s0 = container.resolve(SampleClass)
    const s1 = container.resolve(SampleClass)
    s0 === s1 // true

Salient Features

  1. Detection for cyclic dependency
  2. Support for Container as a dependency using registerWithTypes
class SampleClass {}
container.registerWithTypes(Container).as(SampleClass)

In this case the current instance of Container will be passed as the first argument to the constructor of the SampleClass