Giter Club home page Giter Club logo

sandal's Introduction

Sandal

Build Status

Sandal is a javascript inversion of control container

Installation

$ npm install sandal

Usage

// For use in browsers, including sandal.js will create a global Sandal constructor
var Sandal = require('sandal');

var sandal = new Sandal();

sandal
    .registerService('service1', function (service2, factory1, object1) {})
    .registerService('service2', function (done) {
        setTimeout(function() { done(); }, 500);
    })
    .register('object1', 'any object')
    .registerFactory('factory1', function(object1) {
        return 'another object';
    }, true) // Setting transient lifecycle
    .resolve(function(error, service1) {
        // Resolves service1 and all dependencies
    })

Register

There are three ways you can register:

sandal.register(name, implementation);
sandal.registerService(name, constructor, transient);
sandal.registerFactory(name, factory, transient);

A module registered with register will always have a singleton lifecycle. Resolving will always return the same instance.

A module registered with registerFactory must be a function. The return value of the function will be the resolved value. The default lifecycle is singleton meaning that the function is only evaluated once. By setting transient to true the function will be evaluated every time.

A module registered with registerService must be a function. The function will be called as a constructor with the new keyword. The default lifecycle is singleton meaning that the constructor is only evaluated once. By setting transient to true the constructor will be evaluated every time.

Note that the service or factory is evaluated when resolved and not when registered.

There are two reserved words that can not be used as names:

  • sandal will inject the ioc container
  • done will inject a done callback

Resolve

Resolving can be done in the following ways:

sandal.resolve(callback);
sandal.resolve(name, callback);
sandal.resolve([names], callback);

The first argument of the callback will be any error from resolving.

Resolving with only a callback will user the argument names of the callback function.

Resolving with a name will resolve based on the provided name and inject into the callback.

Resolving with an array of names will resolve based on the provided names and inject into the callback in the same order.

Clear

Registering with a name that is already registered will throw an error. To replace an implementation you first need to clear the current one. It is possible to clear all, one or a set of modules:

sandal.clear(); // Clear all modules
sandal.clear(name);
sandal.clear([names]);

Asynchronous service or factory

If a service or factory has some asynchronous tasks that needs to be done before the instance can be used, it should take an argument named done.

That will inject a done callback to the constructor. Calling the done callback with an error will pass the error to the resolve callback.

Resolving a module with dependencies to an asynchronous service or factory will wait for all dependencies to be done before calling the constructor or function.

sandal's People

Contributors

kesla avatar marcusberner avatar

Watchers

 avatar  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.