Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

odojs/injectinto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Dependency Injection

pod.js

const inject = require('injectinto')

inject('pod', () => {
  const db = inject.one('db')

  db.key = 'value'
})

index.js

const inject = require('injectinto')

const db = {}
inject('db', db)

for (let pod of inject.many('pod')) pod()

What is the problem?

I'd like to build systems that can easily be extended, enhanced, or replaced.

How injectinto solves this problem

  1. One or more objects are bound to string keys, for example 'db'
  2. Other parts of the code can request objects by providing in the key, for example 'db'

API

inject(key, value)

inject.bind(key, value)

Register a value to a string key for retrieval later. Multiple calls will register multiple objects and be accessed via inject.many(key)

inject.unbind(key, value)

Remove a registered value for a key. Does not error if value is not found.

inject.one(key)

Return a single registered value for a given key. Will error if no value is found or if more than one value is found.

inject.oneornone(key)

Return a single registered value or null for a given key. Will error if more than one value is found.

inject.many(key)

Return all registered values for a given key. Will return an empty array if no values are registered.

inject.clear(key)

Clears all registered values for a given key.

inject.reset()

Clears all registered values for all keys.

About

Poor Man's Dependency Injection

Resources

Stars

Watchers

Forks

Packages

No packages published