Skip to content

Commit

Permalink
feat: add tag decorator for app reflect (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-zhang authored Jan 7, 2021
1 parent a82f159 commit 4935de6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augejs/module-core",
"version": "1.0.1-rc.11",
"version": "1.0.2-rc.1",
"description": "`@augejs/module-core` is a module framework which support using `dependency injection` way to composite kinds of `Modules` and `Providers` to complex application.",
"main": "dist/main.js",
"directories": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"@types/object-path": "^0.11.0",
"extend": "^3.0.2",
"inversify": "^5.0.1",
"object-path": "^0.11.4",
"object-path": "^0.11.5",
"reflect-metadata": "^0.1.13",
"yargs-parser": "^20.2.1"
},
Expand Down
14 changes: 14 additions & 0 deletions src/decorators/GetContainer.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Container } from '../ioc';

export function GetContainer(context:string = ''):PropertyDecorator {
return (target: Object, propertyKey: string | symbol) => {
const descriptor:PropertyDescriptor = {
get():Container {
return (this as any).$scanNode.context.container
},
};

Object.defineProperty(target, propertyKey, descriptor);
return descriptor;
}
}
18 changes: 18 additions & 0 deletions src/decorators/Tag.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Metadata } from '@augejs/provider-scanner';
export function Tag(name: string): ClassDecorator {
return function(target: Function) {
Tag.defineMetadata(target, name);
}
}

Tag.hasMetadata = (target: Object): boolean => {
return Metadata.hasMetadata(Tag, target)
}

Tag.defineMetadata = (target: Object, name: string)=> {
Metadata.defineInsertEndArrayMetadata(Tag, [ name ], target);
}

Tag.getMetadata = (target: object): string[] => {
return Metadata.getMetadata(Tag, target) || [];
}
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './GetLogger.decorator';
export * from './LifecycleHook.decorator';
export * from './Cluster.decorator';
export * from './GetScanNode.decorator';
export * from './Tag.decorator';

15 changes: 9 additions & 6 deletions src/utils/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { scan, IScanNode, IScanContext, hookUtil, HookMetadata, Metadata } from
import { getConfigAccessPath } from './config.util';
import { objectPath, objectExtend } from './object.util';
import { BindingScopeEnum, Container } from '../ioc';
import { Cluster, Config, ConfigLoader } from '../decorators';
import { Cluster, Config, ConfigLoader, Tag } from '../decorators';
import { ILogger, Logger, ConsoleLogTransport } from '../logger';

const DefaultLifeCyclePhases =
Expand Down Expand Up @@ -65,7 +65,7 @@ export const boot = async (appModule:Function, options?:IBootOptions): Promise<I
bootLoadConfig(options),
bootLifeCyclePhases(),
]),
scanNodeScanHook: scanNodeCoreHook(),
scanNodeScanHook: scanNodeIOCHook(),
});
};

Expand Down Expand Up @@ -210,7 +210,7 @@ function bootLifeCyclePhases() {
}
}

function scanNodeCoreHook() {
function scanNodeIOCHook() {
return async (scanNode: IScanNode, next: Function) => {
const container:Container = scanNode.context.container;
let instanceFactory:Function | null = null;
Expand Down Expand Up @@ -261,9 +261,12 @@ function scanNodeCoreHook() {
if (instance) {
// keep the reference to scanNode
instance.$scanNode = scanNode;
// here is tags in constructor
if (instance?.constructor) {
Tag.getMetadata(instance!.constructor).forEach((tag: string) => {
container.bind(tag).toConstantValue(instance);
});
}
}
}
}



0 comments on commit 4935de6

Please sign in to comment.