@kaokei/di 1.0.22 → 1.1.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 (41) hide show
  1. package/CHANGELOG.md +135 -0
  2. package/README.md +28 -105
  3. package/dist/index.cjs.js +450 -233
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.cjs.min.js +3 -3
  6. package/dist/index.cjs.min.js.map +1 -1
  7. package/dist/index.cjs.runtime.js +444 -229
  8. package/dist/index.cjs.runtime.js.map +1 -1
  9. package/dist/index.cjs.runtime.min.js +3 -3
  10. package/dist/index.cjs.runtime.min.js.map +1 -1
  11. package/dist/index.esm.js +451 -219
  12. package/dist/index.esm.js.map +1 -1
  13. package/dist/index.esm.min.js +4 -4
  14. package/dist/index.esm.min.js.map +1 -1
  15. package/dist/index.esm.runtime.js +439 -214
  16. package/dist/index.esm.runtime.js.map +1 -1
  17. package/dist/index.esm.runtime.min.js +4 -4
  18. package/dist/index.esm.runtime.min.js.map +1 -1
  19. package/dist/index.iife.js +457 -237
  20. package/dist/index.iife.js.map +1 -1
  21. package/dist/index.iife.min.js +4 -4
  22. package/dist/index.iife.min.js.map +1 -1
  23. package/dist/src/Injector.d.ts +67 -32
  24. package/dist/src/Injector.d.ts.map +1 -1
  25. package/dist/src/constants.d.ts +5 -11
  26. package/dist/src/constants.d.ts.map +1 -1
  27. package/dist/src/decorator.d.ts +13 -1
  28. package/dist/src/decorator.d.ts.map +1 -1
  29. package/dist/src/errors/CircularDependencyError.d.ts +6 -0
  30. package/dist/src/errors/CircularDependencyError.d.ts.map +1 -0
  31. package/dist/src/errors/InjectFailedError.d.ts +6 -0
  32. package/dist/src/errors/InjectFailedError.d.ts.map +1 -0
  33. package/dist/src/errors/ProviderNotValidError.d.ts +6 -0
  34. package/dist/src/errors/ProviderNotValidError.d.ts.map +1 -0
  35. package/dist/src/errors/TokenNotFoundError.d.ts +6 -0
  36. package/dist/src/errors/TokenNotFoundError.d.ts.map +1 -0
  37. package/dist/src/errors/index.d.ts +5 -0
  38. package/dist/src/errors/index.d.ts.map +1 -0
  39. package/dist/src/index.d.ts +1 -1
  40. package/dist/src/index.d.ts.map +1 -1
  41. package/package.json +5 -7
