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

adv-ioc

v1.0.6

Published

Simple IOC

Downloads

3

Readme

adv-ioc

Register class

Register lazy created singleton

import { IOC } from "adv-ioc";

class Foo {
}

class Bar {
    constructor(public foo: Foo) {
    }
}

const ioc = new IOC();
ioc.register(Foo);
ioc.register(Bar);
console.log(ioc.resolve("foo")); // prints Foo {}
console.log(ioc.resolve("bar")); // prints Bar { foo: Foo {} }
console.log(ioc.resolve("foo") === ioc.resolve<Bar>("bar").foo); // prints true

Register singleton under name

Register lazy created singleton

import { IOC } from "adv-ioc";

class Foo {
}

const ioc = new IOC();
ioc.registerWithName("bar", Foo);
console.log(ioc.resolve("bar")); // prints Foo {}

Register value

Register prepared value

import { IOC } from "adv-ioc";
const ioc = new IOC();
ioc.registerValue("foo", 123);
console.log(ioc.resolve("foo")); // prints 123

Register factory

The best to register loggers, to factory as parameters are passed: class, class name, and property name

import { IOC } from "adv-ioc";

class Foo {
    constructor(public logger: Logger) {
    }
    
    foo() {
        this.logger.log("abc");
    }
}

class Logger {
    constructor(public name: string) {
    }
    
    log(...args: unknown[]) {
        console.log(`[${this.name}]`, ...args);
    }
}

const ioc = new IOC();
ioc.register(Foo);
ioc.registerFactory("logger", (parent: unknown, parentName: string|null, propertyName: string) => {
    return new Logger(parentName || "<unknown>");
});
ioc.resolve<Foo>("foo").foo(); // prints "[foo] abc"

Create object

If you want to create new instance of registered class (not to create singleton), use the create method instead of resolve method.

import { IOC } from "adv-ioc";

class Foo {
}

const ioc = new IOC();
ioc.register(Foo);
console.log(ioc.create("foo")); // prints Foo {}

Create object of not registered class

import { IOC } from "adv-ioc";

class A {
}

const ioc = new IOC();
console.log(ioc.createEx(A)); // prints A {}

Create object with one-time dependency map

import { IOC } from "adv-ioc";

class A {
    constructor(public foo: string, public abc: number) {
    }
}

const ioc = new IOC();
console.log(ioc.createEx(A, {foo: "bar", abc: 123})); // prints A { foo: "bar", abc: 123}

Inject dependency into property

Use @Inject decorator. To supress typescript no-initializer error, add exclamation and the end of the variable name

import { IOC, Inject } from "adv-ioc";

class A {
    
    @Inject foo!: string;
    
}

const ioc = new IOC();
ioc.registerValue("foo", "bar");
console.log(ioc.createEx(A)); // prints A { foo: "bar" }

Inject dependency into renamed property

Use @InjectNamed decorator.

import { IOC, InjectNamed } from "adv-ioc";

class A {
    
    @InjectNamed("foo") myVariable!: string;
    
}

const ioc = new IOC();
ioc.registerValue("foo", "bar");
console.log(ioc.createEx(A)); // prints A { myVariable: "bar" }

Usage injected property in constructor

Injected property in constructor will be undefined

import { IOC, Inject } from "adv-ioc";

class A {
    
    @Inject foo!: string;
    
    constructor() {
        console.log(this.foo);
    }
}

const ioc = new IOC();
ioc.registerValue("foo", "bar");
console.log(ioc.createEx(A)); // prints undefined and then A { foo: "bar" }

To fix it extends Injectable class

import { IOC, Inject, Injectable } from "adv-ioc";

class A extends Injectable {
    
    @Inject foo!: string;
    
    constructor() {
        super();
        console.log(this.foo);
    }
}

const ioc = new IOC();
ioc.registerValue("foo", "bar");
console.log(ioc.createEx(A)); // prints "bar" and then A { foo: "bar" }

Register multiple entries from directory

You can also use scanner to add all your servies to ioc. It works only in node context.

import { IOC } from "adv-ioc";
import { Scanner } from "adv-ioc/out/Scanner";

const ioc = new IOC();
Scanner.registerToIoc(ioc, "/path/to/services");

You can pass filter to take only valid files

Scanner.registerToIoc(ioc, "/path/to/services", /.*Service\.js$/);

You can also turn on debug mode to see which classes from which files are added

Scanner.DEBUG = true;