lazybox

0.3.1 • Public • Published

Lazybox

Dependency injection container based on Map()

Usage

 
var Lazybox = require('lazybox');
 
var c = new Lazybox();
 
// Set parameter
c.set('foo.bar', 'baz');
 
// Define a service
c.define('foo', () => {
    return new Service();
});
 
// Define a service with dependencies
c.define('answer', ['answer.value', (value) => {
    console.log('Lazily initialized');
    return {
        life: () => value,
        universe: () => value,
        everything: () => value
    };
}]);
c.set('answer.value', 42);
 
c.get('answer').life(); // Logs 'Lazily initialized'
 
// Define a generator service
 
c.define('nextid', function *nextId() {
    const id = 0;
    while (true) {
        yield id++;
    }
});
let id = c.get('nextid')
id(); // 0
id(); // 1
id(); // 2 ...
 
 

Dependency Injection

Providers

// Register a provider function
c.register((c) => {
    c.set('foo', 'bar');
});
 
// Register a provider object
c.register({
    register: (c) => {
        c.set('foo', 'bar');
    }
});
 

API

container.match(pattern, callback)

Use path-to-regexp to match keys.

This does not initialize any service by calling get()

Arguments

  • pattern: A key pattern
  • callback: A function (key, params, container) to call for each result.
cnt.matchKeys('foo.:bar.:baz?', (key, params, container) => { ... });

container.setdefault(key, value)

Similar to Python's dict.setdefault(key, value) method. It gets the value of a key or sets it to value if not defined.

Package Sidebar

Install

npm i lazybox

Weekly Downloads

1

Version

0.3.1

License

ISC

Last publish

Collaborators

  • alxarch