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

gift-box

v0.0.5

Published

dependency injection container for node

Downloads

247

Readme

Gift Box

Another dependency injection container for nodejs. Not particuarly fast, good, well designed, or anything else.

Installation

npm install [--save] gift-box

Concepts

This documenation needs so much adding to it...

  • Service : Anything we might want to instantiate/fetch. Can be an object/function/primitive/etc
  • ServiceProvider : A function that either returns a Service or a promise that resolves to Service
  • ServiceDefinition: a grouping of
  • Container : Our box that holds definitions of Services and their ServiceProviders and understands the relationships between them
  • Singleton Service : A service that is only instantiated once per container (not the same thing as singleton pattern)
  • Transient Service: A service that is instantiated every single time it is needed

Usage / API

Instantiating a container

It is preferably to use the factory method createContainer

const container = require('gift-box').createContainer()
// or if you need access to the underlying "class"
const Container = require('gift-box').Container
const myContainer = new Container()

addSingleton, addTransient

container.addSingleton('service', serviceProvider, ['array', 'of', 'named', 'services'])

Adds a definition for the named service to the container. Singleton services only get instantiated at most one per container. If multiple services depend on a Singleton service, each one will get the same instance.

container.addTransient('service', serviceProvider, ['array', 'of', 'named', 'services'])

Adds a definition for the named service to the container. Transient services get instantiated each time they are requested in a container. If multiple services depend on a Transient service, each one will get a new instance

  • service is the name of the service we are defining.
  • serviceProvider is a function as mentioned in concepts (ServiceProvider). This function will be supplied with one argument, a dependencies object, that is a plain javascript object containing all the requested dependencies.
  • ['array', 'of', 'named', 'services'] is an array of service names which this service is dependent on. This is optional.

The order of adding service definitions to the container does not matter as long as you do not create a circular dependency chain (an Error will be thrown if you add a definition that would create such a situation)

get

container.get('service')

Gets a service from the container. The container will calculate which dependencies are required to be created so that it can return the requested service. Any singleton services created will be cached internally and re-used. This method returns a Promise that resolves either the service or a rejection if any service provider errors, or if any dependency is missing a service provider

Example

const container = require('gift-box').createContainer()

container.addSingleton('config', function(){
  return {db: process.env['POSTGRES']}
})

container.addTransient('client', function(deps){
  const dbUrl = deps.config.db
  return new DbClient(dbUrl)
}, ['config'])

// Later in another file
container.get('client').then(function(client){
  client.query('some thing')
})

Internals

Add services creates nodes on an internal dependency graph. When a service is requested a subgraph is constructed of any services/objects that need to be instantiated to create the requested service.

TODO

  • Use a faster/better graph library or tree walking impl. (although maybe it's fast as it gets for what we're doing.)
  • add apis's to allow outputting dot-format data so we can print/show dependency graphs
  • add method to show calculated object / new-object graphs (and as above add dot output)
  • Better docs or at least better links to stuff that explains the concepts we are using.
  • Add scoped subcontainers
  • lots more tests
  • some pretty diagrams