Giter VIP home page Giter VIP logo

lazy-dependable's Introduction

lazy-dependable

Motivation

This module was built as an in-place replacement for dependable in order to

  1. Introduce "late resolving" (I preferred the word 'lazy' over 'late', hence the module name). In other words, once a piece of software asks for a dependency to be resolved, lazy-dependable will not complain if the designated dependency is still not registered.
  2. Introduce more elegant ways for defining dependencies when asking for resolving. The only available mechanism in dependable is through names of the parameters in the dependency handler. This mechanism is severely sub-optimal, because it asks for stringifying the provided handler in order to parse the very parameter names. Moreover, this mechanism limits the handler programmers. tldr: you cannot reuse a single handler for different dependencies, you need to write the same function again, but with different names of input parameters.

Usage

Create a Container

A Container is a single unit of dependency handling. It registers dependencies and resolves them for dependency consumers.

var Container = require('lazy-dependable').Container;

var container = new Container();
container.register(...
container.resolve(...
container.get(...
//once/if you're done with your container, then don't forget to
container.destroy();

Register a dependency

container.register('connection',connection);

connection is either an object or a function that will return a connection object. You cannot register functions that will depend on other dependencies - this you can do with dependable

Consume dependencies

get

Use get when you need to check if the dependency is registered, because get returns immediately.

container.get('connection',function(connection){
  if(!connection){
  }else{
  }
});

In the above example, if the get is called before connection is registered, you will get undefined.

resolve

If you have a function that needs a connection, you

container.resolve('connection',function(connection){/*use connection*/});

resolve also supports the "Array notation":

container.resolve(['connection',function(connection){/*use connection*/});

and, of course, the "dependable notation":

container.resolve(function(connection){/*use connection*/});

tldr: if resolve gets a function as a first parameter, it will stringify/parse it to read the names of dependables you need.

Chain dependency providers

You can register functions that will depend on other dependencies and chain dependency providers. For example, if you cannot provide a database without obtaining a configuration first, you register your database provider in the following manner:

container.register('database',function(configuration){
  //use the configuration to produce your database connection
  var database = createDatabase(configuration);
  //and finally
  return database;
});

lazy-dependable's People

Contributors

andrija-hers avatar veljkopopovic avatar

Watchers

Rahul Gaur avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.