JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35
  • Score
    100M100P100Q43638F
  • License MIT

An IoC container supports Annotation Scanner, Automatically Detect Code Changes and Hot Reload. Inspired by Spring Framework

Package Exports

  • buncha

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (buncha) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

IoC Container. Inspired by Spring Framework

What is buncha?

  • IoC Container - Buncha is essential package to building 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 anytime you modify a file? Buncha detects changes and reload it and all dependents for you automatically.

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 an 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

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.extractMethodNames( user ); //return ["getName", "getAge"]
var annotations = Fx.extractAnnotations( fileContentInString ); //return all annotations

Author

Tho Q Luong thoqbk.github.io

License

MIT