@@ -2,7 +2,11 @@ import { Application } from 'egg';
22import { Loader , TeggError } from '@eggjs/tegg-metadata' ;
33import {
44 AccessLevel ,
5- EggProtoImplClass , InitTypeQualifierAttribute , LoadUnitNameQualifierAttribute ,
5+ EggProtoImplClass ,
6+ EggQualifierAttribute ,
7+ EggType ,
8+ InitTypeQualifierAttribute ,
9+ LoadUnitNameQualifierAttribute ,
610 ObjectInitType ,
711 PrototypeUtil ,
812 QualifierUtil ,
@@ -12,9 +16,9 @@ import { COMPATIBLE_PROTO_IMPLE_TYPE } from './EggCompatibleProtoImpl';
1216import { BackgroundTaskHelper } from '@eggjs/tegg-background-task' ;
1317import { EggObjectFactory } from '@eggjs/tegg-dynamic-inject-runtime' ;
1418
15- const APP_CLAZZ_BLACK_LIST = [ 'eggObjectFactory' ] ;
16- const DEFAULT_APP_CLAZZ = [ ] ;
17- const DEFAULT_CONTEXT_CLAZZ = [
19+ export const APP_CLAZZ_BLACK_LIST = [ 'eggObjectFactory' ] ;
20+ export const DEFAULT_APP_CLAZZ : string [ ] = [ ] ;
21+ export const DEFAULT_CONTEXT_CLAZZ = [
1822 'user' ,
1923] ;
2024
@@ -25,46 +29,41 @@ export class EggAppLoader implements Loader {
2529 this . app = app ;
2630 }
2731
28- private buildAppClazz ( name : string ) : EggProtoImplClass {
32+ private buildClazz ( name : string , eggType : EggType ) : EggProtoImplClass {
2933 const app = this . app ;
30- const func : EggProtoImplClass = function ( ) {
31- return app [ name ] ;
32- } as any ;
33- PrototypeUtil . setIsEggPrototype ( func ) ;
34- PrototypeUtil . setFilePath ( func , 'mock_file_path' ) ;
35- PrototypeUtil . setProperty ( func , {
36- name,
37- initType : ObjectInitType . SINGLETON ,
38- accessLevel : AccessLevel . PUBLIC ,
39- protoImplType : COMPATIBLE_PROTO_IMPLE_TYPE ,
40- } ) ;
41- QualifierUtil . addProtoQualifier ( func , LoadUnitNameQualifierAttribute , 'app' ) ;
42- QualifierUtil . addProtoQualifier ( func , InitTypeQualifierAttribute , ObjectInitType . SINGLETON ) ;
43- return func ;
44- }
45-
46- private buildCtxClazz ( name : string ) : EggProtoImplClass {
47- const temp = {
48- [ name ] : function ( ctx ) {
34+ let func : EggProtoImplClass ;
35+ if ( eggType === EggType . APP ) {
36+ func = function ( ) {
37+ return app [ name ] ;
38+ } as any ;
39+ } else {
40+ func = function ( ) {
41+ const ctx = app . currentContext ;
4942 if ( ! ctx ) {
5043 // ctx has been destroyed, throw humanize error info
5144 throw TeggError . create ( `Can not read property \`${ name } \` because egg ctx has been destroyed` , 'read_after_ctx_destroyed' ) ;
5245 }
5346
5447 return ctx [ name ] ;
55- } as any ,
56- } ;
57- const func = temp [ name ] ;
48+ } as any ;
49+ }
50+ Object . defineProperty ( func , 'name' , {
51+ value : name ,
52+ writable : false ,
53+ enumerable : false ,
54+ configurable : true ,
55+ } ) ;
5856 PrototypeUtil . setIsEggPrototype ( func ) ;
5957 PrototypeUtil . setFilePath ( func , 'mock_file_path' ) ;
6058 PrototypeUtil . setProperty ( func , {
6159 name,
62- initType : ObjectInitType . CONTEXT ,
60+ initType : ObjectInitType . SINGLETON ,
6361 accessLevel : AccessLevel . PUBLIC ,
6462 protoImplType : COMPATIBLE_PROTO_IMPLE_TYPE ,
6563 } ) ;
6664 QualifierUtil . addProtoQualifier ( func , LoadUnitNameQualifierAttribute , 'app' ) ;
67- QualifierUtil . addProtoQualifier ( func , InitTypeQualifierAttribute , ObjectInitType . CONTEXT ) ;
65+ QualifierUtil . addProtoQualifier ( func , InitTypeQualifierAttribute , ObjectInitType . SINGLETON ) ;
66+ QualifierUtil . addProtoQualifier ( func , EggQualifierAttribute , eggType ) ;
6867 return func ;
6968 }
7069
@@ -73,6 +72,12 @@ export class EggAppLoader implements Loader {
7372 const func : EggProtoImplClass = function ( ) {
7473 return app . getLogger ( name ) ;
7574 } as any ;
75+ Object . defineProperty ( func , 'name' , {
76+ value : name ,
77+ writable : false ,
78+ enumerable : false ,
79+ configurable : true ,
80+ } ) ;
7681 PrototypeUtil . setIsEggPrototype ( func ) ;
7782 PrototypeUtil . setFilePath ( func , 'mock_file_path' ) ;
7883 PrototypeUtil . setProperty ( func , {
@@ -83,26 +88,7 @@ export class EggAppLoader implements Loader {
8388 } ) ;
8489 QualifierUtil . addProtoQualifier ( func , LoadUnitNameQualifierAttribute , 'app' ) ;
8590 QualifierUtil . addProtoQualifier ( func , InitTypeQualifierAttribute , ObjectInitType . SINGLETON ) ;
86- return func ;
87- }
88-
89- private buildCtxLoggerClazz ( name : string ) : EggProtoImplClass {
90- const temp = {
91- [ name ] : function ( ctx ) {
92- return ctx . getLogger ( name ) ;
93- } as any ,
94- } ;
95- const func = temp [ name ] ;
96- PrototypeUtil . setIsEggPrototype ( func ) ;
97- PrototypeUtil . setFilePath ( func , 'mock_file_path' ) ;
98- PrototypeUtil . setProperty ( func , {
99- name,
100- initType : ObjectInitType . CONTEXT ,
101- accessLevel : AccessLevel . PUBLIC ,
102- protoImplType : COMPATIBLE_PROTO_IMPLE_TYPE ,
103- } ) ;
104- QualifierUtil . addProtoQualifier ( func , LoadUnitNameQualifierAttribute , 'app' ) ;
105- QualifierUtil . addProtoQualifier ( func , InitTypeQualifierAttribute , ObjectInitType . CONTEXT ) ;
91+ QualifierUtil . addProtoQualifier ( func , EggQualifierAttribute , EggType . APP ) ;
10692 return func ;
10793 }
10894
@@ -128,16 +114,14 @@ export class EggAppLoader implements Loader {
128114 ...DEFAULT_CONTEXT_CLAZZ ,
129115 ] ) ) ;
130116 const loggerNames = this . getLoggerNames ( allContextClazzNames ) ;
131- const allSingletonClazzs = allSingletonClazzNames . map ( name => this . buildAppClazz ( name ) ) ;
132- const allContextClazzs = allContextClazzNames . map ( name => this . buildCtxClazz ( name ) ) ;
117+ const allSingletonClazzs = allSingletonClazzNames . map ( name => this . buildClazz ( name , EggType . APP ) ) ;
118+ const allContextClazzs = allContextClazzNames . map ( name => this . buildClazz ( name , EggType . CONTEXT ) ) ;
133119 const appLoggerClazzs = loggerNames . map ( name => this . buildAppLoggerClazz ( name ) ) ;
134- const ctxLoggerClazzs = loggerNames . map ( name => this . buildCtxLoggerClazz ( name ) ) ;
135120
136121 return [
137122 ...allSingletonClazzs ,
138123 ...allContextClazzs ,
139124 ...appLoggerClazzs ,
140- ...ctxLoggerClazzs ,
141125
142126 // inner helper class list
143127 // TODO: should auto the inner class
0 commit comments