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

@gityoog/ioc-di

v1.2.8

Published

frontend ioc-di

Downloads

7

Readme

安装

$ npm install ioc-di@npm:@gityoog/ioc-di --save
$ npm install git+https://github.com/gityoog/ioc-di.git --save

使用

  • 使用 @Root() 装饰器为当前类添加一个顶层容器 会依次初始化所有可注入类和子容器
  • 使用 @Service() 装饰器标记当前类为可注入类
  • 使用 @Inject() 装饰器注入类
  • 当类循环引用时请使用 @InjectRef(() => token) 注入
  • tsconfig.json 中设置 emitDecoratorMetadata: true 可使用属性类型作为默认注入令牌
  • 初始化容器和注入信息是发生在构造函数之后 所以当需要第一时间访问注入属性的时 请使用 @Already 装饰器
  • 当可注入类实例为手动初始化时 可使用 Concat 函数使目标实例使用当前实例的容器
  • 使用 @Container() 可使当前类使用一个独立的子容器
  • @Destroy 使当前函数为销毁函数 调用此函数会使挂载在当前实例的容器销毁 并销毁其创建的其他实例
// file entry.ts

@Root()
@Service()
class Entry {
  @Inject() service!: Service;
}

// file service.ts

@Service()
class Service {
  @Inject(Foo) foo!: Foo;
  @Inject() foo2!: Foo2;

  constructor() {
    this.foo.method(); // ❌无法在构造函数内访问注入属性
    this.init(); //✔️使用 @Already 装饰器可以在对应函数内第一时间访问注入属性
  }

  method() {
    this.foo.method();
    this.foo2.method();

    foo3 = Concat(this, new Foo3());
  }

  @Already
  init() {
    this.foo.method();
    this.foo2.method();
  }
}

// file foo.ts

@Service()
class Foo {
  @InjectRef(() => Foo2) foo2!: Foo2;

  method() {
    console.log(this.foo2);
  }
}

// file foo2.ts

@Service()
class Foo2 {
  @InjectRef(() => Foo) foo!: Foo;

  method() {
    console.log(this.foo);
  }
}

// file foo3.ts

@Container()
@Service()
class Foo3 {}

changelog

  • 2022-09-29 add Put method, export DiContainer InstanceMeta PrototypeMeta
  • 2022-11-17 define injection prop after create
  • 2022-11-24 merge Concat&Put, Already add params for afterInit
  • 2023-05-25 fix InstanceMeta memory leak

运行流程

image