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

@fluffy-spoon/inverse

v1.35.0

Published

`@fluffy-spoon/inverse` is an Inversion of Control framework.

Downloads

12

Readme

@fluffy-spoon/inverse is an Inversion of Control framework.

Installing

npm i @fluffy-spoon/inverse --save

TypeScript requirements

This framework requires the following values set in your TypeScript configuration (tsconfig.json):

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "esModuleInterop": true
    }
}

Examples

These examples assume that there's a Bar class which as some dependencies (A1 and A2), which then further have some dependencies (B1 and B2) as shown below.

Note how every class has an Injectable decorator, and how every dependency has an Inject decorator. That's all that is needed! The examples below would work for exported classes from separate files too.

@Injectable
class Bar {
    constructor(
        @Inject private a: A1, 
        @Inject private b: A2, 
        @Inject private c: A2) 
    {
    }
}

@Injectable
class B1 {

}

@Injectable
class B2 implements B1 {

}

@Injectable
class A1 {
    constructor(
        @Inject private a: B1) 
    {
    }
}

@Injectable
class A2 {
    constructor(
        @Inject private a: B2) 
    {
    }
}

Basic use

In the following example, a Bar instance is requested from the IOC container. Per default, each dependency will be instantiated recursively with its own constructor.

The following code:

import Container from '@fluffy-spoon/inverse';

const container = new Container();

const bar = container.resolve(Bar);

Is equal to:

const bar = new Bar(
    new A1(new B1()),
    new A2(new B2()),
    new A2(new B2()));

Changing dependencies

In the below example, we change all B1 to be instances of B2 instead. This is possible because B2 implements B1.

The following code:

import Container from '@fluffy-spoon/inverse';

const container = new Container();
container.whenRequestingType(B1).useType(B2);

const bar = container.resolve(Bar);

Is equal to:

const bar = new Bar(
    new A1(new B2()),
    new A2(new B2()),
    new A2(new B2()));

You can also use:

  • useFactory for determining how an instance should be created.
  • useRequestedType to use the requested type (this is default).

Changing lifetime

In the below example, we make B2 instances be single-instance by using the asSingleInstance method.

Note that useRequestedType is called here. This just means that when a B2 instance is requested, a B2 instance is also injected (which is default).

import { Container } from '@fluffy-spoon/inverse';

const container = new Container();
container.whenRequestingType(B2).useRequestedType().asSingleInstance();

const bar = container.resolve(Bar);

Is equal to:

const b2 = new B2();
const bar = new Bar(
    new A1(new B1()),
    new A2(b2,
    new A2(b2)));

You can also use:

  • asInstancePerRequest to create a new instance from the type or factory provided every time (this is default).

Framework support

@fluffy-spoon/inverse can be used without a framework, but more packages can be used if you want to tightly integrate it.