@eggjs/tegg-metadata 4.0.0-beta.4 → 4.0.0-beta.6
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/README.md +12 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +29 -0
- package/dist/factory/EggPrototypeCreatorFactory.d.ts +13 -0
- package/dist/factory/EggPrototypeCreatorFactory.js +95 -0
- package/dist/factory/EggPrototypeFactory.d.ts +13 -0
- package/dist/factory/EggPrototypeFactory.js +41 -0
- package/dist/factory/LoadUnitFactory.d.ts +16 -0
- package/dist/factory/LoadUnitFactory.js +58 -0
- package/dist/factory/index.js +5 -0
- package/dist/impl/EggPrototypeBuilder.d.ts +26 -0
- package/dist/impl/EggPrototypeBuilder.js +115 -0
- package/dist/impl/EggPrototypeImpl.d.ts +28 -0
- package/dist/impl/EggPrototypeImpl.js +52 -0
- package/dist/impl/LoadUnitMultiInstanceProtoHook.d.ts +11 -0
- package/dist/impl/LoadUnitMultiInstanceProtoHook.js +16 -0
- package/dist/impl/ModuleLoadUnit.d.ts +38 -0
- package/dist/impl/ModuleLoadUnit.js +211 -0
- package/dist/impl/index.js +6 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +28 -0
- package/dist/model/AppGraph.d.ts +42 -0
- package/dist/model/AppGraph.js +195 -0
- package/dist/model/EggPrototype.d.ts +7 -0
- package/dist/model/EggPrototype.js +7 -0
- package/dist/model/LoadUnit.d.ts +7 -0
- package/dist/model/LoadUnit.js +7 -0
- package/dist/model/ModuleDescriptor.d.ts +22 -0
- package/dist/model/ModuleDescriptor.js +33 -0
- package/dist/model/ProtoDescriptor/AbstractProtoDescriptor.d.ts +36 -0
- package/dist/model/ProtoDescriptor/AbstractProtoDescriptor.js +35 -0
- package/dist/model/ProtoDescriptor/ClassProtoDescriptor.d.ts +16 -0
- package/dist/model/ProtoDescriptor/ClassProtoDescriptor.js +27 -0
- package/dist/model/ProtoDescriptor/index.js +4 -0
- package/dist/model/ProtoDescriptorHelper.d.ts +30 -0
- package/dist/model/ProtoDescriptorHelper.js +113 -0
- package/dist/model/graph/GlobalGraph.d.ts +67 -0
- package/dist/model/graph/GlobalGraph.js +176 -0
- package/dist/model/graph/GlobalModuleNode.d.ts +29 -0
- package/dist/model/graph/GlobalModuleNode.js +42 -0
- package/dist/model/graph/GlobalModuleNodeBuilder.d.ts +17 -0
- package/dist/model/graph/GlobalModuleNodeBuilder.js +56 -0
- package/dist/model/graph/ProtoNode.d.ts +25 -0
- package/dist/model/graph/ProtoNode.js +43 -0
- package/dist/model/graph/ProtoSelector.d.ts +10 -0
- package/dist/model/graph/ProtoSelector.js +3 -0
- package/dist/model/graph/index.js +7 -0
- package/dist/model/index.js +15 -0
- package/dist/util/ClassUtil.d.ts +9 -0
- package/dist/util/ClassUtil.js +18 -0
- package/dist/util/index.js +3 -0
- package/package.json +27 -26
- package/src/errors.d.ts +0 -14
- package/src/errors.js +0 -27
- package/src/factory/EggPrototypeCreatorFactory.d.ts +0 -9
- package/src/factory/EggPrototypeCreatorFactory.js +0 -103
- package/src/factory/EggPrototypeFactory.d.ts +0 -9
- package/src/factory/EggPrototypeFactory.js +0 -52
- package/src/factory/LoadUnitFactory.d.ts +0 -12
- package/src/factory/LoadUnitFactory.js +0 -59
- package/src/factory/index.d.ts +0 -3
- package/src/factory/index.js +0 -4
- package/src/impl/EggPrototypeBuilder.d.ts +0 -22
- package/src/impl/EggPrototypeBuilder.js +0 -124
- package/src/impl/EggPrototypeImpl.d.ts +0 -24
- package/src/impl/EggPrototypeImpl.js +0 -53
- package/src/impl/LoadUnitMultiInstanceProtoHook.d.ts +0 -7
- package/src/impl/LoadUnitMultiInstanceProtoHook.js +0 -18
- package/src/impl/ModuleLoadUnit.d.ts +0 -35
- package/src/impl/ModuleLoadUnit.js +0 -268
- package/src/impl/index.d.ts +0 -4
- package/src/impl/index.js +0 -5
- package/src/index.d.ts +0 -6
- package/src/index.js +0 -7
- package/src/model/AppGraph.d.ts +0 -39
- package/src/model/AppGraph.js +0 -264
- package/src/model/EggPrototype.d.ts +0 -3
- package/src/model/EggPrototype.js +0 -3
- package/src/model/LoadUnit.d.ts +0 -3
- package/src/model/LoadUnit.js +0 -3
- package/src/model/ModuleDescriptor.d.ts +0 -18
- package/src/model/ModuleDescriptor.js +0 -38
- package/src/model/ProtoDescriptor/AbstractProtoDescriptor.d.ts +0 -32
- package/src/model/ProtoDescriptor/AbstractProtoDescriptor.js +0 -30
- package/src/model/ProtoDescriptor/ClassProtoDescriptor.d.ts +0 -12
- package/src/model/ProtoDescriptor/ClassProtoDescriptor.js +0 -30
- package/src/model/ProtoDescriptor/index.d.ts +0 -2
- package/src/model/ProtoDescriptor/index.js +0 -3
- package/src/model/ProtoDescriptorHelper.d.ts +0 -26
- package/src/model/ProtoDescriptorHelper.js +0 -129
- package/src/model/graph/GlobalGraph.d.ts +0 -63
- package/src/model/graph/GlobalGraph.js +0 -233
- package/src/model/graph/GlobalModuleNode.d.ts +0 -25
- package/src/model/graph/GlobalModuleNode.js +0 -38
- package/src/model/graph/GlobalModuleNodeBuilder.d.ts +0 -13
- package/src/model/graph/GlobalModuleNodeBuilder.js +0 -53
- package/src/model/graph/ProtoNode.d.ts +0 -19
- package/src/model/graph/ProtoNode.js +0 -37
- package/src/model/graph/ProtoSelector.d.ts +0 -6
- package/src/model/graph/ProtoSelector.js +0 -2
- package/src/model/graph/index.d.ts +0 -5
- package/src/model/graph/index.js +0 -6
- package/src/model/index.d.ts +0 -7
- package/src/model/index.js +0 -8
- package/src/util/ClassUtil.d.ts +0 -5
- package/src/util/ClassUtil.js +0 -17
- package/src/util/index.d.ts +0 -1
- package/src/util/index.js +0 -2
package/README.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# `@eggjs/tegg-metadata`
|
|
2
2
|
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
5
|
+
[![npm download][download-image]][download-url]
|
|
6
|
+
[](https://nodejs.org/en/download/)
|
|
7
|
+
|
|
8
|
+
[npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-metadata.svg?style=flat-square
|
|
9
|
+
[npm-url]: https://npmjs.org/package/@eggjs/tegg-metadata
|
|
10
|
+
[snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-metadata/badge.svg?style=flat-square
|
|
11
|
+
[snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-metadata
|
|
12
|
+
[download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-metadata.svg?style=flat-square
|
|
13
|
+
[download-url]: https://npmjs.org/package/@eggjs/tegg-metadata
|
|
14
|
+
|
|
3
15
|
# Usage
|
|
4
16
|
|
|
5
17
|
This is an internal tegg library, you probably shouldn't use it directly.
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { EggPrototypeName, QualifierInfo } from "@eggjs/tegg-types";
|
|
2
|
+
import { FrameworkBaseError } from "egg-errors";
|
|
3
|
+
|
|
4
|
+
//#region src/errors.d.ts
|
|
5
|
+
declare class TeggError extends FrameworkBaseError {
|
|
6
|
+
get module(): string;
|
|
7
|
+
}
|
|
8
|
+
declare class EggPrototypeNotFound extends TeggError {
|
|
9
|
+
constructor(protoName: EggPrototypeName, loadUnitId: string | undefined);
|
|
10
|
+
}
|
|
11
|
+
declare class MultiPrototypeFound extends TeggError {
|
|
12
|
+
constructor(name: EggPrototypeName, qualifier: QualifierInfo[], result?: string);
|
|
13
|
+
}
|
|
14
|
+
declare class IncompatibleProtoInject extends TeggError {
|
|
15
|
+
constructor(msg: string);
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { EggPrototypeNotFound, IncompatibleProtoInject, MultiPrototypeFound, TeggError };
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ErrorCodes } from "@eggjs/tegg-types";
|
|
2
|
+
import { FrameworkBaseError } from "egg-errors";
|
|
3
|
+
|
|
4
|
+
//#region src/errors.ts
|
|
5
|
+
var TeggError = class extends FrameworkBaseError {
|
|
6
|
+
get module() {
|
|
7
|
+
return "TEGG";
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var EggPrototypeNotFound = class extends TeggError {
|
|
11
|
+
constructor(protoName, loadUnitId) {
|
|
12
|
+
const msg = loadUnitId ? `Object ${String(protoName)} not found in ${loadUnitId}` : `Object ${String(protoName)} not found`;
|
|
13
|
+
super(msg, ErrorCodes.EGG_PROTO_NOT_FOUND);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var MultiPrototypeFound = class extends TeggError {
|
|
17
|
+
constructor(name, qualifier, result) {
|
|
18
|
+
const msg = `multi proto found for name:${String(name)} and qualifiers ${JSON.stringify(qualifier)}${result ? `, result is ${result}` : ""}`;
|
|
19
|
+
super(msg, ErrorCodes.MULTI_PROTO_FOUND);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var IncompatibleProtoInject = class extends TeggError {
|
|
23
|
+
constructor(msg) {
|
|
24
|
+
super(msg, ErrorCodes.INCOMPATIBLE_PROTO_INJECT);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { EggPrototypeNotFound, IncompatibleProtoInject, MultiPrototypeFound, TeggError };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ClassProtoDescriptor } from "../model/ProtoDescriptor/ClassProtoDescriptor.js";
|
|
2
|
+
import { EggProtoImplClass, EggPrototype, EggPrototypeCreator, LoadUnit } from "@eggjs/tegg-types";
|
|
3
|
+
|
|
4
|
+
//#region src/factory/EggPrototypeCreatorFactory.d.ts
|
|
5
|
+
declare class EggPrototypeCreatorFactory {
|
|
6
|
+
private static creatorMap;
|
|
7
|
+
static registerPrototypeCreator(type: string, creator: EggPrototypeCreator): void;
|
|
8
|
+
static getPrototypeCreator(type: string): EggPrototypeCreator | undefined;
|
|
9
|
+
static createProto(clazz: EggProtoImplClass, loadUnit: LoadUnit): Promise<EggPrototype[]>;
|
|
10
|
+
static createProtoByDescriptor(protoDescriptor: ClassProtoDescriptor, loadUnit: LoadUnit): Promise<EggPrototype>;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { EggPrototypeCreatorFactory };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { EggPrototypeLifecycleUtil } from "../model/EggPrototype.js";
|
|
2
|
+
import "../model/index.js";
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import { debuglog } from "node:util";
|
|
5
|
+
import { InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute, PrototypeUtil } from "@eggjs/core-decorator";
|
|
6
|
+
|
|
7
|
+
//#region src/factory/EggPrototypeCreatorFactory.ts
|
|
8
|
+
const debug = debuglog("tegg/core/metadata/factory/EggPrototypeCreatorFactory");
|
|
9
|
+
var EggPrototypeCreatorFactory = class {
|
|
10
|
+
static creatorMap = /* @__PURE__ */ new Map();
|
|
11
|
+
static registerPrototypeCreator(type, creator) {
|
|
12
|
+
this.creatorMap.set(type, creator);
|
|
13
|
+
}
|
|
14
|
+
static getPrototypeCreator(type) {
|
|
15
|
+
return this.creatorMap.get(type);
|
|
16
|
+
}
|
|
17
|
+
static async createProto(clazz, loadUnit) {
|
|
18
|
+
let properties = [];
|
|
19
|
+
const initTypeQualifierAttributeValue = await PrototypeUtil.getInitType(clazz, {
|
|
20
|
+
unitPath: loadUnit.unitPath,
|
|
21
|
+
moduleName: loadUnit.name
|
|
22
|
+
});
|
|
23
|
+
const defaultQualifier = [{
|
|
24
|
+
attribute: InitTypeQualifierAttribute,
|
|
25
|
+
value: initTypeQualifierAttributeValue
|
|
26
|
+
}, {
|
|
27
|
+
attribute: LoadUnitNameQualifierAttribute,
|
|
28
|
+
value: loadUnit.name
|
|
29
|
+
}];
|
|
30
|
+
if (PrototypeUtil.isEggMultiInstancePrototype(clazz)) {
|
|
31
|
+
const multiInstanceProtoInfo = await PrototypeUtil.getMultiInstanceProperty(clazz, {
|
|
32
|
+
unitPath: loadUnit.unitPath,
|
|
33
|
+
moduleName: loadUnit.name
|
|
34
|
+
});
|
|
35
|
+
assert(multiInstanceProtoInfo, `multiInstanceProtoInfo is undefined, clazz: ${clazz.name}, unitPath: ${loadUnit.unitPath}, moduleName: ${loadUnit.name}`);
|
|
36
|
+
for (const obj of multiInstanceProtoInfo.objects) {
|
|
37
|
+
defaultQualifier.forEach((qualifier) => {
|
|
38
|
+
if (!obj.qualifiers.find((t) => t.attribute === qualifier.attribute)) obj.qualifiers.push(qualifier);
|
|
39
|
+
});
|
|
40
|
+
properties.push({
|
|
41
|
+
name: obj.name,
|
|
42
|
+
protoImplType: multiInstanceProtoInfo.protoImplType,
|
|
43
|
+
initType: multiInstanceProtoInfo.initType,
|
|
44
|
+
accessLevel: multiInstanceProtoInfo.accessLevel,
|
|
45
|
+
qualifiers: obj.qualifiers,
|
|
46
|
+
properQualifiers: obj.properQualifiers,
|
|
47
|
+
className: multiInstanceProtoInfo.className
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
const property = PrototypeUtil.getProperty(clazz);
|
|
52
|
+
if (!property.qualifiers) property.qualifiers = [];
|
|
53
|
+
defaultQualifier.forEach((qualifier) => {
|
|
54
|
+
if (!property.qualifiers.find((t) => t.attribute === qualifier.attribute)) property.qualifiers.push(qualifier);
|
|
55
|
+
});
|
|
56
|
+
properties = [property];
|
|
57
|
+
}
|
|
58
|
+
const protos = [];
|
|
59
|
+
for (const property of properties) {
|
|
60
|
+
const creator = this.getPrototypeCreator(property.protoImplType);
|
|
61
|
+
if (!creator) throw new Error(`not found proto creator for type: ${property.protoImplType}`);
|
|
62
|
+
const ctx = {
|
|
63
|
+
clazz,
|
|
64
|
+
loadUnit,
|
|
65
|
+
prototypeInfo: property
|
|
66
|
+
};
|
|
67
|
+
const proto = creator(ctx);
|
|
68
|
+
await EggPrototypeLifecycleUtil.objectPreCreate(ctx, proto);
|
|
69
|
+
if (proto.init) await proto.init(ctx);
|
|
70
|
+
await EggPrototypeLifecycleUtil.objectPostCreate(ctx, proto);
|
|
71
|
+
PrototypeUtil.setClazzProto(clazz, proto);
|
|
72
|
+
protos.push(proto);
|
|
73
|
+
}
|
|
74
|
+
if (debug.enabled && loadUnit.name === "egg-app") debug("createProto, get protos:%o, from clazz:%o, from loadUnit:%o:%o:%o", protos.map((t) => t.name), clazz.name, loadUnit.type, loadUnit.name, loadUnit.unitPath);
|
|
75
|
+
return protos;
|
|
76
|
+
}
|
|
77
|
+
static async createProtoByDescriptor(protoDescriptor, loadUnit) {
|
|
78
|
+
const creator = this.getPrototypeCreator(protoDescriptor.protoImplType);
|
|
79
|
+
if (!creator) throw new Error(`not found proto creator for type: ${protoDescriptor.protoImplType}`);
|
|
80
|
+
const ctx = {
|
|
81
|
+
clazz: protoDescriptor.clazz,
|
|
82
|
+
loadUnit,
|
|
83
|
+
prototypeInfo: protoDescriptor
|
|
84
|
+
};
|
|
85
|
+
const proto = creator(ctx);
|
|
86
|
+
await EggPrototypeLifecycleUtil.objectPreCreate(ctx, proto);
|
|
87
|
+
if (proto.init) await proto.init(ctx);
|
|
88
|
+
await EggPrototypeLifecycleUtil.objectPostCreate(ctx, proto);
|
|
89
|
+
PrototypeUtil.setClazzProto(protoDescriptor.clazz, proto);
|
|
90
|
+
return proto;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { EggPrototypeCreatorFactory };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EggPrototype, LoadUnit, QualifierInfo } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/factory/EggPrototypeFactory.d.ts
|
|
4
|
+
declare class EggPrototypeFactory {
|
|
5
|
+
static instance: EggPrototypeFactory;
|
|
6
|
+
private publicProtoMap;
|
|
7
|
+
registerPrototype(proto: EggPrototype, loadUnit: LoadUnit): void;
|
|
8
|
+
deletePrototype(proto: EggPrototype, loadUnit: LoadUnit): void;
|
|
9
|
+
getPrototype(name: PropertyKey, loadUnit?: LoadUnit, qualifiers?: QualifierInfo[]): EggPrototype;
|
|
10
|
+
private doGetPrototype;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { EggPrototypeFactory };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { EggPrototypeNotFound, MultiPrototypeFound } from "../errors.js";
|
|
2
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
3
|
+
import { AccessLevel } from "@eggjs/tegg-types";
|
|
4
|
+
import { FrameworkErrorFormater } from "egg-errors";
|
|
5
|
+
|
|
6
|
+
//#region src/factory/EggPrototypeFactory.ts
|
|
7
|
+
var EggPrototypeFactory = class EggPrototypeFactory {
|
|
8
|
+
static instance = new EggPrototypeFactory();
|
|
9
|
+
publicProtoMap = /* @__PURE__ */ new Map();
|
|
10
|
+
registerPrototype(proto, loadUnit) {
|
|
11
|
+
if (proto.accessLevel === AccessLevel.PUBLIC) MapUtil.getOrStore(this.publicProtoMap, proto.name, []).push(proto);
|
|
12
|
+
loadUnit.registerEggPrototype(proto);
|
|
13
|
+
}
|
|
14
|
+
deletePrototype(proto, loadUnit) {
|
|
15
|
+
if (proto.accessLevel === AccessLevel.PUBLIC) {
|
|
16
|
+
const protos = this.publicProtoMap.get(proto.name);
|
|
17
|
+
if (protos) {
|
|
18
|
+
const index = protos.indexOf(proto);
|
|
19
|
+
if (index !== -1) protos.splice(index, 1);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
loadUnit.deletePrototype(proto);
|
|
23
|
+
}
|
|
24
|
+
getPrototype(name, loadUnit, qualifiers) {
|
|
25
|
+
qualifiers = qualifiers || [];
|
|
26
|
+
const protos = this.doGetPrototype(name, qualifiers, loadUnit);
|
|
27
|
+
if (!protos.length) throw FrameworkErrorFormater.formatError(new EggPrototypeNotFound(name, loadUnit?.id));
|
|
28
|
+
if (protos.length === 1) return protos[0];
|
|
29
|
+
throw FrameworkErrorFormater.formatError(new MultiPrototypeFound(name, qualifiers));
|
|
30
|
+
}
|
|
31
|
+
doGetPrototype(name, qualifiers, loadUnit) {
|
|
32
|
+
if (loadUnit) {
|
|
33
|
+
const protos = loadUnit.getEggPrototype(name, qualifiers);
|
|
34
|
+
if (protos.length) return protos;
|
|
35
|
+
}
|
|
36
|
+
return this.publicProtoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
export { EggPrototypeFactory };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EggLoadUnitTypeLike, Id, LoadUnit, LoadUnitCreator, LoadUnitLifecycleContext, Loader } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/factory/LoadUnitFactory.d.ts
|
|
4
|
+
declare class LoadUnitFactory {
|
|
5
|
+
private static loadUnitCreatorMap;
|
|
6
|
+
private static loadUnitMap;
|
|
7
|
+
private static loadUnitIdMap;
|
|
8
|
+
protected static getLoanUnit(ctx: LoadUnitLifecycleContext, type: EggLoadUnitTypeLike): Promise<LoadUnit>;
|
|
9
|
+
static createLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise<LoadUnit>;
|
|
10
|
+
static createPreloadLoadUnit(unitPath: string, type: EggLoadUnitTypeLike, loader: Loader): Promise<LoadUnit>;
|
|
11
|
+
static destroyLoadUnit(loadUnit: LoadUnit): Promise<void>;
|
|
12
|
+
static getLoadUnitById(id: Id): LoadUnit | undefined;
|
|
13
|
+
static registerLoadUnitCreator(type: EggLoadUnitTypeLike, creator: LoadUnitCreator): void;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { LoadUnitFactory };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { LoadUnitLifecycleUtil } from "../model/LoadUnit.js";
|
|
2
|
+
import "../model/index.js";
|
|
3
|
+
|
|
4
|
+
//#region src/factory/LoadUnitFactory.ts
|
|
5
|
+
var LoadUnitFactory = class LoadUnitFactory {
|
|
6
|
+
static loadUnitCreatorMap = /* @__PURE__ */ new Map();
|
|
7
|
+
static loadUnitMap = /* @__PURE__ */ new Map();
|
|
8
|
+
static loadUnitIdMap = /* @__PURE__ */ new Map();
|
|
9
|
+
static async getLoanUnit(ctx, type) {
|
|
10
|
+
const creator = LoadUnitFactory.loadUnitCreatorMap.get(type);
|
|
11
|
+
if (!creator) throw new Error(`not find creator for load unit type ${type}`);
|
|
12
|
+
return await creator(ctx);
|
|
13
|
+
}
|
|
14
|
+
static async createLoadUnit(unitPath, type, loader) {
|
|
15
|
+
if (LoadUnitFactory.loadUnitMap.has(unitPath)) return LoadUnitFactory.loadUnitMap.get(unitPath).loadUnit;
|
|
16
|
+
const ctx = {
|
|
17
|
+
unitPath,
|
|
18
|
+
loader
|
|
19
|
+
};
|
|
20
|
+
const loadUnit = await LoadUnitFactory.getLoanUnit(ctx, type);
|
|
21
|
+
await LoadUnitLifecycleUtil.objectPreCreate(ctx, loadUnit);
|
|
22
|
+
if (loadUnit.init) await loadUnit.init(ctx);
|
|
23
|
+
await LoadUnitLifecycleUtil.objectPostCreate(ctx, loadUnit);
|
|
24
|
+
LoadUnitFactory.loadUnitMap.set(unitPath, {
|
|
25
|
+
loadUnit,
|
|
26
|
+
ctx
|
|
27
|
+
});
|
|
28
|
+
LoadUnitFactory.loadUnitIdMap.set(loadUnit.id, loadUnit);
|
|
29
|
+
return loadUnit;
|
|
30
|
+
}
|
|
31
|
+
static async createPreloadLoadUnit(unitPath, type, loader) {
|
|
32
|
+
const ctx = {
|
|
33
|
+
unitPath,
|
|
34
|
+
loader
|
|
35
|
+
};
|
|
36
|
+
return await LoadUnitFactory.getLoanUnit(ctx, type);
|
|
37
|
+
}
|
|
38
|
+
static async destroyLoadUnit(loadUnit) {
|
|
39
|
+
const { ctx } = LoadUnitFactory.loadUnitMap.get(loadUnit.unitPath);
|
|
40
|
+
try {
|
|
41
|
+
await LoadUnitLifecycleUtil.objectPreDestroy(ctx, loadUnit);
|
|
42
|
+
if (loadUnit.destroy) await loadUnit.destroy(ctx);
|
|
43
|
+
} finally {
|
|
44
|
+
LoadUnitFactory.loadUnitMap.delete(loadUnit.unitPath);
|
|
45
|
+
LoadUnitFactory.loadUnitIdMap.delete(loadUnit.id);
|
|
46
|
+
LoadUnitLifecycleUtil.clearObjectLifecycle(loadUnit);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
static getLoadUnitById(id) {
|
|
50
|
+
return LoadUnitFactory.loadUnitIdMap.get(id);
|
|
51
|
+
}
|
|
52
|
+
static registerLoadUnitCreator(type, creator) {
|
|
53
|
+
LoadUnitFactory.loadUnitCreatorMap.set(type, creator);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
export { LoadUnitFactory };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { EggPrototype, EggPrototypeLifecycleContext } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/EggPrototypeBuilder.d.ts
|
|
4
|
+
declare class EggPrototypeBuilder {
|
|
5
|
+
private clazz;
|
|
6
|
+
private name;
|
|
7
|
+
private initType;
|
|
8
|
+
private accessLevel;
|
|
9
|
+
private filepath;
|
|
10
|
+
private injectType;
|
|
11
|
+
private injectObjects;
|
|
12
|
+
private loadUnit;
|
|
13
|
+
private qualifiers;
|
|
14
|
+
private properQualifiers;
|
|
15
|
+
private className?;
|
|
16
|
+
private multiInstanceConstructorIndex?;
|
|
17
|
+
private multiInstanceConstructorAttributes?;
|
|
18
|
+
static create(ctx: EggPrototypeLifecycleContext): EggPrototype;
|
|
19
|
+
private tryFindDefaultPrototype;
|
|
20
|
+
private tryFindContextPrototype;
|
|
21
|
+
private tryFindSelfInitTypePrototype;
|
|
22
|
+
private findInjectObjectPrototype;
|
|
23
|
+
build(): EggPrototype;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { EggPrototypeBuilder };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { EggPrototypeNotFound, MultiPrototypeFound } from "../errors.js";
|
|
2
|
+
import { EggPrototypeFactory } from "../factory/EggPrototypeFactory.js";
|
|
3
|
+
import { EggPrototypeCreatorFactory } from "../factory/EggPrototypeCreatorFactory.js";
|
|
4
|
+
import "../factory/index.js";
|
|
5
|
+
import { EggPrototypeImpl } from "./EggPrototypeImpl.js";
|
|
6
|
+
import { DEFAULT_PROTO_IMPL_TYPE, InitTypeQualifierAttribute, ObjectInitType } from "@eggjs/tegg-types";
|
|
7
|
+
import assert from "node:assert";
|
|
8
|
+
import { InjectType, PrototypeUtil, QualifierUtil } from "@eggjs/core-decorator";
|
|
9
|
+
import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
|
|
10
|
+
|
|
11
|
+
//#region src/impl/EggPrototypeBuilder.ts
|
|
12
|
+
var EggPrototypeBuilder = class EggPrototypeBuilder {
|
|
13
|
+
clazz;
|
|
14
|
+
name;
|
|
15
|
+
initType;
|
|
16
|
+
accessLevel;
|
|
17
|
+
filepath;
|
|
18
|
+
injectType;
|
|
19
|
+
injectObjects = [];
|
|
20
|
+
loadUnit;
|
|
21
|
+
qualifiers = [];
|
|
22
|
+
properQualifiers = {};
|
|
23
|
+
className;
|
|
24
|
+
multiInstanceConstructorIndex;
|
|
25
|
+
multiInstanceConstructorAttributes;
|
|
26
|
+
static create(ctx) {
|
|
27
|
+
const { clazz, loadUnit } = ctx;
|
|
28
|
+
const filepath = PrototypeUtil.getFilePath(clazz);
|
|
29
|
+
assert(filepath, "not find filepath");
|
|
30
|
+
const builder = new EggPrototypeBuilder();
|
|
31
|
+
builder.clazz = clazz;
|
|
32
|
+
builder.name = ctx.prototypeInfo.name;
|
|
33
|
+
builder.className = ctx.prototypeInfo.className;
|
|
34
|
+
builder.initType = ctx.prototypeInfo.initType;
|
|
35
|
+
builder.accessLevel = ctx.prototypeInfo.accessLevel;
|
|
36
|
+
builder.filepath = filepath;
|
|
37
|
+
builder.injectType = PrototypeUtil.getInjectType(clazz);
|
|
38
|
+
builder.injectObjects = PrototypeUtil.getInjectObjects(clazz) || [];
|
|
39
|
+
builder.loadUnit = loadUnit;
|
|
40
|
+
builder.qualifiers = QualifierUtil.mergeQualifiers(QualifierUtil.getProtoQualifiers(clazz), ctx.prototypeInfo.qualifiers ?? []);
|
|
41
|
+
builder.properQualifiers = ctx.prototypeInfo.properQualifiers ?? {};
|
|
42
|
+
builder.multiInstanceConstructorIndex = PrototypeUtil.getMultiInstanceConstructorIndex(clazz);
|
|
43
|
+
builder.multiInstanceConstructorAttributes = PrototypeUtil.getMultiInstanceConstructorAttributes(clazz);
|
|
44
|
+
return builder.build();
|
|
45
|
+
}
|
|
46
|
+
tryFindDefaultPrototype(injectObject) {
|
|
47
|
+
const propertyQualifiers = QualifierUtil.getProperQualifiers(this.clazz, injectObject.refName);
|
|
48
|
+
const multiInstancePropertyQualifiers = this.properQualifiers[injectObject.refName] ?? [];
|
|
49
|
+
return EggPrototypeFactory.instance.getPrototype(injectObject.objName, this.loadUnit, QualifierUtil.mergeQualifiers(propertyQualifiers, multiInstancePropertyQualifiers));
|
|
50
|
+
}
|
|
51
|
+
tryFindContextPrototype(injectObject) {
|
|
52
|
+
const propertyQualifiers = QualifierUtil.getProperQualifiers(this.clazz, injectObject.refName);
|
|
53
|
+
const multiInstancePropertyQualifiers = this.properQualifiers[injectObject.refName] ?? [];
|
|
54
|
+
return EggPrototypeFactory.instance.getPrototype(injectObject.objName, this.loadUnit, QualifierUtil.mergeQualifiers(propertyQualifiers, multiInstancePropertyQualifiers, [{
|
|
55
|
+
attribute: InitTypeQualifierAttribute,
|
|
56
|
+
value: ObjectInitType.CONTEXT
|
|
57
|
+
}]));
|
|
58
|
+
}
|
|
59
|
+
tryFindSelfInitTypePrototype(injectObject) {
|
|
60
|
+
const propertyQualifiers = QualifierUtil.getProperQualifiers(this.clazz, injectObject.refName);
|
|
61
|
+
const multiInstancePropertyQualifiers = this.properQualifiers[injectObject.refName] ?? [];
|
|
62
|
+
return EggPrototypeFactory.instance.getPrototype(injectObject.objName, this.loadUnit, QualifierUtil.mergeQualifiers(propertyQualifiers, multiInstancePropertyQualifiers, [{
|
|
63
|
+
attribute: InitTypeQualifierAttribute,
|
|
64
|
+
value: this.initType
|
|
65
|
+
}]));
|
|
66
|
+
}
|
|
67
|
+
findInjectObjectPrototype(injectObject) {
|
|
68
|
+
const propertyQualifiers = QualifierUtil.getProperQualifiers(this.clazz, injectObject.refName);
|
|
69
|
+
try {
|
|
70
|
+
return this.tryFindDefaultPrototype(injectObject);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
if (!(e instanceof MultiPrototypeFound && !propertyQualifiers.find((t) => t.attribute === InitTypeQualifierAttribute))) throw e;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
return this.tryFindContextPrototype(injectObject);
|
|
76
|
+
} catch (e) {
|
|
77
|
+
if (!(e instanceof EggPrototypeNotFound)) throw e;
|
|
78
|
+
}
|
|
79
|
+
return this.tryFindSelfInitTypePrototype(injectObject);
|
|
80
|
+
}
|
|
81
|
+
build() {
|
|
82
|
+
const injectObjectProtos = [];
|
|
83
|
+
for (const injectObject of this.injectObjects) {
|
|
84
|
+
const propertyQualifiers = QualifierUtil.getProperQualifiers(this.clazz, injectObject.refName);
|
|
85
|
+
try {
|
|
86
|
+
const proto = this.findInjectObjectPrototype(injectObject);
|
|
87
|
+
let injectObjectProto;
|
|
88
|
+
if (this.injectType === InjectType.PROPERTY) injectObjectProto = {
|
|
89
|
+
refName: injectObject.refName,
|
|
90
|
+
objName: injectObject.objName,
|
|
91
|
+
qualifiers: propertyQualifiers,
|
|
92
|
+
proto
|
|
93
|
+
};
|
|
94
|
+
else injectObjectProto = {
|
|
95
|
+
refIndex: injectObject.refIndex,
|
|
96
|
+
refName: injectObject.refName,
|
|
97
|
+
objName: injectObject.objName,
|
|
98
|
+
qualifiers: propertyQualifiers,
|
|
99
|
+
proto
|
|
100
|
+
};
|
|
101
|
+
if (injectObject.optional) injectObject.optional = true;
|
|
102
|
+
injectObjectProtos.push(injectObjectProto);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (e instanceof EggPrototypeNotFound && injectObject.optional) continue;
|
|
105
|
+
throw e;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const id = IdenticalUtil.createProtoId(this.loadUnit.id, this.name);
|
|
109
|
+
return new EggPrototypeImpl(id, this.name, this.clazz, this.filepath, this.initType, this.accessLevel, injectObjectProtos, this.loadUnit.id, this.qualifiers, this.className, this.injectType, this.multiInstanceConstructorIndex, this.multiInstanceConstructorAttributes);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
EggPrototypeCreatorFactory.registerPrototypeCreator(DEFAULT_PROTO_IMPL_TYPE, EggPrototypeBuilder.create);
|
|
113
|
+
|
|
114
|
+
//#endregion
|
|
115
|
+
export { EggPrototypeBuilder };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AccessLevel, EggProtoImplClass, EggPrototype, EggPrototypeName, Id, InjectConstructorProto, InjectObjectProto, MetaDataKey, ObjectInitTypeLike, QualifierInfo, QualifierValue } from "@eggjs/tegg-types";
|
|
2
|
+
import { InjectType, QualifierAttribute } from "@eggjs/core-decorator";
|
|
3
|
+
|
|
4
|
+
//#region src/impl/EggPrototypeImpl.d.ts
|
|
5
|
+
declare class EggPrototypeImpl implements EggPrototype {
|
|
6
|
+
private readonly clazz;
|
|
7
|
+
private readonly qualifiers;
|
|
8
|
+
readonly filepath: string;
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly name: EggPrototypeName;
|
|
11
|
+
readonly initType: ObjectInitTypeLike;
|
|
12
|
+
readonly accessLevel: AccessLevel;
|
|
13
|
+
readonly injectObjects: Array<InjectObjectProto | InjectConstructorProto>;
|
|
14
|
+
readonly injectType: InjectType;
|
|
15
|
+
readonly loadUnitId: Id;
|
|
16
|
+
readonly className?: string;
|
|
17
|
+
readonly multiInstanceConstructorIndex?: number;
|
|
18
|
+
readonly multiInstanceConstructorAttributes?: QualifierAttribute[];
|
|
19
|
+
[key: symbol]: PropertyDescriptor;
|
|
20
|
+
constructor(id: string, name: EggPrototypeName, clazz: EggProtoImplClass, filepath: string, initType: ObjectInitTypeLike, accessLevel: AccessLevel, injectObjectMap: Array<InjectObjectProto | InjectConstructorProto>, loadUnitId: Id, qualifiers: QualifierInfo[], className?: string, injectType?: InjectType, multiInstanceConstructorIndex?: number, multiInstanceConstructorAttributes?: QualifierAttribute[]);
|
|
21
|
+
verifyQualifiers(qualifiers: QualifierInfo[]): boolean;
|
|
22
|
+
verifyQualifier(qualifier: QualifierInfo): boolean;
|
|
23
|
+
getQualifier(attribute: string): QualifierValue | undefined;
|
|
24
|
+
constructEggObject(...args: any): object;
|
|
25
|
+
getMetaData<T>(metadataKey: MetaDataKey): T | undefined;
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { EggPrototypeImpl };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { InjectType, MetadataUtil } from "@eggjs/core-decorator";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/EggPrototypeImpl.ts
|
|
4
|
+
var EggPrototypeImpl = class {
|
|
5
|
+
clazz;
|
|
6
|
+
qualifiers;
|
|
7
|
+
filepath;
|
|
8
|
+
id;
|
|
9
|
+
name;
|
|
10
|
+
initType;
|
|
11
|
+
accessLevel;
|
|
12
|
+
injectObjects;
|
|
13
|
+
injectType;
|
|
14
|
+
loadUnitId;
|
|
15
|
+
className;
|
|
16
|
+
multiInstanceConstructorIndex;
|
|
17
|
+
multiInstanceConstructorAttributes;
|
|
18
|
+
constructor(id, name, clazz, filepath, initType, accessLevel, injectObjectMap, loadUnitId, qualifiers, className, injectType, multiInstanceConstructorIndex, multiInstanceConstructorAttributes) {
|
|
19
|
+
this.id = id;
|
|
20
|
+
this.clazz = clazz;
|
|
21
|
+
this.name = name;
|
|
22
|
+
this.filepath = filepath;
|
|
23
|
+
this.initType = initType;
|
|
24
|
+
this.accessLevel = accessLevel;
|
|
25
|
+
this.injectObjects = injectObjectMap;
|
|
26
|
+
this.loadUnitId = loadUnitId;
|
|
27
|
+
this.qualifiers = qualifiers;
|
|
28
|
+
this.className = className;
|
|
29
|
+
this.injectType = injectType || InjectType.PROPERTY;
|
|
30
|
+
this.multiInstanceConstructorIndex = multiInstanceConstructorIndex;
|
|
31
|
+
this.multiInstanceConstructorAttributes = multiInstanceConstructorAttributes;
|
|
32
|
+
}
|
|
33
|
+
verifyQualifiers(qualifiers) {
|
|
34
|
+
for (const qualifier of qualifiers) if (!this.verifyQualifier(qualifier)) return false;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
verifyQualifier(qualifier) {
|
|
38
|
+
return this.qualifiers.find((t) => t.attribute === qualifier.attribute)?.value === qualifier.value;
|
|
39
|
+
}
|
|
40
|
+
getQualifier(attribute) {
|
|
41
|
+
return this.qualifiers.find((t) => t.attribute === attribute)?.value;
|
|
42
|
+
}
|
|
43
|
+
constructEggObject(...args) {
|
|
44
|
+
return Reflect.construct(this.clazz, args);
|
|
45
|
+
}
|
|
46
|
+
getMetaData(metadataKey) {
|
|
47
|
+
return MetadataUtil.getMetaData(metadataKey, this.clazz);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { EggPrototypeImpl };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EggProtoImplClass, LifecycleHook, LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/LoadUnitMultiInstanceProtoHook.d.ts
|
|
4
|
+
declare class LoadUnitMultiInstanceProtoHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
|
|
5
|
+
static multiInstanceClazzSet: Set<EggProtoImplClass>;
|
|
6
|
+
static setAllClassList(clazzList: readonly EggProtoImplClass[]): void;
|
|
7
|
+
static clear(): void;
|
|
8
|
+
preCreate(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { LoadUnitMultiInstanceProtoHook };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PrototypeUtil } from "@eggjs/core-decorator";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/LoadUnitMultiInstanceProtoHook.ts
|
|
4
|
+
var LoadUnitMultiInstanceProtoHook = class {
|
|
5
|
+
static multiInstanceClazzSet = /* @__PURE__ */ new Set();
|
|
6
|
+
static setAllClassList(clazzList) {
|
|
7
|
+
for (const clazz of clazzList) if (PrototypeUtil.isEggMultiInstancePrototype(clazz)) this.multiInstanceClazzSet.add(clazz);
|
|
8
|
+
}
|
|
9
|
+
static clear() {
|
|
10
|
+
this.multiInstanceClazzSet.clear();
|
|
11
|
+
}
|
|
12
|
+
async preCreate() {}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { LoadUnitMultiInstanceProtoHook };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { GlobalGraph } from "../model/graph/GlobalGraph.js";
|
|
2
|
+
import { EggLoadUnitType, EggProtoImplClass, EggPrototype, LoadUnit, LoadUnitLifecycleContext, QualifierInfo } from "@eggjs/tegg-types";
|
|
3
|
+
|
|
4
|
+
//#region src/impl/ModuleLoadUnit.d.ts
|
|
5
|
+
declare class ModuleGraph {
|
|
6
|
+
private graph;
|
|
7
|
+
clazzList: EggProtoImplClass[];
|
|
8
|
+
readonly unitPath: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
constructor(clazzList: EggProtoImplClass[], unitPath: string, name: string);
|
|
11
|
+
private findInjectNode;
|
|
12
|
+
build(): Promise<void>;
|
|
13
|
+
sort(): void;
|
|
14
|
+
}
|
|
15
|
+
declare class ModuleLoadUnit implements LoadUnit {
|
|
16
|
+
private protoMap;
|
|
17
|
+
private protos;
|
|
18
|
+
private clazzList;
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly unitPath: string;
|
|
22
|
+
readonly type = EggLoadUnitType.MODULE;
|
|
23
|
+
get globalGraph(): GlobalGraph;
|
|
24
|
+
constructor(name: string, unitPath: string);
|
|
25
|
+
private doLoadClazz;
|
|
26
|
+
private loadClazz;
|
|
27
|
+
preLoad(): Promise<void>;
|
|
28
|
+
init(): Promise<void>;
|
|
29
|
+
containPrototype(proto: EggPrototype): boolean;
|
|
30
|
+
getEggPrototype(name: string, qualifiers: QualifierInfo[]): EggPrototype[];
|
|
31
|
+
registerEggPrototype(proto: EggPrototype): void;
|
|
32
|
+
deletePrototype(proto: EggPrototype): void;
|
|
33
|
+
destroy(): Promise<void>;
|
|
34
|
+
iterateEggPrototype(): IterableIterator<EggPrototype>;
|
|
35
|
+
static createModule(ctx: LoadUnitLifecycleContext): ModuleLoadUnit;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { ModuleGraph, ModuleLoadUnit };
|