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

pdi

v4.0.2

Published

Minimal Promise based dependency injection framework

Downloads

840

Readme

PDI - Minimal Promise based Dependency Injection framework

Build Status

This is a simple library that provides a powerful abstraction for dealing with dependency injection both for system start up and for complex async tasks.

A simple example is as follows:

// File 1
const pdi = require("pdi")
pdi.add("db", () => DB.connect(options))

// File 2
const pdi = require("pdi")
pdi.add("dao", ["db"], ({ db }) => createDao(db))

// File 3
const pdi = require("pdi")
pdi.add("config", configObject)

// File 4
const pdi = require("pdi")
pdi.add(["dao", "config"], ({ dao, config }) => {
  createServer(dao, config)
})

pdi
  .start()
  .then(logSuccess)
  .catch(logError)

The api consists of:

add

Adds a factory function to the DI container. It offers a flexible API with 3 accepted signatures:

  • add(name, dependencies, factory) - Registers a named factory function with dependencies. The factory will only be called when the dependencies are available. They will be passed in as arguments to the factory function.
  • add(name, factory) - A simpler form of the above where there are no dependencies.
  • add(dependencies, factory) - Registers a factory without a name. This is for side-effect functions.

name must be a string

dependencies must be an array of strings

factory can be either a value or a factory function. If it is not a function it will be wrapped with the always function from Ramda. If the factory is asynchronous then it should return a promise.

NB This function will be called with a single argument which is an object containing the required dependencies.

This function will throw on the following conditions:

  • if the DI container has already started
  • if a factory has already been registered with the same name

start

This function accepts no arguments. It starts the DI container and returns a promise that will resolve when all factory functions have resolved.

The function will throw on the following conditions:

  • if start has already been called
  • if there is a missing dependency
  • if there is a circular dependency

The function sorts all previously registered factory functions according to the dependency graph. It then calls as many factory functions in parallel as possible. If any of the functions throw then the promise will reject.

clear

This function clears all previously registered functions, it is mainly useful for testing.

strict

If this function is called before activation, then there are checks made when calling the factory fucntion, the following will cause an error to be thrown

  • If the factory tries to access a property that hasn't been depended on
  • If a property is not accessed even though it has been depended on

create

This function is useful for where the DI container will be used to perform a particular operation, rather then for system start-up. It returns a DI container with the add, start, strict and clear methods.

While being small this library is powerful enough to be used for async flow control, for example:

const flow = pdi()
flow.add("body", req.body)
flow.add("userId", ["body"], path(["body", "userId"]))
flow.add("user", ["userId"], getUser)
flow.add("friends", ["user"], getFriends)
flow.add("result", ["friends", "user"], mergeFriendsAndUser)
flow
  .start()
  .then(({ result }) => res.send(result))
  .catch(err => res.sendStatus(500))

Extra utils for testing:

  • pdi._test.clear()
  • pdi._test.