fndi

0.3.0 • Public • Published

fnDI.js

Simple, functional, ligthweight, dependencyless DI/IOC library for JS

Usage

Install using yarn or npm

yarn add fndi
 
npm install fndi

Getting started

// Function sintax
const scope = require('fndi');
 
function A() {}
 
// Class Sintax
class B {
  constructor(a) {
    this.a = a;
  }
}
 
// Registration Function
const registration = registry => {
  registry({ type: A });
  registry({ type: B });
};
 
function main(resolve, args) {
  const bInstance = resolve(B);
}
 
const scopedMain = scope(registration, main);
 
scopedMain(args);

Register

The registration is done within a function that takes a registry function as argument.

const registration = registry => {
 
  //Only type registry
  registry({ type: MyTypedClass });
 
  // Named Registry
  registry({ name: 'MyNamedInstance', type: MyNamedClass });
 
  // Provide a single instance every time
  registry({ type: MyClassWithValue, value: { a: 1 } });
 
  // Registering a Factory function
  registry({
    type: MyClassWithFactory,
    factory: resolve => resolve(MyClassWithFactory);
  });
 
  // Delegate the class
  registry({
    type: MyDelegatedClass,
    by: AnotherCompletelyDifferentClass
  })
 
};

Registry Entry

Each registry entry (the argument for the registry function) must follow the following definition:

interface Entry<T> {
  name?: string;
  type: Type<T>;
  value?: T;
  factory?: resolveFunction => T;
  by?: Type;
  persist?: boolean;
}

Scopes

scope is a function wrapper, it provides a resolve function which can be used in the underlying function to get instances of the defined types (by either type or name).

function main(resolve, arg1, arg2) {
  const fooInstance = resolve(Foo);
  const barInstance = resolve('BarRegistryName');
}
 
const scopedMain = scope(registration, main);
 
scopedMain(arg1, arg2);

TODO

  • async dependencies
  • Lazy dependencies

Package Sidebar

Install

npm i fndi

Weekly Downloads

1

Version

0.3.0

License

Unlicense

Unpacked Size

292 kB

Total Files

12

Last publish

Collaborators

  • jpichardo