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

@netleaf/extensions-dependency-injection-typed

v0.1.2

Published

Dependency Injection extension for NetLeaf application host framework based on tst-reflect-transformer.

Downloads

7

Readme

@netleaf/extensions-dependency-injection-typed

Dependency Injection extension for NetLeaf application host framework based on tst-reflection-transformer and inspired by .NET.

To proper use of this package, you need ttypescript and tst-reflect. Watch tst-reflect repository for more information.

How to start

  • Install this package: npm i @netleaf/extensions-dependency-injection-typed,
  • install the tst-reflect transformer: npm i tst-reflect-transformer -D,
  • install ttypescript: npm i ttypescript -D,

Now just add the transformer to the tsconfig.json and run npx ttsc instead of tsc.

{
    "compilerOptions": {
        // your options...

        // ADD THIS!
        "plugins": [
            {
                "transform": "tst-reflect-transformer"
            }
        ]
    }
}

and with Webpack

({
    test: /\.(ts|tsx)$/,
    loader: "ts-loader",
    options: {
        // ADD THIS!
        compiler: "ttypescript"
    }
})

Usage

Run on repl.it

First lets have some services (interfaces and classes):

import { IServiceProvider } from "@netleaf/extensions-dependency-injection-abstract";

interface ITextFormatter
{
    format(text: string);
}

class SpaceColorFormatter implements ITextFormatter
{
    format(text: string)
    {
        return "> " + text;
    }
}

interface ILogger
{
    info(message: string);
}

class ConsoleLogger implements ILogger
{
    private readonly console: Console;

    constructor(private formatter: ITextFormatter, serviceProvider: IServiceProvider)
    {
        this.console = serviceProvider.getService("console");
    }

    info(message: string)
    {
        this.console.info(this.formatter.format(message));
    }
}

Usage:

import { TypedServiceCollection, TypedServiceProvider } from "@netleaf/extensions-dependency-injection-typed";

// Create service collection
const serviceCollection = new TypedServiceCollection();

// Add services into the collection
serviceCollection.addTransient<ILogger, ConsoleLogger>();
serviceCollection.addSingleton<ITextFormatter, SpaceColorFormatter>();
serviceCollection.addSingleton("console", console);
serviceCollection.addSingleton("app.config", { someObject: { eg: "config" } });

// Create ServiceProvider from the ServiceCollection
const serviceProvider = new TypedServiceProvider(serviceCollection);

// Get instance of the ILogger
const logger: ILogger = serviceProvider.getService<ILogger>();

// Get app config
const config = serviceProvider.getService("app.config");

logger.info("Hello World!");
logger.info(JSON.stringify(config));

Output:

> Hello World!
> {"someObject":{"eg":"config"}}

Lifetimes

Services can be registered with one of the following lifetimes:

  • transient,
  • scoped,
  • singleton.

Transient

Services registered with transient lifetime are created each time they're requested from the service provider. This lifetime works best for lightweight, stateless services.

Register transient services with addTransient().

Scoped

Services registered with scoped lifetime are created only once per scope. It is similar to a singleton inside one scope.

Register transient services with addScoped().

Create new scope with serviceProvider.createScope();

Example

// Create service collection
const serviceCollection = new TypedServiceCollection();

// Add "service" into the collection
let scopedId = 0;
serviceCollection.addScoped("scopedId", provider => ++scopedId);

const serviceProvider = new TypedServiceProvider(serviceCollection);

console.log(serviceProvider.getService("scopedId")); // > 1
console.log(serviceProvider.getService("scopedId")); // > 1

const scope1 = serviceProvider.createScope();
console.log(scope1.serviceProvider.getService("scopedId")); // > 2
console.log(scope1.serviceProvider.getService("scopedId")); // > 2

const scope2 = serviceProvider.createScope();
console.log(scope2.serviceProvider.getService("scopedId")); // > 3
console.log(scope2.serviceProvider.getService("scopedId")); // > 3

console.log(serviceProvider.getService("scopedId")); // > 1
console.log(serviceProvider.getService("scopedId")); // > 1

Singleton

Services registered with scoped lifetime are created only once. Single instance is used for whole application. Same instance is returned each time it is requested from the service provider, through all scopes.

Register transient services with addSingleton().