1.1.1 • Published 10 months ago

intify v1.1.1

Weekly downloads
1
License
ISC
Repository
-
Last release
10 months ago

Intify

Dependency injection container for javascript, built for functional programming style.

Intify allows you to keep all of your dependencies in one place and lazy load them on demand.

Motivation

When you working in functional programming style you want to control every single piece of your app. And if you use Dependency injection and Inversion of control design patterns in your workflow then you have many different dependencies linked between your functions. And it becomes a pain to reach them all and keeping in mind where from each dependency comes in.

That's why I started to build very simple and lightweight injection container. Intify has only a few methods and it is very handy tool in my toolbag.

Hope you will find it useful too.

Read more with examples


Legacy docs

Installation

yarn add intify

or

npm install intify

How it works

General scheme looks like this: General scheme

And flow is simple: 1. Create store 2. Register all of the dependencies 3. Provide required dependencies to your functions 4. Intify will take over the rest

Usage

Import

Require modules

const { createStore, injectDeps } = require('intify');

Import modules

import { createStore, injectDeps } from 'intify';

General usage

Here is only one requirement. Each function with dependencies should have "deps" object as first argument. For example:

const targetFunction = (deps, ...args) => doSomething;

For simpler syntax you can use object destructuring:

const targetFunction = ({ dependency }, ...args) => doSomething;

Register dependencies

For register any dependency you have to call .register() method:

import { createStore } from 'intify';

const intify = createStore();
intify.register({ dependency });

Injecting dependecies

.inject() is higher order function which returns wrapped function with provided dependencies.

import { injectDeps } from 'intify';
const targetFunction = ({ dependency }, ...args) => doSomething;
export default injectDeps(['dependency'], targetFunction);

Examples

As you can see you can use dependencies without restructuring your code. Intify is pretty smart and will load required dependencies only on target function call. So if haven't registered dependency before import of target function it is ok and it will not throw an Error.

Usage example

// utility.js
import { injectDeps } from 'intify';
const utility = ({ database }, someValue) => database.add(someValue);
export default injectDeps(['database'], utility);

// index.js
import { createStore } from 'intify';
import database from 'database';
import utility from './utility';

export const intify = createStore();
intify.register({ database });

utility();

Test example

// utility.spec.js
import { createStore } from 'intify';
import utility from './utility';

const intify = createStore();

const mockDatabase = jest.fn();
intify.register({ database: mockDatabase });

it('should works', () => {
  utility();
  expect(mockDatabase.mock.calls.length).toEqual(1);
});

API

createStore :: () -> Store

Create store of dependencies. Currently allows to keep only 1 store at time.

Example:

import { createStore } from 'intify';
const intify = createStore();

intify
  .register({ database: database })
  .register({ log: console.log });

Store methods

.getDeps :: () -> Object

Returns object with all registered dependencies

.subscribe :: (next) -> Observer

Experimental feature

Require "next" function which will be triggered each time when any dependency will be called.

Next will be called with object:

{
  fn, // called function
  name, // called function's name
  args // arguments provided to called function
}

Example:

import { createStore } from 'intify';
const intify = createStore();

const observer = intify.subscribe(console.log);
// to unsubscribe just call
observer.unsubscribe();

.unsubscribe :: () -> Void

Remove all listeners.

Example

import { createStore } from 'intify';
const intify = createStore();

intify.subscribe(console.log);

// later
intify.unsubscribe();

injectDeps :: (String[] | null, Function) => Function

Inject dependencies to target function.

Example:

import { injectDeps } from 'intify';
const addValueToDatabase = ({ database }, newValue) => database.add(newValue);

/* - will inject only "database" function */
export default injectDeps(['database'], addValueToDatabase);

// or

/* - will inject all available dependencies */
export default injectDeps(null, addValueToDatabase);

Dependencies

Contributing

1.1.1

10 months ago

1.1.0

10 months ago

1.0.0

10 months ago

1.0.0-beta1

7 years ago

0.3.7-beta

7 years ago

0.3.6-beta

7 years ago

0.3.5-beta

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0-beta

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago