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

voila-di

v3.0.2

Published

A library for javascript dependency injection

Downloads

7

Readme

Voilà, Dependency Injection

Build Status Test Coverage

A dependency injection library for async utility, modularity, and testability

Create the dependency injection container

import di from 'voila-di';

const container = di();

Configure a dependency graph and get values

import di from 'voila-di';

function getPromise(val) {
  return new Promise<any>(resolve => setTimeout(() => resolve(val), 50));
}

const container = di({
  // You can specify a value named 'a' and a function
  // whose parameters indicate named dependencies from
  // elsewhere in the graph
  a: (b, c, d) => getPromise(b + c + d + 1),
  // Or the function can return a value
  b(e) {
    return 2;
  },
  // Or you can directly specify a promise
  c: getPromise(5),
  // Or a non-function value
  d: 5,
  // Or do arbitrary imperative tasks
  e: () => console.log('e done before b'),
  // And it works with async/await
  async f() {
    const val = await getPromise(5);
    return val + 1;
  }
});

// It will lazily resolve only the transitive dependencies of the requested key
container.get('a').then(console.log);
// => e done before b
// => 13

// Or it can eagerly load the entire graph,
// Note: will only eagerly load if container.then or container.catch is called
const values = await container;
console.log(values);
// => { d: 5, e: undefined, b: 2, c: 5, f: 6, a: 13 }

Configure a dependency graph and ensure all the tasks are triggered once

import di from 'voila-di';

function printEventually(val) {
  return new Promise(resolve => {
    setTimeout(() => {
      console.log(val);
      resolve();
    }, 50);
  });
}

const container2 = di({
  a: (b, c) => printEventually('a done after b and c'),
  b: (c) => printEventually('b done after c'),
  c: () => printEventually('c done'),
  d: 5,
  e: (d) => d + 1
});

container2.getAll().then(console.log);
// => c done
// => b done after c
// => a done after b and c
// => { d: 5, e: 6, c: undefined, b: undefined, a: undefined }

Resolve a graph with resolvable circular dependencies

import di from 'voila-di';

const container = di({
  a(b) {
    return {
      print: () => 1 + b.provide,
      provide: 1
    };
  },
  b(c) {
    return {
      print: () => 1 + c.provide,
      provide: 2
    };
  },
  c(a) {
    return {
      print: () => 1 + a.provide,
      provide: 3
    };
  }
});

const { a, b, c } = await container;
console.log(`a: ${a.print()}, b: ${b.print()}, c: ${c.print()}`);
// => a: 3, b: 4, c: 2

Inspect a tree view of all of the dependencies in the graph using topiary to pretty print the tree

import di           from 'voila-di';
import * as topiary from 'topiary';

const container3 = di({
  service1: (service2, service3) => {},
  service2: (service3) => {},
  service3: () => {},
});

const tree = container3.getTree();
const treeView = topiary(tree, 'children', { sort: true });
console.log(treeView);

// container
//  ├─┬service1
//  │ ├─┬service2
//  │ │ └──service3
//  │ └──service3
//  ├─┬service2
//  │ └──service3
//  └──service3