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

dismoi

v0.3.5

Published

dependency injection for javascript projects

Downloads

14,551

Readme

dismoi

Lightweight (less than 1kb of code) declarative Dependency Injection library for Javascript on any platform.

The dependency registry is centralized which leads to a good type inference and the ability to type check the dependency graph before runtime.

Installation

Nodejs:

npm install --save dismoi

Browser (CDN)

<script type="module">
    import {createProvider} from 'https://unpkg.com/[email protected]/src/index.js'
</script>

(replace 0.0.1 by the appropriate version)

Usage

Define your module

You define a registry of injectable items within a flat object whose keys (strings or Symbols) are the lookup tokens and values are factories to instantiate those items.

A factory must have the following signature

<T extends {}>(deps?: T) => any // returns an injectable

deps is an object providing the named dependency map of the injectable.

Alternatively it can be any value which gets automatically wrapped into a factory function.

const token = Symbol('something'); 
const injectables = {
    [token]: ({foo}) => { return 'whathever'},
    foo: ({externalThing, someValue}) => externalThing,
    someValue: 'something' // a value
}

the dependency graph of your module is the following:

  1. The injectable designed by the symbol token depends on foo
  2. foo depends on externalThing (not provided by the module) and someValue
  3. someValue always returns the string something

Factories can be decorated to adapt to any instantiation pattern:

import {fromClass, singleton} from 'dismoi';

const injectables = {
    foo: fromClass(class blah {
        constructor({depA}){};
    }),
    depA: singleton(someFactory) // make sure someFactory only instantiate once and then returns the same instance
}

How factories get registered in the module is left out: simple imports, to sophisticated class annotation system.

Create a provider

You pass the injectable registry to the createProvider function alongside with the injectable list you want to expose. It gives you a function to instantiate the module:

Example using the injectables aforementioned

import {createProvider} from 'dismoi';

const provide = createProvider({
    injectables,
    api:['foo']
});

You call the provide function to instantiate the module passing the missing dependencies in the graph, eventually overwriting some you have defined in the registry.

const moduleA = provide({
    someValue: 'otherValue', // overwrite
    externalThing: 42 // required
}) 

Then injectables get instantiated lazily when required through their getter.

A different instance is created each time, unless you have a "singleton" factory

const { foo } = services;
const otherFoo = services.foo;

An exception is thrown if some dependencies are not met.

See the extensive test suite for advanced usages.

Typescript support.

Typescript is well-supported and the compiler will throw if there are incompatible dependencies or if some are missing.

typescript compiler error