@kaokei/di 1.0.25 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +46 -15
  2. package/dist/index.cjs.js +230 -329
  3. package/dist/index.cjs.js.map +1 -1
  4. package/dist/index.cjs.min.js +2 -2
  5. package/dist/index.cjs.min.js.map +1 -1
  6. package/dist/index.cjs.runtime.js +218 -310
  7. package/dist/index.cjs.runtime.js.map +1 -1
  8. package/dist/index.cjs.runtime.min.js +2 -2
  9. package/dist/index.cjs.runtime.min.js.map +1 -1
  10. package/dist/index.esm.js +230 -327
  11. package/dist/index.esm.js.map +1 -1
  12. package/dist/index.esm.min.js +3 -3
  13. package/dist/index.esm.min.js.map +1 -1
  14. package/dist/index.esm.runtime.js +218 -299
  15. package/dist/index.esm.runtime.js.map +1 -1
  16. package/dist/index.esm.runtime.min.js +3 -3
  17. package/dist/index.esm.runtime.min.js.map +1 -1
  18. package/dist/index.iife.js +230 -329
  19. package/dist/index.iife.js.map +1 -1
  20. package/dist/index.iife.min.js +3 -3
  21. package/dist/index.iife.min.js.map +1 -1
  22. package/dist/src/Injector.d.ts +142 -131
  23. package/dist/src/Injector.d.ts.map +1 -1
  24. package/dist/src/constants.d.ts +16 -17
  25. package/dist/src/constants.d.ts.map +1 -1
  26. package/dist/src/decorator.d.ts +41 -41
  27. package/dist/src/decorator.d.ts.map +1 -1
  28. package/dist/src/errors/CircularDependencyError.d.ts +5 -5
  29. package/dist/src/errors/CircularDependencyError.d.ts.map +1 -1
  30. package/dist/src/errors/InjectFailedError.d.ts +5 -5
  31. package/dist/src/errors/InjectFailedError.d.ts.map +1 -1
  32. package/dist/src/errors/ProviderNotValidError.d.ts +5 -5
  33. package/dist/src/errors/ProviderNotValidError.d.ts.map +1 -1
  34. package/dist/src/errors/TokenNotFoundError.d.ts +5 -5
  35. package/dist/src/errors/TokenNotFoundError.d.ts.map +1 -1
  36. package/dist/src/errors/index.d.ts +4 -4
  37. package/dist/src/errors/index.d.ts.map +1 -1
  38. package/dist/src/forwardRef.d.ts +6 -6
  39. package/dist/src/forwardRef.d.ts.map +1 -1
  40. package/dist/src/index.d.ts +7 -7
  41. package/dist/src/index.d.ts.map +1 -1
  42. package/dist/src/utils.d.ts +3 -2
  43. package/dist/src/utils.d.ts.map +1 -1
  44. package/package.json +38 -37
  45. package/CHANGELOG.md +0 -128
  46. package/dist/src/types/index.d.ts +0 -2
  47. package/dist/src/types/index.d.ts.map +0 -1
package/README.md CHANGED
@@ -3,46 +3,77 @@
3
3
 
