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

typeject

v3.0.0

Published

Simple, typesafe and unobstrusive dependency injection

Downloads

1

Readme

typeject

Simple, typesafe and unobstrusive dependency injection

Build Status

Why another dependency injection library for TypeScript?

I was looking for a dependency injection framework that fulfills those criterias:

  1. It should be very small
  2. It should be unobstrusive (no decorator and metadata voodoo)
  3. It should be easily and intuitively configurable
  4. It should be type-safe
  5. It should have a very low memory footprint
  6. It should be tree-shakeable

When looking around, I only found libraries like inversify and TypeDI, which are great, I'm sure! But for my needs it was too full-blown and I didn't want to turn on metadata generation, if not really necessary, so I built my own library which matches my requirements.

Concept

typeject is a type safe dependency injection library. All Dependencies in typeject are created lazily via providers. There are two types of providers in this DI framework that you can use as you need.

Singletons

Singleton providers always provide the same instance of a type. Once a singleton is instantiated, it's factory can be garbage-collected.

Volatiles

Volatiles are similar to singletons. The difference is that the factory of each volatile is remembered and the instance is weakly referenced. This means that, if there are no references anymore, the instance is scheduled for garbage collection. If it was garbage collected, the provider will create and serve a new instance on the next request.

Weak references are only used, if the environment supports it. As a fallback, volatiles are treated as singletons.

Prototypes

Prototypes always provide a new instance of a factory.

When to use which type of provider?

Here are some hints to drive your decision:

  • If your instance of a factory holds state for the lifetime of the app, you should definitely use a singleton for this factory.
  • If memory is a great concern and parts of your app are not used all the time, you should use volatiles. Single Page Apps for example can benefit from this approach.
  • If memory is no concern and you want max performance, go for singletons.
  • If you always need a fresh instance, use a prototype.

Example

// Types

interface IServiceA {
  state: { foo: string };
  use(): void;
}

interface IServiceB {
  useServiceA(): void;
}

// Implementation

class ServiceA implements IServiceA {
  state = { foo: "bar" };
  use(): void {
    console.log(this.state.foo);
  }
}

class ServiceB implements IServiceB {
  constructor(private serviceA: IServiceA) {}
  useServiceA() {
    this.serviceA.use();
  }
}

// Configuration

const serviceA = volatile<IServiceA>(() => new ServiceA());

const serviceB = volatile<IServiceB>(() => new ServiceB(serviceA()));

// Run

serviceB().useServiceA();

API

singleton<T>(factory: Factory<T>): () => T

Creates a singleton provider.

volatile<T>(factory: Factory<T>): () => T

Creates a volatile provider.