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

@simple-ui/di

v1.0.2

Published

A set of Typescript decorators for dependency injection

Downloads

6

Readme

Dependency Injection

Coverage Status Build Status Dependency Status npm version

A dependency injection library using TypeScript decorators.

Install

Install with npm.

npm install --save @simple-ui/di

Install with bower.

bower install --save @simple-ui/di

Setup

The library uses Decorators to dress classes. To use this library you have to enable experimentalDecorators in your tsconfig.json file.

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

The library uses ES6 exports for all decorators and the main DI object. To import the full library into a file you can destructure the export object.

import { di, provide, inject, ready } from '@simple-ui/di';

The four object you are injecting are:

| Object | Purpose |:-----------|:-------------------------------------------------------------------------------------------------------------------------------- | di | The main DI container | provide | A decorator to register a provided object | inject | A decorator to inject a provided object | ready | A decorator to register a function to call after the constructor when all objects have been injected

Injecting An Object

While a DI system handles creation of all object, there has to be one object created with starts the process. Here is a contrived example which demonstrates the full capabilities of the library.

import { di, provide, inject, ready } from '@simple-ui/di';

@provide()
class ServiceA {
  constructor() {
    console.log(`ServiceA exists`);
  }

  @ready()
  onReady_forA() {
    console.log(`ServiceA is ready`);
  }
}

@provide()
class ServiceB {
  
  @inject()
  public serviceA: ServiceA;

  constructor() {
    console.log(`ServiceB exists`);
  }

  @ready()
  onReady_forB() {
    console.log(`ServiceB is ready`);
  }
}

const serviceB: ServiceB = di.injectProvider('ServiceB');

Output of this code is, which describes the order of operations, the services are created and then ready functions are called.

ServiceB exists
ServiceA exists
ServiceA is ready
ServiceB is ready

The variable of an @inject() must be the name of the service with the first letter lowercase.

DI Containers

The system supports mulitple DI containers for namespacing. There is currently no mechanism to combine two DI namespaces. All providers have been register to the 'global' namespace so far.

import { di, provide, inject, ready } from '@simple-ui/di';

@provide('my-namespace')
class A {

}

@provide('my-namespace')
class B {
  @inject('my-namespace')
  public a: A;
}

To inject an object with di you can use the second parameter to reference a namespace.

di.injectProvider('InNamespace2', 'namespace-2');

Future Enhancements

A list of future enhancements.

  • Connect two or more DI container namespaces