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

@csgis/di

v0.0.1-alpha.1

Published

[![Build Status](https://travis-ci.org/csgis/js-di.svg?branch=master)](https://travis-ci.org/csgis/js-di) [![codecov](https://codecov.io/gh/csgis/js-di/branch/master/graph/badge.svg)](https://codecov.io/gh/csgis/js-di)

Downloads

6

Readme

JavaScript Dependency Injection

Build Status codecov

A simple and lightweight JavaScript library for Dependency Injection/Inversion of Control (DI/IoC).

There are other libraries that are more powerful but also are more complex and with bigger goals (such as InversifyJS). We focus on simplicity and cleanest API possible.

Getting started

Install with npm:

npm install --save @csgis/di

or yarn:

yarn add @csgis/di

And use it in your module:

import di from '@csgis/di';

class Map {
  addLayer(opts) {
    // ...
  }
}

class OLMap extends Map {
  addLayer(opts) {
    // ...
  }
}

di.bind(Map, OLMap);

var map = di.get(Map);
map.addLayer({
  // ...
});

Examples

Binding literal values

di.bind('value', 42);

Binding singletons

class Map {
  addLayer(opts) {}
}
class OLMap extends Map {
  addLayer(opts) {
    // ...
  }
}

di.bind(Map, new OLMap());

Binding classes

class Map {
  addLayer(opts) {}
}
class OLMap extends Map {
  addLayer(opts) {
    /// ...
  }
}

di.bind(Map, OLMap);

Binding classes with injected members:

class Renderer {
  render() {}
}
class WebGLRenderer extends Renderer {
  render() {
    // ...
  }
}
class Map {
  addLayer(opts) {}
}
class OLMap extends Map {
  constructor() {
    super();
    this.$inject = {
      renderer: Renderer
    };
  }

  addLayer(opts) {
    // ...
    this.renderer.render();
  }
}


di.bind(Renderer, new WebGLRenderer());
di.bind(Map, OLMap);
di
.get(Map) // here the new OLMap instance has the renderer member injected with a the WebGLRenderer instance.
.addLayer({
  // ...
}); // this calls render in injected WebGLRenderer as part of the addLayer method in OLMap.

Injecting objects

class Map {
  addLayer(opts) {}
}
class OLMap extends Map {
  addLayer(opts) {
    // ...
  }
}

di.bind(Map, new OLMap());

var obj = {
  $inject = {
    mymap: Map
  }
}
di.inject(obj);

obj.mymap.addLayer({
  // ...
});

Removing all bindings:

di.reset();

API

bind(service, obj)

Binds a literal, object or class to a service.

  • service (string or class): If class, it checks that obj is either a subclass or an instance of service.

  • obj: It can be anything when service is a string.

    It must be a subclass or an instance of service when service is a class. If it's an instance, get will return the exact instance; if it's a class, it will return a new instance by calling the constructor with no params.

Throws DIError.

inject(obj)

Injects an existing object based on its $inject property.

  • obj An object with an $inject property. $inject is an object whose keys are property names and values are services.

Throws DIError.

get(service)

Gets a bound literal or object.

  • service: A string or class previously used with the bind function.

Throws DIError.

reset()

Removes all bindings.

DIError

Generic error for injecting problems. Extends Error. Can be imported by:

import { DIError } from '@csgis/di';