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

iocjs

v0.0.2

Published

Trying to bring IOC in node.js (and later to browser).

Downloads

1

Readme

#IOC

Trying to bring IOC in node.js (and later to browser).

This IOC works on an applications level and act as a container for all your depedencies.

###Installing IOC

npm install iocjs

###Browser Version

https://raw.githubusercontent.com/dervalp/ioc/master/dist/ioc.js

###Preparing the IOC

In order to prepare the IOC, you need to have a "ioc.json" file in the root of your application.

This file will define all the dependencies of your application. It tries to follow the same syntax as the API defined below.

ioc.json

{
  "register" : [
    { "key": "TestService",  "target": "./services/testService.js", lifeTime: "singleton" },
    { "key": "TestService2", "target": "./services/testService2.js", factory: "./adapters/backbone.js" },
    { "key": "TestService3", "target": "./services/testService3.js", lazyForProperties: true }
  ],
  "when": [
    { "property": "Provider", "use": "TestService" }
  ]
}

###How it works

In order to use the IOC, the class needs to be registed and created by the IOC.

var IOC = require("IOC");

var app = IOC();

var myService = app.create("TestService");

###Using the API in code

####Registering

  var objectDefinition = function ( name ) {
    this.name = name;
  }

  IOC.register( "uniqueKeyForAService" )
     .define( objectDefinition );

###Creating

When you have the Object registered, here is how you create one:

  var myUniqueKeyForAService = IOC.create( "uniqueKeyForAService" );
  
  console.log( myUniqueKeyForAService.name ); //output undefined

####Constructor Injection

We can directly inject the object you want by analysing the parameters of your constructor.

Registring the service:

  IOC.register( "uniqueKeyForAService" )
     .define( objectDefinition )
     .inject( { name: "Test" } );

Creating the service:

  var myUniqueKeyForAService = IOC.create( "uniqueKeyForAService" );
  console.log( myUniqueKeyForAService.name ); //output "Test"

###Auto-Discoverability of your dependencies

By using our naming convention (prefix parameter "$"), you can automaticaly declare a dependency and defining which service to use.

Register a service:

  var serviceA = function ( ) {
    this.greeting = "Hello ";
  };

  IOC.register( "serviceA" )
     .define( serviceA );

We want the "serviceA" to be automatically injected to another object.

Registering the dependant service:

  var serviceB = function ( name, $greetingProvider //declaring the dependency ) {
    this.name = name;

    console.log( $greetingProvider.greeting + this.name + " !");
  };

  IOC.register( "serviceB" )
     .define( serviceB )
     .inject({
        name: "Marty";
     });

By convention, the IOC will only look at the parameters in the constructor which starts with a '$'.

Now that we have our 2 services registered, we need to bind the $greetingProvider with the serviceA.

  IOC.when( "greetingProvider" )
     .use( "serviceA" );

By default, you can directly use the name of your unique key prefix by a '$' sign when declaring your dependency

Note: when binding the parameter, you do not need the '$' sign

Creating your object:

var serviceB = IOC.create( "serviceB" ); //will output 'Hello Marty !'

###Property Injection

We also provide a way to directly inject properties.

To define a property dependency for your object, you just need to define a static property.

  var serviceA = function ( ) {
    this.greeting = "Hello ";
  };

  IOC.register( "serviceA" )
     .define( serviceA );

 var serviceB = function ( name ) {
    this.name = name;
  };

  serviceB.prototype.greet = function () {
    console.log( this.$greetingProvider.greeting + this.name + " !");
  }

  serviceB.$greetingProvider = void 0; //you can assign anything you want, we just need the key

  IOC.register( "serviceB" )
     .define( serviceB )
     .inject({
        name: "Marty";
     });

  IOC.when( "greetingProvider" )
     .use( "serviceA" );

  var serviceB = IOC.create( "serviceB" );
  serviceB.greet(); //will output 'Hello Marty !'

###Life time Management

IOC lets you decide what kind of lifetime you want for your object. Currently, to keep it simple, you have the choice between a singleton and an instance per dependency.

Singleton

  IOC.register( "serviceB" )
     .define( serviceB )
     .singleInstance( )
     .inject({
        name: "Marty";
     });

PerDependcy

  IOC.register( "serviceB" )
     .define( serviceB )
     .instancePerDependency( )
     .inject({
        name: "Marty";
     });

Default is instancePerDependency

lazyForProperties

When using property injection, you can defer the creation of the Service until something uses the Property.

  IOC.register( "serviceB" )
     .define( serviceB )
     .instancePerDependency( )
     .lazyForProperties()
     .inject({
        name: "Marty";
     });

###Factories

By default, we expect function as object for defining dependencies. We also support literal definition but it will be used as the prototype for the created object.

  IOC.register( "serviceDefinedByLiteral" )
     .define( {
      doSomething: function () {

      },
      $aDependency: function ( ) { } //just empty function for the key
     } )
     .inject({
        name: "Marty";
     });

The parameters passed to the inject method will be attached to the object.

Create your own

If you do not like our way of creating the Object, you can define your own factory method.

  var backboneAdapter = function ( definition, args ) {
    var model = Backbone.extend( definition );
    return new Model( args );
  };

  var objDefinition = {
    doSomething: function () { },
    $aDependency: function ( ) { } //just empty function for the key
  };

  IOC.register( "customFactory" )
     .define( objDefinition )
     .factory( backboneAdapter )
     .inject({
        name: "Marty";
     });

###Express Example

You can find an express example here: https://github.com/dervalp/ioc/tree/master/test/fakeExpress

###Support for browser

IOC supports the browser.

  function subModel( ) {
    this.sub = true;
  };

  function mainModel ( $subModel ) {
    this.main = true;
    this.subModel = $subModel;
  };
  
  var app = IOC({
    "register" : [
      { "key": "mainModel", "target": "mainModel" },
      { "key": "subModel",  "target": "subModel" }
    ]
  });

  var mainModel = app.create( "mainModel" );

###Conclusion

Of course, this is still an alpha version. The end goal is to provide an IOC container which will work seamlessly on browser and node.js. Moreover, we would like to add options to manage the lifetime of an object ( eager, lazy, singleton ) and a config file to quickly setup all the IOC.

To be continued...

###License

MIT