4
4
  [![Build Status](https://github.com/kaokei/di/actions/workflows/build.yml/badge.svg)](https://github.com/kaokei/di/actions/workflows/build.yml)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/kaokei/di/badge.svg?branch=main)](https://coveralls.io/github/kaokei/di?branch=main)
6
+ [![Downloads](https://img.shields.io/npm/dm/@kaokei/di.svg?sanitize=true)](https://npmcharts.com/compare/@kaokei/di?minimal=true)
7
+ [![Version](https://img.shields.io/npm/v/@kaokei/di.svg?sanitize=true)](https://www.npmjs.com/package/@kaokei/di)
8
+ [![License](https://img.shields.io/npm/l/@kaokei/di.svg?sanitize=true)](https://www.npmjs.com/package/@kaokei/di)
6
9
 
7
10
  </div>
8
11
 
9
- ## 文档
12
+ - [documentation](https://kaokei.com/project/di/)
13
+ - [online playground](https://codesandbox.io/s/di-playground-zjnyv)
14
+ - [online demo](https://kaokei.com/project/di/)
10
15
 
11
- [Document](https://www.kaokei.com/project/di/)
16
+ ```bash
17
+ npm install @kaokei/di reflect-metadata
18
+ ```
12
19
 
13
- ## 解决了什么问题?
20
+ ## 灵感
14
21
 
15
- 基于 typescript 实现依赖注入能力。类似的目前比较流行的 library 是`InversifyJS`。
22
+ Inspired by [Spring](https://spring.io/), [Angular](https://angular.io/), [typedi](https://github.com/typestack/typedi), [InversifyJS](https://github.com/inversify/InversifyJS)
16
23
 
17
24
  其他类似的 library 可以[参考这里](https://github.com/topics/ioc?l=typescript)和[参考这里](https://github.com/topics/dependency-injection?l=typescript)
18
25
 
19
- ### playground
26
+ 最初接触依赖注入是从 Spring 中开始的,依稀记得我的第一份工作是 java 开发,在第一次开发业务需求的时候,还只知道`new`一个类来获取对象。然后我就在代码中 new 了很多实例对象。不出意外的出现问题了,组长就开始帮我调试代码,最终组长告诉我不要自己手动`new`实例对象,只需要在`XML`文件中配置对应的 bean 对象即可。这给当时的我留下了深刻的印象。现在回想起来,如果我是当时的组长,我肯定会非常生气,连这么基础的东西都不知道。
27
+
28
+ 后来又接触到 Angular,第一次了解到依赖注入在前端的应用。最关键的是我之前有使用过 vue 和 react,先入为主的认为 Angular 中一定也有一个全局的 store,但是却怎么也找不到这个 store 对象,反而都是碎片化的 service。后来才反应过来,多 service 其实可以理解为多 store,如果整个项目只有一个 service,那么不就退化成和 vue 以及 react 一样了吗?只不过 service 在 Angular 中非常方便使用而且易于控制作用范围,相对的 vue 中的 store 的 module 本质还是全局范围可见的不同命名空间而已。
20
29
 
21
- https://codesandbox.io/s/di-playground-zjnyv
30
+ 所以我的想法就是要把 angular 的 service 特性迁移到 vue 和 react 中。本库作为一个基础库,被[user-vue-service](https://github.com/kaokei/use-vue-service)等库依赖。
31
+
32
+ ## 解决了什么问题?
22
33
 
23
- ### 已经支持的特性
34
+ 基于 typescript 实现依赖注入能力。类似的目前比较流行的 library 是`InversifyJS`和`typedi`。
35
+
36
+ ## 已经支持的特性
24
37
 
25
38
  - 支持类,Symbol,字符串等作为服务的 token
26
39
  - 支持类,值,工厂函数等作为服务
27
40
  - 支持@Inject、@Skip、@Self、@Optional 以及 forwardRef 控制获取服务
28
41
  - 支持@Optional 配置可选注入以及类属性的初始值作为默认值
29
42
  - Injector 作为 DI 容器,易于测试
30
- - 同一个服务在同一个 Injector 中一定是单例的
43
+ - 同一个服务在同一个 Injector 中一定是单例的。相对的[Inversify 支持 3 种 Scope](https://github.com/inversify/InversifyJS/blob/master/wiki/scope.md)。
31
44
  - 支持分层 DI 系统
32
45
  - 支持属性注入和构造函数注入
33
46
  - 支持部分循环依赖,对于不支持的循环依赖会有相应的提示
34
47
  - 支持类型提示,以及 InjectionKey 的类型提示
35
48
  - 支持 dispose 生命周期钩子
36
49
 
37
- ### 不支持的特性
50
+ ## 不支持的特性
38
51
 
39
- - 不支持 multi-injection
40
- - 不支持不能自己 new 的注入,比如 react 类组件的实例化过程我们干预不了,因为 Injector 容器内部非常重要的一个工作就是 new 一个对象。
52
+ - 不支持 multi-injection,有需要可以[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/multi_injection.md)
53
+ - 不支持不能自己 new 的注入,比如 react 类组件的实例化过程我们干预不了,所以不能在 react 类组件上使用依赖注入。有需要可以[参考这里](https://itnext.io/dependency-injection-in-react-using-inversifyjs-a38ff0c6601)
41
54
  - 对于类的继承没有过多的支持,[参考这里](https://docs.typestack.community/typedi/v/develop/02-basic-usage-guide/07-inheritance)以及[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/inheritance.md)
42
55
  - 暂时不实现 providers 中使用 forwardRef,主要是不了解使用场景,目前只支持在@Inject 中使用 forwardRef
43
56
  - 不支持异步实例化,即该实例对象的状态和数据依赖服务器端数据,需要异步获取,等待获取数据成功之后才实例化对象。对于这种场景建议在实例对象中维护类似 loading/ready/inited 这种字段来表明数据是否准备完毕。
44
57
  - 不支持 LazyInject
45
- - 不支持 onInit 生命周期钩子,虽然 Spring 中时支持的,但是实际场景中并不常用,[参考这里](https://github.com/angular/angular/issues/23235)
46
- - 不支持自定义装饰器,比如@Prev、@Post 等装饰器,类似 autobind,应该独立维护
47
- - 原来确实导出了 autobind,后续删除了该导出,有业务需要可以自己 `npm install autobind-decorator` 即可
48
- - 不支持中间件
58
+ - 不支持 onInit/PostConstruct 生命周期钩子,虽然 Spring 中是支持的,但是实际场景中并不常用,[参考这里](https://github.com/angular/angular/issues/23235)以及[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/post_construct.md)
59
+ - 不支持中间件,有需要可以[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/middleware.md)
60
+ - 原来确实导出了 autobind,后续删除了该导出,有业务需要可以自己 `npm install autobind-decorator` 即可。
61
+ - 没有实现自定义装饰器,比如@Prev、@Post、@Aop 等装饰器,类似 autobind,应该独立维护。
62
+
63
+ ## 已知问题
64
+
65
+ 循环依赖导致的问题运行时报错。
66
+
67
+ - [Design:type metadata for cyclic dependencies throw at runtime #27519](https://github.com/microsoft/TypeScript/issues/27519)
68
+ - [Support for classes](https://github.com/inversify/InversifyJS/blob/master/wiki/classes_as_id.md#known-limitation-classes-as-identifiers-and-circular-dependencies)
69
+
70
+ 经过一番探究,有不同的场景会导致这个问题。
71
+
72
+ 问题一:A 类和 B 类循环依赖。
73
+
74
+ 解决方案:使用 forwardRef 解藕 A 和 B 的实例化过程。类似 InversifyJS 中的 lazyInject。
75
+
76
+ 问题二:已经使用 forawrdRef 解藕了 A 和 B。仍然有运行时错误。
77
+
78
+ 解决方案:这是因为 tsconfig.json 中的 target 不是 es5。通过测试发现只有设置为 es5 才能避免这个问题。
79
+ 可以[参考这个项目](https://github.com/kaokei/test-webpack-typescript)