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

Diogenes

v1.4.14

Published

A small Dependency Injection library for Javascript.

Downloads

6

Readme

Diogenes

A small Dependency Injection library for Javascript.

Introduction

Dependency injection is a way of creating objects within a Javascript application. Its advantages are that it helps to reduce object creation code boilerplate, making clode cleaner, and it facilitates testing by decoupling objects from their implementation.

I have written more generally on the subject of dependency injection on my blog.

I have also created a small demo app which uses DIogenes in order to show it working as part of an application. The code for this app is within the repository.

Note on terminology

In the context of dependency injection, an object taking the role of a dependency being injected into another object is known as a 'service'. The object receiving the service is known as the 'client'. Within the DI system, objects take the role of both service and client according to whether they are being injected or being injected into.

Installation

Use npm to pull package down from NPM.

    npm install --save Diogenes

CDN

The library can be downloaded from the following CDN:

https://unpkg.com/[email protected]/lib/injector.js

Demo

Here is a JSFiddle which shows a very simple working example.

demo app with old school constructors, Backbone, and RequireJS

Tutorial

Begin by importing the constructor function and using that to create an instance of the injector.

    let Diogenes = require('Diogenes');
    let injector = new Diogenes();

Dependency injection operates on POJSO (Plain Ol' Javascript Objects). Define constructor functions as normal, passing in any dependencies they have through function parameters.

    let Dep1 = function () {

        this.name = 'I am dep1';
    }

    let Dep2 = function () {

        this.name = 'I am dep2';
    }

    let Foo = function (options) {
        this.dep1 = options.dep1;
        this.dep2 = options.dep2;
    }
    Foo.inject = ['dep1', 'dep2'];

Tell the injector about these objects, or services, using the register() function. The first argument is the 'token' by which we refer to this service. The second argument is the 'service provider' which is an object that teaches the injector how to create the service. In this case, the service provider is the constructor function. The third argument is a constant which gives additional information to the injector as to how to create a service. Injector.INSTANCE tells the injector to create a new instance of the service every time a client object asks for it. Other constants are described below in the API section.

    injector.register('dep1', Dep1, Diogenes.INSTANCE );
    injector.register('dep2', Dep2, Diogenes.INSTANCE );
    injector.register('foo',  Foo,  Diogenes.INSTANCE );

An object declares its dependencies using annotations. In DI systems, annotations can take many different forms. In Diogenes, they consist of an array of string service tokens, as shown in the above example. This array is assigned to the static inject property of the constructor function.

    Foo.inject = ['dep1', 'dep2'];

Note also that property on the options object by which we access a service is also derived from the service token. For this reason, tokens should not contain characters that are illegal within Javascript properties.

The last step is to bootstrap the DI system. This is done using the start() method. The first argument to start() is a service token representing the object at the root of our dependency tree. The DI system will instantiate this object and all of its dependencies, and, by recursion, all the objects that make up the dependency tree. The second argument is a call back which is called once this process is completed. It is passed the aforementioned root object.

    injector.start('foo', function (foo) {
        console.log(foo.dep1.name, foo.dep2.name);
    });

API

Constants

These are passed to the register() method to give instructions to the injector as to how to create services.

| Name | Purpose | |-----------------------------|-------------------------------------------------| | Diogenes.INSTANCE | Specifies that the injector should return a new instance of the service on every request. The service provider must be a constructor function.| | Diogenes.CACHE_INSTANCE | Specifies that the injector should return the same instance of the service on every request.The service provider must be a constructor function. | | Diogenes.FACTORY_FUNCTION | Specifies that the injector should return the result of calling the service provider on every request.The service provider must be a function. | | Diogenes.VALUE | Specifies that the injector should return the service provider on every request. The service provider can be any type of javascript function, object, or primitive. |

Instance methods

register(token, serviceProvider, mode, locals)

Register a service with the injector. Optionally provide arguments to be passed to the service provider.

  • token - string identifier for this service
  • serviceProvider - object that teaches injector how to create the instance. Varies depending on the mode (see constants above)
  • mode - constant(see above) which provides additional info to the injector on how to create the dependency
  • locals - additional optional arguments that can be passed to the service provider. When the service worker is called these locals will be the second argument passed.

has(token)

Returns true if service has been registered on the injector.

  • token - string identifier for this service

get(token)

Returns service identified by token.

  • token - string identifier for this service

start(token, callback)

Bootstrapping method.

  • token - string identifier for the root object of dependency tree
  • callback - called once dependency tree has been created. Is passed an instance of the root object.