npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

einigeln

v0.1.0

Published

Dependency Injection container using a interface like Pimple3 with compiler, configuration and events.

Downloads

4

Readme

Einigeln.js Build Status

Einigeln is a library to do "Dependency Injection" or "Inversion of Control" initially for NodeJS, inspired by some other libraries like Pimple or Electrolyte.

The difference from the other libraries, this one targets at:

  • Throwing errors if anything invalid happens.
  • Lazy instantiation of services.
  • Access to raw service definitions.
  • Tagging services with configurations.
  • Frozen Services if they have been created at least once.
  • Lock Container by configuration, so definitions will not change.
  • Forbid instantiation by configuration, so container will behave like a ContainerBuilder.
  • Compiler that enables events before and after instantiation is enabled.

Other features have not been targeted, like:

  • Not (yet) including require actions. These need to be done by users.
  • Not (yet) offering a module.export[@require] Syntax or such, but could be added if needed.

In the end Einigeln is a container that can be given to 3-rd-party code to define their own services inside.


API

There are just a few calls, ordered by the most common ones:

// This code can also be found at: `example.js`

var Einigeln = require('./einigeln');

var container = new Einigeln();

// Defining a parameter
container.set('name', 'world');
console.log(container.get('name'));

// Defining a service
container.set('hello', function(container) {
    // `container` is the same instance as outside `container`
    return 'hello '+container.get('name')
});
console.log(container.get('hello'));

// Defining a service with inject
container.inject('bye', function(name) {
    // `name` has been injected.
    return 'bye '+name
}, ['name']);
console.log(container.get('bye'));

// Defining a service with MAGIC inject
container.inject('bye2', function(name) {
    // `name` has been MAGICALLY injected.
    return 'bye '+name
});
console.log(container.get('bye2'));

// Check if a definition exists
container.exists('name'); // true

// Remove a definition
container.set('foo', 42);
container.unset('foo');
container.exists('foo'); // false

// Factory, is a callback that will by executed every time the service is accessed.
container.set('random', container.factory(function(container) {
    return Math.random();
}));
console.log('Two random numbers: ', container.get('random'), container.get('random'));

//--- Tagging services

container.set('static.users', function(container) {
    return ['alice', 'bob'];
});
container.tag('static.users', 'userprovider', {some: 'config'});
console.log(container.tagged('userprovider'));
// Will output:
// [ { name: 'static.users', config: { some: 'config' } } ]

//--- Some more uncommon API calls:

var keys = container.keys();
console.log('All defined services: ', keys);

// Extend will wrap the old service callback with the new given one.
container.set('alice', function(container) {
    return 'Hello Alice!';
});
container.extend('alice', function(oldDefinition, container) {
    return 'Hello Bob! '+oldDefinition();
});
console.log(container.get('alice')); // Will output 'Hello Bob! Hello Alice!'

// Protect, is a callback that will not be executed and should be threaded like a static parameter.
container.set('simple.callback', container.protect(function(container) {
    return 'Very simple!';
}));
console.log(container.get('simple.callback')());

// Raw will return the callback used in the service definition.
container.set('leet', function(container) {
    return 1337;
});
var leet = container.raw('leet');
console.log(leet());

Compiler

Using the compiler is a more advances feature, even if it works quite simple.

var containerConfig = null;
container = new Einigeln(function(config) {
    containerConfig = config;
});
console.log(containerConfig);
// Will output:
// { instantiate: true,
//   locked: false,
//   compiler:
//      { onCompilePost: [Function],
//        onCompilePre: [Function],
//        emitCompile: [Function] } }

// Disabling instantiation for container:
containerConfig.instantiate = false;

// Registering for Compiler events:
containerConfig.compiler.onCompilePre(function(container) {
    // This event will be called when the compiler will 'emitCompile()'.
    // In such a callback, a second chance for changes in the definitions is given.
    // This could be used to change the definitions of modules that have put their definitions after you in the container.
    console.log('containerConfig.instantiate is', containerConfig.instantiate);
    console.log('onCompilePre');
});

containerConfig.compiler.onCompilePost(function(container) {
    // This event will be called just _after_ 'onCompilePre()' with always `instantiate: true`.
    // In such a callback, there can be done stuff with the real instantiated objects.
    // The difference to `onCompilePre` is, that here we can not change the definitions inside the container anymore,
    // but can do late wiring of instantiated services from the container.
    console.log('containerConfig.instantiate is', containerConfig.instantiate);
    console.log('onCompilePost');
});

// Run the compiler callbacks.
containerConfig.compiler.emitCompile();

// Lock the container, so changes are not possible.
containerConfig.locked = true;

// This will fail then:
container.set('foo', 'bar');

TODO

  • If somebody wants to use it in a browser, change module loading that way.

About the name

Imagine a hedgehog that curls himself to a ball. There is a german word for that called "einigeln", with no english representation AFAIK. The analogy to this library is the possibility to protected the container from unwanted changes from outside.