UNPKG

objbox

Version:

基于typescript装饰器的轻量级IOC容器

77 lines (69 loc) 3.56 kB
import { ComponentCreatedType, getFunName, registerClass, Component, ComponentHandler, ComponentHandlerInterface, ObjBoxHelper, ObjBoxInterface, ScannedTemplate } from "objbox" //自定义class注解%AnnotationName%的参数 export interface %AnnotationName%Arg{ yourArg1: any //注解参数1 yourArg2: any //注解参数2 } /** * 自定义class注解:%AnnotationName% * @param yourArg1 * @param yourArg2 */ export function %AnnotationName% (options:%AnnotationName%Arg): ClassDecorator { //利用异常栈信息获取当前函数名称,等效于let _annotationName = "%AnnotationName%",之所以不用字符串常量是防止将函数名称修改之后忘记修改字符串常量(防呆) let _annotationName = getFunName(2); return function (target: Function): any { // 如果你希望这个注解能够直接成为组件注解被objbox自动管理,就手动给当前注解手动添加注册 // registerClass(Component.name, { name: target.name, scope: ComponentCreatedType.Singleton }, target) registerClass<%AnnotationName%Arg>(_annotationName, options, target) } } // 防止ts编译优化将函数名称擦除 Object.defineProperty(%AnnotationName%, "name", { value: "%AnnotationName%" }); /** * 实现注解 @%AnnotationName%(<%AnnotationName%Arg>) 功能 */ @ComponentHandler() export class %AnnotationName%AnnotationHandler implements ComponentHandlerInterface { // 模板刚扫描完成 scanned(objbox: ObjBoxInterface, template: ScannedTemplate) { if (ObjBoxHelper.doesTemplateHaveClassAnnotation(%AnnotationName%.name, template)) { let classInfoAnno = ObjBoxHelper.getClassAnnotationFromTemplate<%AnnotationName%Arg>(%AnnotationName%.name, template) if (classInfoAnno != null) { // 这里加工被扫描的组件模版 } } } // 组件刚创建完成 afterCreated(objbox: ObjBoxInterface, template: ScannedTemplate, component: any){ if (ObjBoxHelper.doesComponentHaveClassAnnotation(%AnnotationName%.name, component)) { let classInfoAnno = ObjBoxHelper.getClassAnnotationFromComponent<%AnnotationName%Arg>(%AnnotationName%.name, component) if (classInfoAnno != null) { // 这里加工刚刚创建好但是并未进行任何注入的原始组件对象 } } } // 组件完成注入与配置 afterCompleted(objbox: ObjBoxInterface, template: ScannedTemplate, component: any){ if (ObjBoxHelper.doesComponentHaveClassAnnotation(%AnnotationName%.name, component)) { let classInfoAnno = ObjBoxHelper.getClassAnnotationFromComponent<%AnnotationName%Arg>(%AnnotationName%.name, component) if (classInfoAnno != null) { // 这里加工完成对象注入的完全组件对象,此时对象还未被任何其他组件引用(现在是第一次) } } } // objbox准备就绪 beforeReady(objbox: ObjBoxInterface, template: ScannedTemplate, component: any){ if (ObjBoxHelper.doesComponentHaveClassAnnotation(%AnnotationName%.name, component)) { let classInfoAnno = ObjBoxHelper.getClassAnnotationFromComponent<%AnnotationName%Arg>(%AnnotationName%.name, component) if (classInfoAnno != null) { // objbox准备就绪,下一步就是整个框架开始运行 } } } }