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

di-js

v4.0.1

Published

Simple JavaScript dependency injection

Downloads

4

Readme

DI-JS

Circle CI

DI-JS is a simple Dependency Injection for JavaScript for the Web. Unlike ES6's module, this is written to work with HTML Imports and libraries not using ES6 module.

One of the main features of DI-JS is the ability for the developer to override any bindings. This makes testing and writing customizable frameworks easier.

Installation

npm install --save-dev di-js

To use this, include this in your html file:

<script src="path/to/di-js/out/bin.js"></script>

Basic Usage

There are two main usages of DI-JS: Injecting and Binding

Injecting

Use the DIJS.run method to inject values. For example:

DIJS.run(
    function(require, optional) {
      var URL = require('global.URL');
      var iceCream = require('service.iceCream');
      var opt_config = optional('service.config');

      load('deps');

      // Code using URL and iceCream service
    });

In the example above, the function receives two functions:

  • require injects the given key. In this case, it injects service.iceCream and global.URL
  • optional optionally injects the service.config. If this value is not bound, it will return an undefined.

This function is called a "provider".

Binding

DI-JS supports two kinds of bindings: Global and Local bindings. Global bindings are done using the DIJS.bind method:

DIJS
    .bind(
        'service.iceCream',
        function(require, optional) {
          var http = require('service.http');

          var Service = function() { };
          Service.prototype.getFlavors = function() {
            http.get();
          };

          return Service;
        }]);

The first argument to DIJS.bind is the key to bind the value to. The second argument is a provider to run. Note that this function returns a Service. This is the value that will be bound to service.iceCream.

The second type of binding is local binding. This can be done using the DIJS.with method:

DIJS
    .with('urlPath', function(require, optional) {
      return require('service.location').href;
    })
    .bind('service.iceCream', function(require, optional) {
      var url = require('urlPath');
    });

Calling with returns a child scope with the given value bound. The value bound by the with method will only be available to descendants of that scope.

Another way to locally bind a value is using constant. This is just a shortcut to:

DIJS.with('urlPath', function(constant) {
  return constant;
}.bind(null, constant));

Running a program

Note that DI-JS lazily evaluates any providers. Calling DIJS.bind does not run the provider. The only time a provider is run is during injection or when calling DIJS.run:

DIJS.run(function(require, optional) {
  iceCream = require('service.iceCream');
  iceCream.getFlavors();
});

DIJS.run is the entry point of an application. Every code that depends on a bound value must run inside a provider. This ensures that the value is ready when it is used.

Calling run also creates a new "run context". A given run context will have its own bindings, even the global bindings. No bindings will be shared between two different run context. This will help to keep the global environment clean for testing.

Overriding values

One of the key features of DI-JS is the ability to override bound values. Recall that there are two kinds of bindings: global and local binding. There is a third type of binding called "run context" binding. A "run context" binding is a local binding who is an ancestor of a run context. For example:

DIJS
    .constant('run.context.binding', 2)
    .run(function() {});

When DI-JS resolves a key, it looks in three different places, in this order:

  1. The local bindings visible to the provider.
  2. The run context binding.
  3. The global binding.

This means that, from a run context, you can only override global bindings. For example:

DIJS
    .constant('a', 1)
    .bind('b', function() { return 2; })
    .bind('c', function(require) {
      require('a');  // 1
      require('b');  // 2000

      return 3;
    });

DIJS
    .constant('a', 1000)
    .constant('b', 2000)
    .run(function(require) {
      require('a'); // 1000
      require('b'); // 2000
      require('c'); // 3
    });

Note that the value of 'a' is different in run provider and in the provider of 'c'.