Giter Club home page Giter Club logo

buncha's Introduction

IoC Container. Inspired by Spring Framework

What is buncha?

  • IoC Container - Buncha is essential package to build a powerful, large application
  • Invoke Function and Construct Object - Buncha will find the correct arguments automatically to invoke your functions or construct an object from your class.
  • Dependency Manager - Manage dependencies of services in container. Auto detect dependency cycle.
  • Annotation Scanner - Buncha uses @Service annotation to detect services in your projects.
  • Watch File Changes and Auto Reload - You hate restarting your application to apply the changes every time you modify a file? Buncha detects changes and reload it and all dependents for you automatically.
  • Fast Execution - Buncha usually spends less than 0.02 milliseconds to parse and start invoking a function or constructing an object. See benchmark test

Install

$ npm install --save buncha

Register services manually

//Declare services
var userService = function () {
    //...
}
var ReportService = function (userService) {
    //...
}
//Register services. The registration order is NOT important
var container = new (require("buncha").Container)();
container.registerByConstructor("reportService", ReportService);
container.register("userService", userService);

//Get service by name
var reportService = container.resolve("reportService");
var services = container.resolve(["reportService", "userService"])

Invoke function and construct object

function generateReport (userService, reportService) {
    //...
}

function Report (reportService, userService, type){
    //...
}

//Buncha finds correct arguments to invoke the function
var report1 = container.invoke(generateReport);

//Add missingResolver {type:"pdf"}
//Missingresolver can be a function(parameterName){}
var report2 = container.construct(Report, {type:"pdf"});

Using service annotation to declare a service

Create file service/order-service.js with annotation @Service in multi-line comment block

/**
 * @Service(name="orderService")
 *
 */
module.exports = OrderService;
function OrderService (userService, reportService) {
    //...
}

Scan and watch

Use buncha to scan all services in service directory

var container = new (require("buncha").Container)();
var promise = container.scan( ["service"] );// .scan("service") is also OK.

We can also use .watch() to scan and watch all changes of services to auto reload them:

var container = new (require("buncha").Container)();
var promise = container.watch(["service"]);

Default services in IoC container:

  • $construct
  • $invoke
  • $register
  • $registerByConstructor
  • $resolve
  • $resolveByAnnotation

Function utility

function hello(name, age){
    //...
}
var User = function(name, age){
    this.getName = function(){
        return name;
    }
    this.getAge = function(){
        return age;
    }
}
var user = new User("Tom", 10);

var Fx = require("buncha").Fx;
var parameters = Fx.extractParameters( hello ); //return ["name", "age"]
var methods = Fx.getMethodNames( user ); //return ["getName", "getAge"]
var annotations = Fx.extractAnnotations( fileContentInString ); //return all annotations

Benchmark

On my machine, Buncha usually spends only more 0.00122 ms to 0.009347 ms to parse and invoke a function and 0.001195 ms to 0.020473 ms to construct an object.

Following is the result of my test:

Benchmark result of $invoke:
Wo Container: It takes 34ms to invoke 1000000 times.
W/ Container, Wo Caching: It takes 9381ms to invoke 1000000 times. Average time spent by Buncha: 0.009347ms
W/ Container, With Caching: It takes 1254ms to invoke 1000000 times. Average time spent by Buncha: 0.00122ms

Benchmark result of $construct:
Wo Container: It takes 38ms to construct 1000000 times.
W/ Container, Wo Caching: It takes 20511ms to construct 1000000 times. Average time spent by Buncha: 0.020473ms
W/ Container, With Caching: It takes 1233ms to construct 1000000 times. Average time spent by Buncha: 0.001195ms

Run the benchmark test on your machine:

$ mocha --grep "Benchmark"

Author

Tho Q Luong thoqbk.github.io

License

MIT

buncha's People

Contributors

thoqbk avatar

Stargazers

Dongsu Jang avatar  avatar Anderson de Jesus avatar Wadeck Follonier avatar wangbin avatar Tung Pun avatar  avatar

Watchers

James Cloos avatar wangbin 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.