Skip to content

Commit

Permalink
feat(plugin): 支持vue plugin
Browse files Browse the repository at this point in the history
支持vue plugin,支持postHook和mergeHook,支持unmount生命周期
  • Loading branch information
kaokei committed Aug 6, 2021
1 parent f740128 commit a4cee12
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
12 changes: 12 additions & 0 deletions ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ watch 功能其实和 useEffect 已经非常相似了。
## 生命周期

主要是 destroy,卸载资源

## hooks

post hook

merge hook

需要这两个 hook 才能配置响应式能力

## 支持 vue 插件

在 app 实例上声明 providers
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"commit": "git-cz"
},
"dependencies": {
"@kaokei/di": "^1.0.12"
"@kaokei/di": "^1.0.14"
},
"peerDependencies": {
"reflect-metadata": "^0.1.13",
Expand Down
8 changes: 8 additions & 0 deletions src/createVuePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { INJECTOR_KEY } from './constants';
import { getInjector } from './utils';

export const createVuePlugin = (providers: any = []) => ({
install: (app: any) => {
app.provide(INJECTOR_KEY, getInjector(providers));
},
});
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// 导出依赖注入需要的装饰器
export { Inject, Self, Skip, Optional, Injectable } from '@kaokei/di';
export {
Inject,
Self,
Skip,
Optional,
Injectable,
forwardRef,
} from '@kaokei/di';

// setup中声明服务提供者
export { declareProviders } from './declareProviders';

// setup中获取服务实例
export { useService } from './useService';

// 在createApp返回的app实例上挂载Injector
export { createVuePlugin } from './createVuePlugin';

// 以下到处不是面向普通用户使用的
// 而是面向第三方库的开发者,比如我开发的支持类组件的库
export { INJECTOR_KEY, DEFAULT_INJECTOR } from './constants';
Expand Down
19 changes: 15 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reactive, ref } from 'vue';
import { Injector } from '@kaokei/di';
import { reactive, ref, proxyRefs } from 'vue';
import { Injector, has } from '@kaokei/di';

function DEFAULT_POST_HOOK(service: any) {
if (service && typeof service === 'object') {
Expand All @@ -9,10 +9,21 @@ function DEFAULT_POST_HOOK(service: any) {
}
}

function DEFAULT_MERGE_HOOK(target: any, source: any) {
for (const key in source) {
if (has(source, key)) {
target[key] = proxyRefs(source[key]);
}
}
return target;
}

export function getInjector(
provides?: any[],
parentInjector?: Injector,
postHook?: any
options: any = {}
) {
return new Injector(provides, parentInjector, postHook || DEFAULT_POST_HOOK);
options.postHook = options.postHook || DEFAULT_POST_HOOK;
options.mergeHook = options.mergeHook || DEFAULT_MERGE_HOOK;
return new Injector(provides, parentInjector, options);
}

0 comments on commit a4cee12

Please sign in to comment.