@eggjs/core-decorator 4.0.0-beta.7 → 4.0.0-beta.8
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.
- package/dist/index.d.ts +193 -13
- package/dist/index.js +531 -15
- package/package.json +3 -3
- package/dist/decorator/ConfigSource.d.ts +0 -4
- package/dist/decorator/ConfigSource.js +0 -13
- package/dist/decorator/ContextProto.d.ts +0 -7
- package/dist/decorator/ContextProto.js +0 -14
- package/dist/decorator/EggQualifier.d.ts +0 -6
- package/dist/decorator/EggQualifier.js +0 -13
- package/dist/decorator/InitTypeQualifier.d.ts +0 -6
- package/dist/decorator/InitTypeQualifier.js +0 -13
- package/dist/decorator/Inject.d.ts +0 -7
- package/dist/decorator/Inject.js +0 -77
- package/dist/decorator/ModuleQualifier.d.ts +0 -4
- package/dist/decorator/ModuleQualifier.js +0 -13
- package/dist/decorator/MultiInstanceInfo.d.ts +0 -6
- package/dist/decorator/MultiInstanceInfo.js +0 -13
- package/dist/decorator/MultiInstanceProto.d.ts +0 -6
- package/dist/decorator/MultiInstanceProto.js +0 -35
- package/dist/decorator/Prototype.d.ts +0 -6
- package/dist/decorator/Prototype.js +0 -27
- package/dist/decorator/SingletonProto.d.ts +0 -7
- package/dist/decorator/SingletonProto.js +0 -14
- package/dist/decorator/index.js +0 -12
- package/dist/util/MetadataUtil.d.ts +0 -39
- package/dist/util/MetadataUtil.js +0 -75
- package/dist/util/PrototypeUtil.d.ts +0 -112
- package/dist/util/PrototypeUtil.js +0 -216
- package/dist/util/QualifierUtil.d.ts +0 -17
- package/dist/util/QualifierUtil.js +0 -67
- package/dist/util/index.js +0 -5
package/dist/index.js
CHANGED
|
@@ -1,19 +1,535 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import "./util/index.js";
|
|
5
|
-
import { ConfigSourceQualifier } from "./decorator/ConfigSource.js";
|
|
6
|
-
import { Prototype } from "./decorator/Prototype.js";
|
|
7
|
-
import { ContextProto } from "./decorator/ContextProto.js";
|
|
8
|
-
import { EggQualifier } from "./decorator/EggQualifier.js";
|
|
9
|
-
import { InitTypeQualifier } from "./decorator/InitTypeQualifier.js";
|
|
10
|
-
import { Inject, InjectOptional } from "./decorator/Inject.js";
|
|
11
|
-
import { ModuleQualifier } from "./decorator/ModuleQualifier.js";
|
|
12
|
-
import { MultiInstanceInfo } from "./decorator/MultiInstanceInfo.js";
|
|
13
|
-
import { MultiInstanceProto } from "./decorator/MultiInstanceProto.js";
|
|
14
|
-
import { SingletonProto } from "./decorator/SingletonProto.js";
|
|
15
|
-
import "./decorator/index.js";
|
|
1
|
+
import { AccessLevel, ConfigSourceQualifierAttribute, DEFAULT_PROTO_IMPL_TYPE, EggQualifierAttribute, InitTypeQualifierAttribute, InjectType, LoadUnitNameQualifierAttribute, MultiInstanceType, ObjectInitType, PROPERTY_QUALIFIER_META_DATA, QUALIFIER_META_DATA } from "@eggjs/tegg-types";
|
|
2
|
+
import { MapUtil, NameUtil, ObjectUtils, StackUtil } from "@eggjs/tegg-common-util";
|
|
3
|
+
import { debuglog } from "node:util";
|
|
16
4
|
|
|
17
5
|
export * from "@eggjs/tegg-types/core-decorator"
|
|
18
6
|
|
|
7
|
+
//#region src/util/MetadataUtil.ts
|
|
8
|
+
var MetadataUtil = class {
|
|
9
|
+
static deleteMetaData(metadataKey, clazz) {
|
|
10
|
+
Reflect.deleteMetadata(metadataKey, clazz);
|
|
11
|
+
}
|
|
12
|
+
static defineMetaData(metadataKey, metadataValue, clazz) {
|
|
13
|
+
Reflect.defineMetadata(metadataKey, metadataValue, clazz);
|
|
14
|
+
}
|
|
15
|
+
static getOwnMetaData(metadataKey, clazz) {
|
|
16
|
+
return Reflect.getOwnMetadata(metadataKey, clazz);
|
|
17
|
+
}
|
|
18
|
+
static hasMetaData(metadataKey, clazz, propKey) {
|
|
19
|
+
return Reflect.hasMetadata(metadataKey, clazz, propKey);
|
|
20
|
+
}
|
|
21
|
+
static getMetaData(metadataKey, clazz, propKey) {
|
|
22
|
+
return Reflect.getMetadata(metadataKey, clazz, propKey);
|
|
23
|
+
}
|
|
24
|
+
static getBooleanMetaData(metadataKey, clazz) {
|
|
25
|
+
return !!this.getMetaData(metadataKey, clazz);
|
|
26
|
+
}
|
|
27
|
+
static getOwnBooleanMetaData(metadataKey, clazz) {
|
|
28
|
+
return !!this.getOwnMetaData(metadataKey, clazz);
|
|
29
|
+
}
|
|
30
|
+
static getArrayMetaData(metadataKey, clazz) {
|
|
31
|
+
return this.getMetaData(metadataKey, clazz) || [];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* init array metadata
|
|
35
|
+
* not inherit parent metadata
|
|
36
|
+
* return value true means use default value
|
|
37
|
+
* return value false means use map value
|
|
38
|
+
*/
|
|
39
|
+
static initArrayMetaData(metadataKey, clazz, defaultValue) {
|
|
40
|
+
if (!this.getOwnMetaData(metadataKey, clazz)) this.defineMetaData(metadataKey, defaultValue, clazz);
|
|
41
|
+
return this.getOwnMetaData(metadataKey, clazz);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* init own array metadata
|
|
45
|
+
* if parent metadata exists, inherit
|
|
46
|
+
* if parent metadata not exits, use default value
|
|
47
|
+
* return value true means use default value
|
|
48
|
+
* return value false means use map value
|
|
49
|
+
*/
|
|
50
|
+
static initOwnArrayMetaData(metadataKey, clazz, defaultValue) {
|
|
51
|
+
if (!this.getOwnMetaData(metadataKey, clazz)) {
|
|
52
|
+
const selfValue = (this.getMetaData(metadataKey, clazz) || defaultValue).slice();
|
|
53
|
+
this.defineMetaData(metadataKey, selfValue, clazz);
|
|
54
|
+
}
|
|
55
|
+
return this.getOwnMetaData(metadataKey, clazz);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* init own map metadata
|
|
59
|
+
* if parent metadata exists, inherit
|
|
60
|
+
* if parent metadata not exits, use default value
|
|
61
|
+
* return value true means use default value
|
|
62
|
+
* return value false means use map value
|
|
63
|
+
*/
|
|
64
|
+
static initOwnMapMetaData(metadataKey, clazz, defaultValue) {
|
|
65
|
+
if (!this.getOwnMetaData(metadataKey, clazz)) {
|
|
66
|
+
const parentValue = this.getMetaData(metadataKey, clazz);
|
|
67
|
+
const selfValue = /* @__PURE__ */ new Map();
|
|
68
|
+
const ownDefaultValue = parentValue || defaultValue;
|
|
69
|
+
for (const [k, v] of ownDefaultValue) selfValue.set(k, v);
|
|
70
|
+
this.defineMetaData(metadataKey, selfValue, clazz);
|
|
71
|
+
}
|
|
72
|
+
return this.getOwnMetaData(metadataKey, clazz);
|
|
73
|
+
}
|
|
74
|
+
static getOrStoreMetaData(metadataKey, clazz, metadataValue) {
|
|
75
|
+
if (!Reflect.hasMetadata(metadataKey, clazz)) Reflect.defineMetadata(metadataKey, metadataValue, clazz);
|
|
76
|
+
return Reflect.getMetadata(metadataKey, clazz);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/util/PrototypeUtil.ts
|
|
82
|
+
var PrototypeUtil = class PrototypeUtil {
|
|
83
|
+
static IS_EGG_OBJECT_PROTOTYPE = Symbol.for("EggPrototype#isEggPrototype");
|
|
84
|
+
static IS_EGG_OBJECT_MULTI_INSTANCE_PROTOTYPE = Symbol.for("EggPrototype#isEggMultiInstancePrototype");
|
|
85
|
+
static FILE_PATH = Symbol.for("EggPrototype.filePath");
|
|
86
|
+
static PROTOTYPE_PROPERTY = Symbol.for("EggPrototype.Property");
|
|
87
|
+
static MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY = Symbol.for("EggPrototype.MultiInstanceStaticProperty");
|
|
88
|
+
static MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY = Symbol.for("EggPrototype.MultiInstanceCallbackProperty");
|
|
89
|
+
static INJECT_OBJECT_NAME_SET = Symbol.for("EggPrototype.injectObjectNames");
|
|
90
|
+
static INJECT_TYPE = Symbol.for("EggPrototype.injectType");
|
|
91
|
+
static INJECT_CONSTRUCTOR_NAME_SET = Symbol.for("EggPrototype.injectConstructorNames");
|
|
92
|
+
static CLAZZ_PROTO = Symbol.for("EggPrototype.clazzProto");
|
|
93
|
+
static MULTI_INSTANCE_CONSTRUCTOR_INDEX = Symbol.for("EggPrototype#multiInstanceConstructorIndex");
|
|
94
|
+
static MULTI_INSTANCE_CONSTRUCTOR_ATTRIBUTES = Symbol.for("EggPrototype#multiInstanceConstructorAttributes");
|
|
95
|
+
/**
|
|
96
|
+
* Mark class is egg object prototype
|
|
97
|
+
* @param {Function} clazz -
|
|
98
|
+
*/
|
|
99
|
+
static setIsEggPrototype(clazz) {
|
|
100
|
+
MetadataUtil.defineMetaData(PrototypeUtil.IS_EGG_OBJECT_PROTOTYPE, true, clazz);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* If class is egg object prototype, return true
|
|
104
|
+
* @param {Function} clazz -
|
|
105
|
+
*/
|
|
106
|
+
static isEggPrototype(clazz) {
|
|
107
|
+
return MetadataUtil.getOwnBooleanMetaData(PrototypeUtil.IS_EGG_OBJECT_PROTOTYPE, clazz);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Mark class is egg object multi instance prototype
|
|
111
|
+
* @param {Function} clazz -
|
|
112
|
+
*/
|
|
113
|
+
static setIsEggMultiInstancePrototype(clazz) {
|
|
114
|
+
MetadataUtil.defineMetaData(PrototypeUtil.IS_EGG_OBJECT_MULTI_INSTANCE_PROTOTYPE, true, clazz);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* If class is egg object multi instance prototype, return true
|
|
118
|
+
* @param {Function} clazz -
|
|
119
|
+
*/
|
|
120
|
+
static isEggMultiInstancePrototype(clazz) {
|
|
121
|
+
return MetadataUtil.getOwnBooleanMetaData(PrototypeUtil.IS_EGG_OBJECT_MULTI_INSTANCE_PROTOTYPE, clazz);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get the type of the egg multi-instance prototype.
|
|
125
|
+
* @param {Function} clazz -
|
|
126
|
+
*/
|
|
127
|
+
static getEggMultiInstancePrototypeType(clazz) {
|
|
128
|
+
if (!PrototypeUtil.isEggMultiInstancePrototype(clazz)) return;
|
|
129
|
+
if (MetadataUtil.getOwnMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz)) return MultiInstanceType.STATIC;
|
|
130
|
+
return MultiInstanceType.DYNAMIC;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* set class file path
|
|
134
|
+
* @param {Function} clazz -
|
|
135
|
+
* @param {string} filePath -
|
|
136
|
+
*/
|
|
137
|
+
static setFilePath(clazz, filePath) {
|
|
138
|
+
MetadataUtil.defineMetaData(PrototypeUtil.FILE_PATH, filePath, clazz);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* get class file path
|
|
142
|
+
* @param {Function} clazz -
|
|
143
|
+
*/
|
|
144
|
+
static getFilePath(clazz) {
|
|
145
|
+
return MetadataUtil.getOwnMetaData(PrototypeUtil.FILE_PATH, clazz);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* set class property
|
|
149
|
+
* @param {EggProtoImplClass} clazz -
|
|
150
|
+
* @param {EggPrototypeInfo} property -
|
|
151
|
+
*/
|
|
152
|
+
static setProperty(clazz, property) {
|
|
153
|
+
MetadataUtil.defineMetaData(PrototypeUtil.PROTOTYPE_PROPERTY, property, clazz);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* get class property
|
|
157
|
+
* @param {EggProtoImplClass} clazz -
|
|
158
|
+
* @return {EggPrototypeInfo} -
|
|
159
|
+
*/
|
|
160
|
+
static getProperty(clazz) {
|
|
161
|
+
return MetadataUtil.getOwnMetaData(PrototypeUtil.PROTOTYPE_PROPERTY, clazz);
|
|
162
|
+
}
|
|
163
|
+
static async getInitType(clazz, ctx) {
|
|
164
|
+
return (PrototypeUtil.getProperty(clazz) ?? await PrototypeUtil.getMultiInstanceProperty(clazz, ctx))?.initType;
|
|
165
|
+
}
|
|
166
|
+
static async getAccessLevel(clazz, ctx) {
|
|
167
|
+
return (PrototypeUtil.getProperty(clazz) ?? await PrototypeUtil.getMultiInstanceProperty(clazz, ctx))?.accessLevel;
|
|
168
|
+
}
|
|
169
|
+
static async getObjNames(clazz, ctx) {
|
|
170
|
+
const property = PrototypeUtil.getProperty(clazz);
|
|
171
|
+
if (property) return [property.name];
|
|
172
|
+
return (await PrototypeUtil.getMultiInstanceProperty(clazz, ctx))?.objects.map((t) => t.name) || [];
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* set class property
|
|
176
|
+
* @param {EggProtoImplClass} clazz -
|
|
177
|
+
* @param {EggPrototypeInfo} property -
|
|
178
|
+
*/
|
|
179
|
+
static setMultiInstanceStaticProperty(clazz, property) {
|
|
180
|
+
MetadataUtil.defineMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, property, clazz);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* set class property
|
|
184
|
+
* @param {EggProtoImplClass} clazz -
|
|
185
|
+
* @param {EggPrototypeInfo} property -
|
|
186
|
+
*/
|
|
187
|
+
static setMultiInstanceCallbackProperty(clazz, property) {
|
|
188
|
+
MetadataUtil.defineMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, property, clazz);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get instance property of Static multi-instance prototype.
|
|
192
|
+
* @param {EggProtoImplClass} clazz -
|
|
193
|
+
*/
|
|
194
|
+
static getStaticMultiInstanceProperty(clazz) {
|
|
195
|
+
const metadata = MetadataUtil.getOwnMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
|
|
196
|
+
if (metadata) return metadata;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Get instance property of Dynamic multi-instance prototype.
|
|
200
|
+
* @param {EggProtoImplClass} clazz -
|
|
201
|
+
* @param {MultiInstancePrototypeGetObjectsContext} ctx -
|
|
202
|
+
*/
|
|
203
|
+
static async getDynamicMultiInstanceProperty(clazz, ctx) {
|
|
204
|
+
const callBackMetadata = MetadataUtil.getOwnMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, clazz);
|
|
205
|
+
if (callBackMetadata) {
|
|
206
|
+
const objects = await callBackMetadata.getObjects(ctx);
|
|
207
|
+
return {
|
|
208
|
+
...callBackMetadata,
|
|
209
|
+
objects
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* get class property
|
|
215
|
+
* @param {EggProtoImplClass} clazz -
|
|
216
|
+
* @param {MultiInstancePrototypeGetObjectsContext} ctx -
|
|
217
|
+
*/
|
|
218
|
+
static async getMultiInstanceProperty(clazz, ctx) {
|
|
219
|
+
const metadata = MetadataUtil.getOwnMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
|
|
220
|
+
if (metadata) return metadata;
|
|
221
|
+
const callBackMetadata = MetadataUtil.getOwnMetaData(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, clazz);
|
|
222
|
+
if (callBackMetadata) {
|
|
223
|
+
const objects = await callBackMetadata.getObjects(ctx);
|
|
224
|
+
const defaultQualifier = [{
|
|
225
|
+
attribute: InitTypeQualifierAttribute,
|
|
226
|
+
value: callBackMetadata.initType
|
|
227
|
+
}, {
|
|
228
|
+
attribute: LoadUnitNameQualifierAttribute,
|
|
229
|
+
value: ctx.moduleName
|
|
230
|
+
}];
|
|
231
|
+
for (const object of objects) defaultQualifier.forEach((qualifier) => {
|
|
232
|
+
if (!object.qualifiers.find((t) => t.attribute === qualifier.attribute)) object.qualifiers.push(qualifier);
|
|
233
|
+
});
|
|
234
|
+
return {
|
|
235
|
+
...callBackMetadata,
|
|
236
|
+
objects
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
static setMultiInstanceConstructorAttributes(clazz, attributes) {
|
|
241
|
+
MetadataUtil.defineMetaData(PrototypeUtil.MULTI_INSTANCE_CONSTRUCTOR_ATTRIBUTES, attributes, clazz);
|
|
242
|
+
}
|
|
243
|
+
static getMultiInstanceConstructorAttributes(clazz) {
|
|
244
|
+
return MetadataUtil.getMetaData(PrototypeUtil.MULTI_INSTANCE_CONSTRUCTOR_ATTRIBUTES, clazz) || [];
|
|
245
|
+
}
|
|
246
|
+
static setMultiInstanceConstructorIndex(clazz, index) {
|
|
247
|
+
MetadataUtil.defineMetaData(PrototypeUtil.MULTI_INSTANCE_CONSTRUCTOR_INDEX, index, clazz);
|
|
248
|
+
}
|
|
249
|
+
static getMultiInstanceConstructorIndex(clazz) {
|
|
250
|
+
return MetadataUtil.getMetaData(PrototypeUtil.MULTI_INSTANCE_CONSTRUCTOR_INDEX, clazz);
|
|
251
|
+
}
|
|
252
|
+
static setInjectType(clazz, type) {
|
|
253
|
+
const injectType = MetadataUtil.getMetaData(PrototypeUtil.INJECT_TYPE, clazz);
|
|
254
|
+
if (!injectType) MetadataUtil.defineMetaData(PrototypeUtil.INJECT_TYPE, type, clazz);
|
|
255
|
+
else if (injectType !== type) throw new Error(`class ${clazz.name} already use inject type ${injectType} can not use ${type}`);
|
|
256
|
+
}
|
|
257
|
+
static addInjectObject(clazz, injectObject) {
|
|
258
|
+
const objs = MetadataUtil.initOwnArrayMetaData(PrototypeUtil.INJECT_OBJECT_NAME_SET, clazz, []);
|
|
259
|
+
objs.push(injectObject);
|
|
260
|
+
MetadataUtil.defineMetaData(PrototypeUtil.INJECT_OBJECT_NAME_SET, objs, clazz);
|
|
261
|
+
}
|
|
262
|
+
static addInjectConstructor(clazz, injectConstructorInfo) {
|
|
263
|
+
const objs = MetadataUtil.initArrayMetaData(PrototypeUtil.INJECT_CONSTRUCTOR_NAME_SET, clazz, []);
|
|
264
|
+
objs.push(injectConstructorInfo);
|
|
265
|
+
MetadataUtil.defineMetaData(PrototypeUtil.INJECT_CONSTRUCTOR_NAME_SET, objs, clazz);
|
|
266
|
+
}
|
|
267
|
+
static getInjectType(clazz) {
|
|
268
|
+
return MetadataUtil.getMetaData(PrototypeUtil.INJECT_TYPE, clazz);
|
|
269
|
+
}
|
|
270
|
+
static getInjectObjects(clazz) {
|
|
271
|
+
const injectType = MetadataUtil.getMetaData(PrototypeUtil.INJECT_TYPE, clazz);
|
|
272
|
+
if (!injectType) return [];
|
|
273
|
+
if (injectType === InjectType.CONSTRUCTOR) return MetadataUtil.getArrayMetaData(PrototypeUtil.INJECT_CONSTRUCTOR_NAME_SET, clazz).sort((a, b) => {
|
|
274
|
+
return a.refIndex - b.refIndex;
|
|
275
|
+
});
|
|
276
|
+
return MetadataUtil.getArrayMetaData(PrototypeUtil.INJECT_OBJECT_NAME_SET, clazz);
|
|
277
|
+
}
|
|
278
|
+
static getClazzProto(clazz) {
|
|
279
|
+
return MetadataUtil.getMetaData(PrototypeUtil.CLAZZ_PROTO, clazz);
|
|
280
|
+
}
|
|
281
|
+
static setClazzProto(clazz, proto) {
|
|
282
|
+
MetadataUtil.defineMetaData(PrototypeUtil.CLAZZ_PROTO, proto, clazz);
|
|
283
|
+
}
|
|
284
|
+
static getDesignType(clazz, propKey) {
|
|
285
|
+
return MetadataUtil.getMetaData("design:type", clazz, propKey);
|
|
286
|
+
}
|
|
287
|
+
static getDesignParamtypes(clazz, propKey) {
|
|
288
|
+
return MetadataUtil.getMetaData("design:paramtypes", clazz, propKey);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/util/QualifierUtil.ts
|
|
294
|
+
var QualifierUtil = class QualifierUtil {
|
|
295
|
+
static addProtoQualifier(clazz, attribute, value) {
|
|
296
|
+
MetadataUtil.initOwnMapMetaData(QUALIFIER_META_DATA, clazz, /* @__PURE__ */ new Map()).set(attribute, value);
|
|
297
|
+
}
|
|
298
|
+
static getProtoQualifiers(clazz) {
|
|
299
|
+
const qualifiers = MetadataUtil.getMetaData(QUALIFIER_META_DATA, clazz);
|
|
300
|
+
if (!qualifiers) return [];
|
|
301
|
+
const res = [];
|
|
302
|
+
for (const [attribute, value] of qualifiers) res.push({
|
|
303
|
+
attribute,
|
|
304
|
+
value
|
|
305
|
+
});
|
|
306
|
+
return res;
|
|
307
|
+
}
|
|
308
|
+
static addInjectQualifier(clazz, property, parameterIndex, attribute, value) {
|
|
309
|
+
if (typeof parameterIndex === "number") {
|
|
310
|
+
const argName = ObjectUtils.getConstructorArgNameList(clazz)[parameterIndex];
|
|
311
|
+
QualifierUtil.addProperQualifier(clazz, argName, attribute, value);
|
|
312
|
+
} else QualifierUtil.addProperQualifier(clazz.constructor, property, attribute, value);
|
|
313
|
+
}
|
|
314
|
+
static addProperQualifier(clazz, property, attribute, value) {
|
|
315
|
+
const properQualifiers = MetadataUtil.initOwnMapMetaData(PROPERTY_QUALIFIER_META_DATA, clazz, /* @__PURE__ */ new Map());
|
|
316
|
+
MapUtil.getOrStore(properQualifiers, property, /* @__PURE__ */ new Map()).set(attribute, value);
|
|
317
|
+
}
|
|
318
|
+
static getProperQualifiers(clazz, property) {
|
|
319
|
+
const qualifiers = MetadataUtil.getMetaData(PROPERTY_QUALIFIER_META_DATA, clazz)?.get(property);
|
|
320
|
+
if (!qualifiers) return [];
|
|
321
|
+
const res = [];
|
|
322
|
+
for (const [attribute, value] of qualifiers) res.push({
|
|
323
|
+
attribute,
|
|
324
|
+
value
|
|
325
|
+
});
|
|
326
|
+
return res;
|
|
327
|
+
}
|
|
328
|
+
static getQualifierValue(clazz, attribute) {
|
|
329
|
+
return MetadataUtil.getMetaData(QUALIFIER_META_DATA, clazz)?.get(attribute);
|
|
330
|
+
}
|
|
331
|
+
static getProperQualifier(clazz, property, attribute) {
|
|
332
|
+
return (MetadataUtil.getMetaData(PROPERTY_QUALIFIER_META_DATA, clazz)?.get(property))?.get(attribute);
|
|
333
|
+
}
|
|
334
|
+
static matchQualifiers(clazzQualifiers, requestQualifiers) {
|
|
335
|
+
for (const request of requestQualifiers) if (!clazzQualifiers.find((t) => t.attribute === request.attribute && t.value === request.value)) return false;
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
static equalQualifiers(clazzQualifiers, requestQualifiers) {
|
|
339
|
+
if (clazzQualifiers.length !== requestQualifiers.length) return false;
|
|
340
|
+
return QualifierUtil.matchQualifiers(clazzQualifiers, requestQualifiers);
|
|
341
|
+
}
|
|
342
|
+
static mergeQualifiers(...qualifiers) {
|
|
343
|
+
const result = [];
|
|
344
|
+
const temp = {};
|
|
345
|
+
for (const qualifierList of qualifiers) for (const { attribute, value } of qualifierList) temp[attribute] = value;
|
|
346
|
+
for (const key of Reflect.ownKeys(temp)) result.push({
|
|
347
|
+
attribute: key,
|
|
348
|
+
value: temp[key]
|
|
349
|
+
});
|
|
350
|
+
return result;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/decorator/ConfigSource.ts
|
|
356
|
+
function ConfigSourceQualifier(moduleName) {
|
|
357
|
+
return function(target, propertyKey, parameterIndex) {
|
|
358
|
+
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, ConfigSourceQualifierAttribute, moduleName);
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/decorator/Prototype.ts
|
|
364
|
+
const DEFAULT_PARAMS$1 = {
|
|
365
|
+
initType: ObjectInitType.SINGLETON,
|
|
366
|
+
accessLevel: AccessLevel.PRIVATE,
|
|
367
|
+
protoImplType: DEFAULT_PROTO_IMPL_TYPE
|
|
368
|
+
};
|
|
369
|
+
function Prototype(param) {
|
|
370
|
+
return function(clazz) {
|
|
371
|
+
PrototypeUtil.setIsEggPrototype(clazz);
|
|
372
|
+
const property = {
|
|
373
|
+
...DEFAULT_PARAMS$1,
|
|
374
|
+
...param,
|
|
375
|
+
className: clazz.name
|
|
376
|
+
};
|
|
377
|
+
if (!property.name) property.name = NameUtil.getClassName(clazz);
|
|
378
|
+
PrototypeUtil.setProperty(clazz, property);
|
|
379
|
+
PrototypeUtil.setFilePath(clazz, StackUtil.getCalleeFromStack(false, 4));
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/decorator/ContextProto.ts
|
|
385
|
+
function ContextProto(params) {
|
|
386
|
+
return Prototype({
|
|
387
|
+
initType: ObjectInitType.CONTEXT,
|
|
388
|
+
accessLevel: params?.accessLevel || AccessLevel.PRIVATE,
|
|
389
|
+
...params
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region src/decorator/EggQualifier.ts
|
|
395
|
+
function EggQualifier(eggType) {
|
|
396
|
+
return function(target, propertyKey, parameterIndex) {
|
|
397
|
+
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, EggQualifierAttribute, eggType);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
//#endregion
|
|
402
|
+
//#region src/decorator/InitTypeQualifier.ts
|
|
403
|
+
function InitTypeQualifier(initType) {
|
|
404
|
+
return function(target, propertyKey, parameterIndex) {
|
|
405
|
+
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, InitTypeQualifierAttribute, initType);
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
//#endregion
|
|
410
|
+
//#region src/decorator/Inject.ts
|
|
411
|
+
const debug = debuglog("tegg/core/core-decorator/decorator/Inject");
|
|
412
|
+
function guessInjectInfo(clazz, name, proto) {
|
|
413
|
+
let objName;
|
|
414
|
+
let initType;
|
|
415
|
+
if (typeof proto === "function" && proto !== Object) {
|
|
416
|
+
const info = PrototypeUtil.getProperty(proto);
|
|
417
|
+
objName = info?.name;
|
|
418
|
+
if (info?.initType) {
|
|
419
|
+
if (!QualifierUtil.getProperQualifier(clazz, name, InitTypeQualifierAttribute)) initType = info.initType;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return {
|
|
423
|
+
objName,
|
|
424
|
+
initType
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function Inject(param) {
|
|
428
|
+
const injectParam = typeof param === "string" ? { name: param } : param;
|
|
429
|
+
function propertyInject(target, propertyKey) {
|
|
430
|
+
let objName;
|
|
431
|
+
let initType;
|
|
432
|
+
if (!injectParam) {
|
|
433
|
+
const proto = PrototypeUtil.getDesignType(target, propertyKey);
|
|
434
|
+
const result = guessInjectInfo(target.constructor, propertyKey, proto);
|
|
435
|
+
objName = result.objName;
|
|
436
|
+
initType = result.initType;
|
|
437
|
+
} else objName = injectParam?.name;
|
|
438
|
+
const injectObject = {
|
|
439
|
+
refName: propertyKey,
|
|
440
|
+
objName: objName || propertyKey
|
|
441
|
+
};
|
|
442
|
+
if (injectParam?.optional) injectObject.optional = true;
|
|
443
|
+
PrototypeUtil.setInjectType(target.constructor, InjectType.PROPERTY);
|
|
444
|
+
PrototypeUtil.addInjectObject(target.constructor, injectObject);
|
|
445
|
+
debug("propertyInject, clazz: %s, propertyKey: %s, injectObject: %o", target.constructor.name, propertyKey, injectObject);
|
|
446
|
+
if (initType) QualifierUtil.addProperQualifier(target.constructor, propertyKey, InitTypeQualifierAttribute, initType);
|
|
447
|
+
}
|
|
448
|
+
function constructorInject(target, parameterIndex) {
|
|
449
|
+
const argName = ObjectUtils.getConstructorArgNameList(target)[parameterIndex];
|
|
450
|
+
let objName;
|
|
451
|
+
let initType;
|
|
452
|
+
if (!injectParam) {
|
|
453
|
+
const protos = PrototypeUtil.getDesignParamtypes(target);
|
|
454
|
+
({objName, initType} = guessInjectInfo(target, argName, protos?.[parameterIndex]));
|
|
455
|
+
} else objName = injectParam?.name;
|
|
456
|
+
const injectObject = {
|
|
457
|
+
refIndex: parameterIndex,
|
|
458
|
+
refName: argName,
|
|
459
|
+
objName: objName || argName
|
|
460
|
+
};
|
|
461
|
+
if (injectParam?.optional) injectObject.optional = true;
|
|
462
|
+
PrototypeUtil.setInjectType(target, InjectType.CONSTRUCTOR);
|
|
463
|
+
PrototypeUtil.addInjectConstructor(target, injectObject);
|
|
464
|
+
if (initType) QualifierUtil.addProperQualifier(target, argName, InitTypeQualifierAttribute, initType);
|
|
465
|
+
}
|
|
466
|
+
return function(target, propertyKey, parameterIndex) {
|
|
467
|
+
if (typeof parameterIndex === "undefined") propertyInject(target, propertyKey);
|
|
468
|
+
else constructorInject(target, parameterIndex);
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
function InjectOptional(param) {
|
|
472
|
+
return Inject({
|
|
473
|
+
...typeof param === "string" ? { name: param } : param,
|
|
474
|
+
optional: true
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
//#endregion
|
|
479
|
+
//#region src/decorator/ModuleQualifier.ts
|
|
480
|
+
function ModuleQualifier(moduleName) {
|
|
481
|
+
return function(target, propertyKey, parameterIndex) {
|
|
482
|
+
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, LoadUnitNameQualifierAttribute, moduleName);
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
//#endregion
|
|
487
|
+
//#region src/decorator/MultiInstanceInfo.ts
|
|
488
|
+
function MultiInstanceInfo(attributes) {
|
|
489
|
+
return function(target, _propertyKey, parameterIndex) {
|
|
490
|
+
PrototypeUtil.setMultiInstanceConstructorIndex(target, parameterIndex);
|
|
491
|
+
PrototypeUtil.setMultiInstanceConstructorAttributes(target, attributes);
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/decorator/MultiInstanceProto.ts
|
|
497
|
+
const DEFAULT_PARAMS = {
|
|
498
|
+
initType: ObjectInitType.SINGLETON,
|
|
499
|
+
accessLevel: AccessLevel.PRIVATE,
|
|
500
|
+
protoImplType: DEFAULT_PROTO_IMPL_TYPE
|
|
501
|
+
};
|
|
502
|
+
function MultiInstanceProto(param) {
|
|
503
|
+
return function(clazz) {
|
|
504
|
+
PrototypeUtil.setIsEggMultiInstancePrototype(clazz);
|
|
505
|
+
if (param.objects) {
|
|
506
|
+
const property = {
|
|
507
|
+
...DEFAULT_PARAMS,
|
|
508
|
+
...param,
|
|
509
|
+
className: clazz.name
|
|
510
|
+
};
|
|
511
|
+
PrototypeUtil.setMultiInstanceStaticProperty(clazz, property);
|
|
512
|
+
} else if (param.getObjects) {
|
|
513
|
+
const property = {
|
|
514
|
+
...DEFAULT_PARAMS,
|
|
515
|
+
...param,
|
|
516
|
+
className: clazz.name
|
|
517
|
+
};
|
|
518
|
+
PrototypeUtil.setMultiInstanceCallbackProperty(clazz, property);
|
|
519
|
+
}
|
|
520
|
+
PrototypeUtil.setFilePath(clazz, StackUtil.getCalleeFromStack(false, 4));
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
//#endregion
|
|
525
|
+
//#region src/decorator/SingletonProto.ts
|
|
526
|
+
function SingletonProto(params) {
|
|
527
|
+
return Prototype({
|
|
528
|
+
initType: ObjectInitType.SINGLETON,
|
|
529
|
+
accessLevel: params?.accessLevel || AccessLevel.PRIVATE,
|
|
530
|
+
...params
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
//#endregion
|
|
19
535
|
export { ConfigSourceQualifier, ContextProto, EggQualifier, InitTypeQualifier, Inject, InjectOptional, MetadataUtil, ModuleQualifier, MultiInstanceInfo, MultiInstanceProto, Prototype, PrototypeUtil, QualifierUtil, SingletonProto };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/core-decorator",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.8",
|
|
4
4
|
"description": "tegg core decorator",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"egg",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@eggjs/tegg-common-util": "4.0.0-beta.
|
|
38
|
-
"@eggjs/tegg-types": "4.0.0-beta.
|
|
37
|
+
"@eggjs/tegg-common-util": "4.0.0-beta.8",
|
|
38
|
+
"@eggjs/tegg-types": "4.0.0-beta.8"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^22.10.5",
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { QualifierUtil } from "../util/QualifierUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
import { ConfigSourceQualifierAttribute } from "@eggjs/tegg-types";
|
|
4
|
-
|
|
5
|
-
//#region src/decorator/ConfigSource.ts
|
|
6
|
-
function ConfigSourceQualifier(moduleName) {
|
|
7
|
-
return function(target, propertyKey, parameterIndex) {
|
|
8
|
-
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, ConfigSourceQualifierAttribute, moduleName);
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
export { ConfigSourceQualifier };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import * as _eggjs_tegg_types0 from "@eggjs/tegg-types";
|
|
2
|
-
import { ContextProtoParams } from "@eggjs/tegg-types";
|
|
3
|
-
|
|
4
|
-
//#region src/decorator/ContextProto.d.ts
|
|
5
|
-
declare function ContextProto(params?: ContextProtoParams): (clazz: _eggjs_tegg_types0.EggProtoImplClass) => void;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { ContextProto };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Prototype } from "./Prototype.js";
|
|
2
|
-
import { AccessLevel, ObjectInitType } from "@eggjs/tegg-types";
|
|
3
|
-
|
|
4
|
-
//#region src/decorator/ContextProto.ts
|
|
5
|
-
function ContextProto(params) {
|
|
6
|
-
return Prototype({
|
|
7
|
-
initType: ObjectInitType.CONTEXT,
|
|
8
|
-
accessLevel: params?.accessLevel || AccessLevel.PRIVATE,
|
|
9
|
-
...params
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
//#endregion
|
|
14
|
-
export { ContextProto };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { QualifierUtil } from "../util/QualifierUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
import { EggQualifierAttribute } from "@eggjs/tegg-types";
|
|
4
|
-
|
|
5
|
-
//#region src/decorator/EggQualifier.ts
|
|
6
|
-
function EggQualifier(eggType) {
|
|
7
|
-
return function(target, propertyKey, parameterIndex) {
|
|
8
|
-
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, EggQualifierAttribute, eggType);
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
export { EggQualifier };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { ObjectInitTypeLike } from "@eggjs/tegg-types";
|
|
2
|
-
|
|
3
|
-
//#region src/decorator/InitTypeQualifier.d.ts
|
|
4
|
-
declare function InitTypeQualifier(initType: ObjectInitTypeLike): (target: any, propertyKey?: PropertyKey, parameterIndex?: number) => void;
|
|
5
|
-
//#endregion
|
|
6
|
-
export { InitTypeQualifier };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { QualifierUtil } from "../util/QualifierUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
import { InitTypeQualifierAttribute } from "@eggjs/tegg-types";
|
|
4
|
-
|
|
5
|
-
//#region src/decorator/InitTypeQualifier.ts
|
|
6
|
-
function InitTypeQualifier(initType) {
|
|
7
|
-
return function(target, propertyKey, parameterIndex) {
|
|
8
|
-
QualifierUtil.addInjectQualifier(target, propertyKey, parameterIndex, InitTypeQualifierAttribute, initType);
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
export { InitTypeQualifier };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { InjectParams } from "@eggjs/tegg-types";
|
|
2
|
-
|
|
3
|
-
//#region src/decorator/Inject.d.ts
|
|
4
|
-
declare function Inject(param?: InjectParams | string): (target: any, propertyKey?: PropertyKey, parameterIndex?: number) => void;
|
|
5
|
-
declare function InjectOptional(param?: Omit<InjectParams, 'optional'> | string): (target: any, propertyKey?: PropertyKey, parameterIndex?: number) => void;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { Inject, InjectOptional };
|