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

what-di

v1.0.5

Published

依赖注入工具 ---

Downloads

14

Readme

依赖注入工具

what-di

安装方法:

npm i what-di
# 或
yarn add what-di

使用方法

创建根容器:

一般在应用入口处创建。创建是可以提供默认的providers和modules。

import { createRootContainer } from 'what-id';

createRootContainer({
  providers: [
    {provide: 'config', useValue: {host: '0.0.0.0'}},
  ],
  modules: []
});

运行时注册供应者:

需要动态注册provider时,可以使用该函数将供应者注册到根容器。

import { registerRootProviders } from 'what-id';

registerRootProviders([
  { provide: 'extra', useValue: { publicUrl: '/' },
]);

注入依赖:

import { inject } from 'what-id';

const config = inject('config');

注入模块依赖:

注入模块依赖时,需要提供对应子模块的命名空间。注入器会先在当前模块内查找实例,如果未找到则会继续从父容器内查找,直到找到。找不到时返回undefined。

import { inject, createNamespaceHelpers } from 'what-id';

const config = inject('config', 'module-name');

// 也可以创建版绑定模块名的辅助函数

const { inject } = createNamespaceHelpers('module-name');
const config = inject('config'); // 即可略去模块名

在类中注入依赖:

import { inject, Inject } from 'what-id';

class Example {
  constructor() {
    this.config = inject('config');
  }
}

// 或装饰器模式。实验功能,可能不稳定。

class Example {
  @Inject('config')
  config;
}

创建子容器:

import { createModuleContainer } from 'what-id';

const module = createModuleContainer({
  providers: [
    {provide: 'module', useValue: {host: '0.0.0.0'}},
  ]
});

创建的子模块可以在创建根容器的时候作为参数传递到modules数组中。

供应者类型

import { BaseProvider } from 'what-di';

export type ValueProvider = {
  provide: string;
  useValue: any;
}

export type FactoryProvider = {
  provide: string;
  useFactory: any;
}

export interface ClassProvider {
  provide: string;
  useClass: typeof BaeProvider;
}

// 也可以直接传递一个类到provider数组,但该类需要继承BaseProvider,例如

// import { BaseProvider, createRootContainer } from 'what-di';
class UserService extends BaseProvider {
  // ...
}

createRootContainer({
  providers: [
    UserService, // 直接使用类,注入时可以 inject('UserService') 或 inject(UserService)
  ],
  modules: []
});

如何订阅状态provider实例状态?

如果使用ClassProvider,需要继承BaseProvider,BaseProvider内置subscribe方法提供订阅;setState方法用于设置状态。

如果是ValueProvider或FactoryProvider,则需要自行实现订阅发布规则。

import { BaseProvider, inject } from 'what-di'
type StateType = {
  name: string;
}

class Subscribable extends BaseProvider<StateType> {
  // ...
  fetch() {
    // ... 
    this.setState(userData);
  }
}

// 订阅状态

const Subscribable = inject<Subscribable>(Subscribable);

const unsubscribe = Subscribable.subscribe((state: StateType) => {
  console.log(state);
});

// 在需要时取消订阅
unsubscribe()

附加API(不建议使用)

getRoot(): Container; 获取根容器实例

what-di工具参考文档: https://www.npmjs.com/package/what-di