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

di.libx.js

v2.3.2

Published

> Lightweight & non intrusive Dependency Injection module that supports async/deferred resolution and uglified support for Typescript and JavaScript in 3.3kB gzipped (14.7kB on disk). Feature complete, fast, reliable and well tested.

Downloads

358

Readme

Node.js CI

💉 di.libx.js

Lightweight & non intrusive Dependency Injection module that supports async/deferred resolution and uglified support for Typescript and JavaScript in 3.3kB gzipped (14.7kB on disk). Feature complete, fast, reliable and well tested.

Features:

  • Deferred resolution - asynchronously require dependencies that are not yet available and resolve once it is.
  • Automatic resolve of function params - resolve & map dependencies manually or as function's parameters
  • NodeJS & browser - browserified version ready to use from CDN.
  • Explicit or implicit dependencies - works with uglified files by specified dependencies' names or implicitly from function/class name.
  • Typescript support - specify injected instance's types.
  • Non Intrusive - register any modules; your internal modules or 3rd-party modules without modifing its code. No attribute wrapping needed.

Use:

Install via yarn (recommended):

yarn add di.libx.js

Install via npm:

npm install --save di.libx.js

Browser:

https://cdn.jsdelivr.net/npm/di.libx.js@latest/dist/browser.min.js
(Modules are loaded into `window.libx.di` object).

Use this library in browser from CDN, code example, live example.
Or include from node_modules.


Basic usage:

import DependencyInjector from 'di.libx.js';
// const DependencyInjector = require('di.libx.js');

const di = new DependencyInjector();

const myFunc = () => {
    console.log('This is myFunc');
};

// Register a dependencies
di.register('func', myFunc);

// Require dependencies. Will wait until all dependencies are ready
// Note that awaiting this function will create dead lock unless the other register will called in parallel
di.inject((func, anonFunc) => {
    func();
    anonFunc();
});

// Register another dependencies. Will trigger execution of the `require`
di.register('anonFunc', () => console.log('Anonymous func'));

More examples:

// in your web app add:
// <script src="https://cdn.jsdelivr.net/npm/di.libx.js@latest/dist/browser.min.js"></script>

var myModule = { somveVar: 1 };
libx.di.register('myModule', myModule);

// async require:
await libx.di.inject((myModule) => {
    console.log('dependency resolved!', myModule);
    // execute your code here. 'await' is optional incase you want it to be async and continue execution.
    // note: the callback will be triggered only once the dependency is registered somewhere else in your program. Beware not to create dead-lock.
});

// synchronously get a module:
mod = await libx.di.require('myModule');

// register new module with other dependencies:
mod = libx.di.injectAndRegister('myNewModule', (myModule) => {
    return () => console.log('this came from myNewModule!', myModule);
});

// inject for uglified code (second param is module identifiers, injected by position. So `myUglifiedModule` == `myModule`):
libx.di.inject(
    (myUglifiedModule) => {
        console.log('unglified dependency resolved!', myUglifiedModule);
    },
    ['myModule']
);
Sub Container:
// Register a local scoped container that inherits from the main container. 
// All locally registered modules will be disposed once exited scope.
const subContainer = new DependencyInjector(di);
subContainer.register('moduleB', di.initiate(ModuleB));

// Main execution point:
subContainer.inject((moduleB) => {
	const result = moduleB.Run(10);
	console.log('Result: ', result);
}).then(() => {
	console.log('DONE!');
});

Check more examples in unit-test at tests.


Develop:

Build:

$ yarn build

Watch & Build:

$ yarn watch

Run tests:

$ yarn test