package/CHANGELOG.md ADDED
@@ -0,0 +1,135 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+ ## [1.1.0](https://github.com/kaokei/di/compare/v1.0.25...v1.1.0) (2022-02-11)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **typo:** 修复文字错误 ([2fdb91f](https://github.com/kaokei/di/commit/2fdb91ff95eab8c1c13153e5c267ce584bcf985b))
11
+
12
+ ### [1.0.25](https://github.com/kaokei/di/compare/v1.0.24...v1.0.25) (2021-12-11)
13
+
14
+ ### [1.0.24](https://github.com/kaokei/di/compare/v1.0.23...v1.0.24) (2021-12-11)
15
+
16
+ ### [1.0.23](https://github.com/kaokei/di/compare/v1.0.22...v1.0.23) (2021-12-10)
17
+
18
+
19
+ ### Features
20
+
21
+ * **default:** 删除了defaultValue ([7cae1d0](https://github.com/kaokei/di/commit/7cae1d039298b39ec7fccd793a804743ec43ab6c))
22
+
23
+
24
+ ### Bug Fixes
25
+
26
+ * **fix:** catch dispose error ([2ee9b4a](https://github.com/kaokei/di/commit/2ee9b4a69158e8a5741144170615beb25c2e84a7))
27
+ * **fix:** 修复了判断InjectFailed场景的错误 ([c97f092](https://github.com/kaokei/di/commit/c97f092744e178236b1317aab108f92d337eb943))
28
+
29
+ ### 1.0.22 (2021-10-07)
30
+
31
+
32
+ ### Bug Fixes
33
+
34
+ * **bugfix:** 修复dispose的bug ([eb5116d](https://github.com/kaokei/di/commit/eb5116d28139ad7c21738e089ce764e76609b3c6))
35
+
36
+ ### [1.0.21](https://github.com/kaokei/di/compare/v1.0.20...v1.0.21) (2021-09-16)
37
+
38
+
39
+ ### Features
40
+
41
+ * **inject:** 修改Inject的参数是必填项 ([2ec3dbe](https://github.com/kaokei/di/commit/2ec3dbeea8dc02ab0d8b43ce104192626cb8a9dd))
42
+
43
+ ### [1.0.20](https://github.com/kaokei/di/compare/v1.0.19...v1.0.20) (2021-08-22)
44
+
45
+
46
+ ### Bug Fixes
47
+
48
+ * **reflect-metadata:** import reflect-metadata ([5e21184](https://github.com/kaokei/di/commit/5e21184f922f32d97640b42c96982313dc5c9ea4))
49
+
50
+ ### [1.0.19](https://github.com/kaokei/di/compare/v1.0.18...v1.0.19) (2021-08-22)
51
+
52
+
53
+ ### Bug Fixes
54
+
55
+ * **fix export:** 重新导出autobind ([ea721c2](https://github.com/kaokei/di/commit/ea721c28be8c0734e43b95a01bc234843346ca5e))
56
+
57
+ ### [1.0.18](https://github.com/kaokei/di/compare/v1.0.17...v1.0.18) (2021-08-21)
58
+
59
+ ### [1.0.17](https://github.com/kaokei/di/compare/v1.0.16...v1.0.17) (2021-08-21)
60
+
61
+
62
+ ### Bug Fixes
63
+
64
+ * **autobind:** export autobind ([4f2f4c8](https://github.com/kaokei/di/commit/4f2f4c83bbdb0bcb87bc44df608bdb6fc4858ec2))
65
+
66
+ ### [1.0.16](https://github.com/kaokei/di/compare/v1.0.15...v1.0.16) (2021-08-21)
67
+
68
+ ### [1.0.15](https://github.com/kaokei/di/compare/v1.0.14...v1.0.15) (2021-08-21)
69
+
70
+
71
+ ### Features
72
+
73
+ * **autobind:** add autobind-decorator pacakge ([ff60ead](https://github.com/kaokei/di/commit/ff60ead4eb892a15249d8ef74153451c16e0c696))
74
+
75
+ ### [1.0.14](https://github.com/kaokei/di/compare/v1.0.13...v1.0.14) (2021-08-06)
76
+
77
+
78
+ ### Features
79
+
80
+ * **util:** export utils ([458d75e](https://github.com/kaokei/di/commit/458d75e7583d4ca6bfa3c32755daf9b1e1f17b1a))
81
+
82
+ ### [1.0.13](https://github.com/kaokei/di/compare/v1.0.12...v1.0.13) (2021-08-06)
83
+
84
+
85
+ ### Features
86
+
87
+ * **merge:** 支持mergeHook ([427a1e2](https://github.com/kaokei/di/commit/427a1e2f9681284e579a96a94dce0f4ee96f0414))
88
+
89
+ ### [1.0.12](https://github.com/kaokei/di/compare/v1.0.10...v1.0.12) (2021-08-04)
90
+
91
+
92
+ ### Features
93
+
94
+ * **injector:** injector support dispose ([7b1246d](https://github.com/kaokei/di/commit/7b1246d31ad0a621fe7ee6e0b007d621fb28643b))
95
+
96
+ ### [1.0.10](https://github.com/kaokei/di/compare/v1.0.9...v1.0.10) (2021-08-03)
97
+
98
+ ### [1.0.9](https://github.com/kaokei/di/compare/v1.0.8...v1.0.9) (2021-07-01)
99
+
100
+ ### [1.0.8](https://github.com/kaokei/di/compare/v1.0.7...v1.0.8) (2021-06-29)
101
+
102
+
103
+ ### Bug Fixes
104
+
105
+ * **package.json:** set reflect-metadata to be peer dependency ([4368411](https://github.com/kaokei/di/commit/4368411ce44d0f0f754c8bf8e3f263e3f27dbed2))
106
+
107
+ ### [1.0.7](https://github.com/kaokei/di/compare/v1.0.6...v1.0.7) (2021-06-29)
108
+
109
+ ### [1.0.6](https://github.com/kaokei/di/compare/v1.0.5...v1.0.6) (2021-06-29)
110
+
111
+
112
+ ### Bug Fixes
113
+
114
+ * **index:** change location of reflect-metadata ([b7b8e03](https://github.com/kaokei/di/commit/b7b8e033554a3ea4604d3753947be5762e4b9032))
115
+
116
+ ### [1.0.5](https://github.com/kaokei/di/compare/v1.0.4...v1.0.5) (2021-06-29)
117
+
118
+
119
+ ### Bug Fixes
120
+
121
+ * **injector:** should return postHook service ([6c73a3e](https://github.com/kaokei/di/commit/6c73a3eb43a6d1670d446fcaaadcd1178ab51fda))
122
+
123
+ ### [1.0.4](https://github.com/kaokei/di/compare/v1.0.3...v1.0.4) (2021-06-29)
124
+
125
+
126
+ ### Features
127
+
128
+ * **injector:** add postHook ([db75b02](https://github.com/kaokei/di/commit/db75b02e049017ed959a83e310b3cb96e9514060))
129
+
130
+ ### [1.0.3](https://github.com/kaokei/di/compare/v1.0.1...v1.0.3) (2021-06-29)
131
+
132
+
133
+ ### Bug Fixes
134
+
135
+ * **ci:** modify ci name ([b43f69c](https://github.com/kaokei/di/commit/b43f69cbdfe2069f559045a95efc92a956b04d20))
package/README.md CHANGED
@@ -1,131 +1,54 @@
1
1
  <h1 align="center">di</h1>
2
2
  <div align="center">
3
3
 
4
- [![Build Status](https://github.com/kaokei/di/actions/workflows/build.yml/badge.svg)](https://github.com/kaokei/di/actions/workflows/build.yml) [![Coverage Status](https://coveralls.io/repos/github/kaokei/di/badge.svg?branch=main)](https://coveralls.io/github/kaokei/di?branch=main)
4
+ [![Build Status](https://github.com/kaokei/di/actions/workflows/build.yml/badge.svg)](https://github.com/kaokei/di/actions/workflows/build.yml)
5
+ [![Coverage Status](https://coveralls.io/repos/github/kaokei/di/badge.svg?branch=main)](https://coveralls.io/github/kaokei/di?branch=main)
5
6
 
6
7
  </div>
7
8
 
8
- ## 待修改的 5 个名字
9
+ ## 灵感
9
10
 
10
- - 项目名,也就是项目文件夹的名称,这个在我们创建项目时就已经指定了。
11
- - package.json 中的 name,这个大多数时可能就是和项目名一致了,但是如果是带有 scope 的,或者驼峰的,都需要自己修改。
12
- - package.json 中的 browserVariableName,这个大多数情况下不关心也不会有问题,但是如果我们想要编译的代码想要在浏览器中直接使用,最好是指定一个全局变量。
13
- - package.json 中的 homepage,bugs-url,repository-url 这些外部链接。
14
- - README.md 中的 github 地址。
11
+ Inspired by [Spring](https://spring.io/), [Angular](https://angular.io/), [typedi](https://github.com/typestack/typedi), [InversifyJS](https://github.com/inversify/InversifyJS)
15
12
 
16
- ## github 地址
13
+ 其他类似的 library 可以[参考这里](https://github.com/topics/ioc?l=typescript)和[参考这里](https://github.com/topics/dependency-injection?l=typescript)
17
14
 
18
- - [github](https://github.com/kaokei/di)
15
+ 最初接触依赖注入是从 Spring 中开始的,依稀记得我的第一份工作是 java 开发,在第一次开发业务需求的时候,还只知道`new`一个类来获取对象。然后我就在代码中 new 了很多实例对象。不出意外的出现问题了,组长就开始帮我调试代码,最终组长告诉我不要自己手动`new`实例对象,只需要在`XML`文件中配置对应的 bean 对象即可。这给当时的我留下了深刻的印象。现在回想起来,如果我是当时的组长,我肯定会非常生气,连这么基础的东西都不知道。
19
16
 
20
- ## 解决了什么问题?
21
-
22
- ## 整体方案以及使用方式
23
-
24
- ## 特性
25
-
26
- 使用 typescript,并且类型定义统一在 types 文件夹中,建议使用 module 来管理类型,而不是 script 来创建全局的类型。
27
- `tsconfig.json`作为编辑器的默认配置文件,方便编辑器识别。实际构建时使用`tsconfig.app.json`文件
28
-
29
- 使用 esm 模块化规范
30
-
31
- 使用 npm 作为包管理
32
-
33
- 使用 git 作为代码版本工具。
34
-
35
- 编码规范使用 eslint+prettier+editorconfig
36
-
37
- git commit message 采用 angular 规范,以及使用 commitlint 校验
38
-
39
- 使用 yorkie 自动化校验并格式化代码,自动化校验 commit message
40
-
41
- 使用 jest 作为单元测试,统一放在`tests`文件夹中。
42
-
43
- 可以在 playground 中进行代码实验,使用 vscode 配置.vscode/launch.json 可以调试 nodejs
44
-
45
- 使用 rollup 作为打包工具,同时打包出多个版本的 bundle。支持压缩/未压缩、使用 runtime/不使用 runtime、commonjs/esm、浏览器版本总共 10 个版本。
46
-
47
- npm run release:first 第一次发布,会自动创建 CHANGELOG.md 文件
48
- npm run release patch 发布新版本
49
-
50
- 使用 MIT 作为开源协议
51
-
52
- ### 循环依赖
53
-
54
- 一般来讲循环依赖在不同的场景下有不同的含义。
55
-
56
- 举个例子:
57
-
58
- 小明眼睛看不见,小王腿走不了路。所以小明要看书时就需要小王来帮忙。小王要走路的时候就需要小明来帮忙。
59
-
60
- 这个例子中,可以很明显的看出来小明和小王之间是存在互相依赖的。但是这种互相依赖具体是不是循环依赖取决于具体的场景。
17
+ 后来又接触到 Angular,第一次了解到依赖注入在前端的应用。最关键的是我之前有使用过 vue 和 react,先入为主的认为 Angular 中一定也有一个全局的 store,但是却怎么也找不到这个 store 对象,反而都是碎片化的 service。后来才反应过来,多 service 其实可以理解为多 store,如果整个项目只有一个 service,那么不就退化成和 vue 以及 react 一样了吗?只不过 service 在 Angular 中非常方便使用而且易于控制作用范围,相对的 vue 中的 store 的 module 本质还是全局范围可见的不同命名空间而已。
61
18
 
62
- 考虑这样的场景,就是循环依赖了。小明要看书,请求小王帮忙读书,小王说读书可以是可以,但是我一直坐在椅子上,没有力气读书,你背我走一走吧,小明说你先帮我读书,我再被你走路,小王说你先背我走路,我再帮你读书。
19
+ 所以我的想法就是要把 angular 的 service 特性迁移到 vue 和 react 中。本库作为一个基础库,被[user-vue-service](https://github.com/kaokei/use-vue-service)等库依赖。
63
20
 
64
- 像这种同一时刻,两种资源同时互相依赖即可称为循环依赖。
65
-
66
- 再回归到 es6 中,在 es6 的模块定义的时候,一般“较少”有循环依赖的场景。但是互相依赖的场景则是非常常见的。
67
-
68
- 比如调用 A 模块中的 a 方法时,会调用 B 模块中的 b 方法。就算 b 方法再次调用 a 方法,只要这种循环调用会中止即可,这也只是互相依赖,而不是循环依赖。
69
-
70
- 上面说到的“较少”有循环依赖的场景。常见场景有这两个:
71
-
72
- 场景一:A extends B 同时 B extends A,即 extends 关键字会导致循环依赖。
73
- 场景二:@decorator(A) class B {} 同时 @decorator(B) class A {}。装饰器也是立即执行的,也会导致循环依赖。
74
-
75
- ### 对循环依赖的态度
76
-
77
- 业务中应该尽量避免循环依赖,如果实在避免不了,那么就需要仔细分析各个模块之间的依赖关系。需要手动解耦各个模块。使之变为互相依赖。
78
-
79
- 目前的 es6/babel/typescript 都不能直接帮我们解决这种循环依赖,但是应该有相应的提示信息辅助我们发现潜在的循环依赖。
80
-
81
- ### 关于循环依赖的讨论
82
-
83
- 看了半天,总结起来就是问题是客观存在的,只能具体问题具体分析。并不存在一劳永逸的方案。
84
-
85
- [How to solve this basic ES6-module circular dependency problem?](https://esdiscuss.org/topic/how-to-solve-this-basic-es6-module-circular-dependency-problem)
86
-
87
- [TypeScript needs to support circular references between classes in separate files.](https://github.com/Microsoft/TypeScript/issues/20361)
88
-
89
- [Angular 2 Forward Reference](https://segmentfault.com/a/1190000008626276)
90
-
91
- ### 大佬怎么解决循环依赖的
92
-
93
- 我没能复现这种解决方案,感觉并不能解决问题
94
-
95
- [How to fix nasty circular dependency issues once and for all in JavaScript & TypeScript](https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de)
96
-
97
- [How to fix this ES6 module circular dependency?](https://stackoverflow.com/questions/38841469/how-to-fix-this-es6-module-circular-dependency)
98
-
99
- [Known Limitation: Classes as identifiers and circular dependencies](https://github.com/inversify/InversifyJS/blob/master/wiki/classes_as_id.md#known-limitation-classes-as-identifiers-and-circular-dependencies)
21
+ ## 解决了什么问题?
100
22
 
101
- [InversifyJS Circular dependencies](https://github.com/inversify/InversifyJS/blob/master/wiki/circular_dependencies.md)
23
+ 基于 typescript 实现依赖注入能力。类似的目前比较流行的 library 是`InversifyJS`和`typedi`。
102
24
 
103
- ### 待支持特性
25
+ [详细文档地址](https://kaokei.com/project/di/)
104
26
 
105
- - 删除了@DefauleValue,使用 ts 自带的默认参数
106
- - usePromise
107
- - 自定义装饰器@Prev @Post
108
- - postConstruct 代替 onInit
109
- - LazyInject 看情况-Vue 目前是不需要的
110
- - middleware
111
- - 各种 forwardRef
27
+ [online playground](https://codesandbox.io/s/di-playground-zjnyv)
112
28
 
113
- ### 已经支持的特性
29
+ ## 已经支持的特性
114
30
 
115
31
  - 支持类,Symbol,字符串等作为服务的 token
116
32
  - 支持类,值,工厂函数等作为服务
117
- - Injector 作为 DI 容器,易于测试
118
33
  - 支持@Inject、@Skip、@Self、@Optional 以及 forwardRef 控制获取服务
119
- - 同一个服务在同一个 Injector 中一定是单例的
34
+ - 支持@Optional 配置可选注入以及类属性的初始值作为默认值
35
+ - Injector 作为 DI 容器,易于测试
36
+ - 同一个服务在同一个 Injector 中一定是单例的。相对的[Inversify 支持 3 种 Scope](https://github.com/inversify/InversifyJS/blob/master/wiki/scope.md)。
120
37
  - 支持分层 DI 系统
121
- - 支持属性注入
38
+ - 支持属性注入和构造函数注入
122
39
  - 支持部分循环依赖,对于不支持的循环依赖会有相应的提示
123
40
  - 支持类型提示,以及 InjectionKey 的类型提示
41
+ - 支持 dispose 生命周期钩子
124
42
 
125
- ### 不支持的特性
43
+ ## 不支持的特性
126
44
 
127
- - 不支持 multi-injection
128
- - 不支持不能自己 new 的注入
129
- - 对于继承没有过多的支持
130
- - 没有 InversifyJS 中复杂的 binding 的 API,尽量采用`new Injector([providers])`
45
+ - 不支持 multi-injection,有需要可以[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/multi_injection.md)
46
+ - 不支持不能自己 new 的注入,比如 react 类组件的实例化过程我们干预不了,所以不能在 react 类组件上使用依赖注入。有需要可以[参考这里](https://itnext.io/dependency-injection-in-react-using-inversifyjs-a38ff0c6601)
47
+ - 对于类的继承没有过多的支持,[参考这里](https://docs.typestack.community/typedi/v/develop/02-basic-usage-guide/07-inheritance)以及[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/inheritance.md)
131
48
  - 暂时不实现 providers 中使用 forwardRef,主要是不了解使用场景,目前只支持在@Inject 中使用 forwardRef
49
+ - 不支持异步实例化,即该实例对象的状态和数据依赖服务器端数据,需要异步获取,等待获取数据成功之后才实例化对象。对于这种场景建议在实例对象中维护类似 loading/ready/inited 这种字段来表明数据是否准备完毕。
50
+ - 不支持 LazyInject
51
+ - 不支持 onInit/PostConstruct 生命周期钩子,虽然 Spring 中是支持的,但是实际场景中并不常用,[参考这里](https://github.com/angular/angular/issues/23235)以及[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/post_construct.md)
52
+ - 不支持中间件,有需要可以[参考这里](https://github.com/inversify/InversifyJS/blob/master/wiki/middleware.md)
53
+ - 原来确实导出了 autobind,后续删除了该导出,有业务需要可以自己 `npm install autobind-decorator` 即可。
54
+ - 没有实现自定义装饰器,比如@Prev、@Post、@Aop 等装饰器,类似 autobind,应该独立维护。