@eggjs/tegg-runtime 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/factory/EggContainerFactory.d.ts +30 -0
- package/dist/factory/EggContainerFactory.js +80 -0
- package/dist/factory/EggObjectFactory.d.ts +17 -0
- package/dist/factory/EggObjectFactory.js +52 -0
- package/dist/factory/LoadUnitInstanceFactory.d.ts +15 -0
- package/dist/factory/LoadUnitInstanceFactory.js +49 -0
- package/dist/factory/index.js +5 -0
- package/dist/impl/ContextInitiator.d.ts +12 -0
- package/dist/impl/ContextInitiator.js +37 -0
- package/dist/impl/ContextObjectGraph.d.ts +10 -0
- package/dist/impl/ContextObjectGraph.js +36 -0
- package/dist/impl/EggAlwaysNewObjectContainer.d.ts +17 -0
- package/dist/impl/EggAlwaysNewObjectContainer.js +30 -0
- package/dist/impl/EggObjectImpl.d.ts +21 -0
- package/dist/impl/EggObjectImpl.js +126 -0
- package/dist/impl/EggObjectUtil.d.ts +11 -0
- package/dist/impl/EggObjectUtil.js +147 -0
- package/dist/impl/ModuleLoadUnitInstance.d.ts +22 -0
- package/dist/impl/ModuleLoadUnitInstance.js +73 -0
- package/dist/impl/index.js +8 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +21 -0
- package/dist/model/AbstractEggContext.d.ts +22 -0
- package/dist/model/AbstractEggContext.js +83 -0
- package/dist/model/ContextHandler.d.ts +12 -0
- package/dist/model/ContextHandler.js +18 -0
- package/dist/model/EggContext.d.ts +7 -0
- package/dist/model/EggContext.js +7 -0
- package/dist/model/EggObject.d.ts +7 -0
- package/dist/model/EggObject.js +7 -0
- package/dist/model/LoadUnitInstance.d.ts +7 -0
- package/dist/model/LoadUnitInstance.js +7 -0
- package/dist/model/index.js +7 -0
- package/package.json +26 -26
- package/src/factory/EggContainerFactory.d.ts +0 -26
- package/src/factory/EggContainerFactory.js +0 -77
- package/src/factory/EggObjectFactory.d.ts +0 -14
- package/src/factory/EggObjectFactory.js +0 -46
- package/src/factory/LoadUnitInstanceFactory.d.ts +0 -12
- package/src/factory/LoadUnitInstanceFactory.js +0 -57
- package/src/factory/index.d.ts +0 -3
- package/src/factory/index.js +0 -4
- package/src/impl/ContextInitiator.d.ts +0 -8
- package/src/impl/ContextInitiator.js +0 -37
- package/src/impl/ContextObjectGraph.d.ts +0 -6
- package/src/impl/ContextObjectGraph.js +0 -38
- package/src/impl/EggAlwaysNewObjectContainer.d.ts +0 -13
- package/src/impl/EggAlwaysNewObjectContainer.js +0 -32
- package/src/impl/EggObjectImpl.d.ts +0 -17
- package/src/impl/EggObjectImpl.js +0 -178
- package/src/impl/EggObjectUtil.d.ts +0 -7
- package/src/impl/EggObjectUtil.js +0 -172
- package/src/impl/ModuleLoadUnitInstance.d.ts +0 -18
- package/src/impl/ModuleLoadUnitInstance.js +0 -90
- package/src/impl/index.d.ts +0 -6
- package/src/impl/index.js +0 -7
- package/src/index.d.ts +0 -5
- package/src/index.js +0 -5
- package/src/model/AbstractEggContext.d.ts +0 -18
- package/src/model/AbstractEggContext.js +0 -89
- package/src/model/ContextHandler.d.ts +0 -9
- package/src/model/ContextHandler.js +0 -14
- package/src/model/EggContext.d.ts +0 -3
- package/src/model/EggContext.js +0 -3
- package/src/model/EggObject.d.ts +0 -3
- package/src/model/EggObject.js +0 -3
- package/src/model/LoadUnitInstance.d.ts +0 -3
- package/src/model/LoadUnitInstance.js +0 -3
- package/src/model/index.d.ts +0 -5
- package/src/model/index.js +0 -6
package/README.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# `@eggjs/tegg-runtime`
|
|
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-runtime.svg?style=flat-square
|
|
9
|
+
[npm-url]: https://npmjs.org/package/@eggjs/tegg-runtime
|
|
10
|
+
[snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-runtime/badge.svg?style=flat-square
|
|
11
|
+
[snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-runtime
|
|
12
|
+
[download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-runtime.svg?style=flat-square
|
|
13
|
+
[download-url]: https://npmjs.org/package/@eggjs/tegg-runtime
|
|
14
|
+
|
|
3
15
|
# Usage
|
|
4
16
|
|
|
5
17
|
This is an internal tegg library, you probably shouldn't use it directly.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ContainerGetMethod, EggContainer, EggObject, EggObjectName, EggProtoImplClass, EggPrototype, LifecycleContext, ObjectInitTypeLike, QualifierInfo } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/factory/EggContainerFactory.d.ts
|
|
4
|
+
declare class EggContainerFactory {
|
|
5
|
+
private static containerGetMethodMap;
|
|
6
|
+
private static ContextInitiatorClass;
|
|
7
|
+
static registerContainerGetMethod(initType: ObjectInitTypeLike, method: ContainerGetMethod): void;
|
|
8
|
+
static getContainer(proto: EggPrototype): EggContainer<LifecycleContext>;
|
|
9
|
+
/**
|
|
10
|
+
* get or create egg object
|
|
11
|
+
* If get singleton egg object in context,
|
|
12
|
+
* will create context egg object for it.
|
|
13
|
+
*/
|
|
14
|
+
static getOrCreateEggObject(proto: EggPrototype, name?: EggObjectName): Promise<EggObject>;
|
|
15
|
+
/**
|
|
16
|
+
* get or create egg object from the Class
|
|
17
|
+
* If get singleton egg object in context,
|
|
18
|
+
* will create context egg object for it.
|
|
19
|
+
*/
|
|
20
|
+
static getOrCreateEggObjectFromClazz(clazz: EggProtoImplClass, name?: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject>;
|
|
21
|
+
/**
|
|
22
|
+
* get or create egg object from the Name
|
|
23
|
+
* If get singleton egg object in context,
|
|
24
|
+
* will create context egg object for it.
|
|
25
|
+
*/
|
|
26
|
+
static getOrCreateEggObjectFromName(name: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject>;
|
|
27
|
+
static getEggObject(proto: EggPrototype, name?: EggObjectName): EggObject;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { EggContainerFactory };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ContextHandler } from "../model/ContextHandler.js";
|
|
2
|
+
import "../model/index.js";
|
|
3
|
+
import { debuglog } from "node:util";
|
|
4
|
+
import { EggPrototypeFactory } from "@eggjs/tegg-metadata";
|
|
5
|
+
import { NameUtil } from "@eggjs/tegg-common-util";
|
|
6
|
+
import { PrototypeUtil } from "@eggjs/core-decorator";
|
|
7
|
+
|
|
8
|
+
//#region src/factory/EggContainerFactory.ts
|
|
9
|
+
const debug = debuglog("tegg/core/runtime/EggContainerFactory");
|
|
10
|
+
var EggContainerFactory = class EggContainerFactory {
|
|
11
|
+
static containerGetMethodMap = /* @__PURE__ */ new Map();
|
|
12
|
+
static ContextInitiatorClass;
|
|
13
|
+
static registerContainerGetMethod(initType, method) {
|
|
14
|
+
if (debug.enabled) debug("registerContainerGetMethod %o %o, exists: %s", initType, method.toString(), this.containerGetMethodMap.has(initType));
|
|
15
|
+
this.containerGetMethodMap.set(initType, method);
|
|
16
|
+
}
|
|
17
|
+
static getContainer(proto) {
|
|
18
|
+
const method = this.containerGetMethodMap.get(proto.initType);
|
|
19
|
+
if (!method) throw new Error(`InitType ${proto.initType} has not register ContainerGetMethod`);
|
|
20
|
+
return method(proto);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* get or create egg object
|
|
24
|
+
* If get singleton egg object in context,
|
|
25
|
+
* will create context egg object for it.
|
|
26
|
+
*/
|
|
27
|
+
static async getOrCreateEggObject(proto, name) {
|
|
28
|
+
const container = this.getContainer(proto);
|
|
29
|
+
name = name || proto.name;
|
|
30
|
+
const obj = await container.getOrCreateEggObject(name, proto);
|
|
31
|
+
const ctx = ContextHandler.getContext();
|
|
32
|
+
if (ctx) {
|
|
33
|
+
if (!EggContainerFactory.ContextInitiatorClass) {
|
|
34
|
+
const { ContextInitiator } = await import("../impl/ContextInitiator.js");
|
|
35
|
+
EggContainerFactory.ContextInitiatorClass = ContextInitiator;
|
|
36
|
+
}
|
|
37
|
+
await EggContainerFactory.ContextInitiatorClass.createContextInitiator(ctx).init(obj);
|
|
38
|
+
debug("getOrCreateEggObject with context eggObject:%o, from proto:%o, name:%s", obj.name, proto.name, name);
|
|
39
|
+
} else debug("getOrCreateEggObject without context, get eggObject:%o, from proto:%o, name:%s", obj.name, proto.name, name);
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* get or create egg object from the Class
|
|
44
|
+
* If get singleton egg object in context,
|
|
45
|
+
* will create context egg object for it.
|
|
46
|
+
*/
|
|
47
|
+
static async getOrCreateEggObjectFromClazz(clazz, name, qualifiers) {
|
|
48
|
+
let proto = PrototypeUtil.getClazzProto(clazz);
|
|
49
|
+
const isMultiInstance = PrototypeUtil.isEggMultiInstancePrototype(clazz);
|
|
50
|
+
debug("getOrCreateEggObjectFromClazz:%o, isMultiInstance:%s, proto:%o", clazz.name, isMultiInstance, !!proto);
|
|
51
|
+
if (isMultiInstance) {
|
|
52
|
+
const defaultName = NameUtil.getClassName(clazz);
|
|
53
|
+
name = name ?? defaultName;
|
|
54
|
+
proto = EggPrototypeFactory.instance.getPrototype(name, void 0, qualifiers);
|
|
55
|
+
} else if (proto) name = name ?? proto.name;
|
|
56
|
+
if (!proto) {
|
|
57
|
+
debug("getOrCreateEggObjectFromClazz:%o not found, eggObjectName:%s, qualifiers:%o, proto:%o, isMultiInstance:%s", clazz.name, name, qualifiers, proto, isMultiInstance);
|
|
58
|
+
throw new Error(`can not get proto for clazz ${clazz.name}`);
|
|
59
|
+
}
|
|
60
|
+
return await this.getOrCreateEggObject(proto, name);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* get or create egg object from the Name
|
|
64
|
+
* If get singleton egg object in context,
|
|
65
|
+
* will create context egg object for it.
|
|
66
|
+
*/
|
|
67
|
+
static async getOrCreateEggObjectFromName(name, qualifiers) {
|
|
68
|
+
const proto = EggPrototypeFactory.instance.getPrototype(name, void 0, qualifiers);
|
|
69
|
+
if (!proto) throw new Error(`can not get proto for clazz ${String(name)}`);
|
|
70
|
+
return await this.getOrCreateEggObject(proto, name);
|
|
71
|
+
}
|
|
72
|
+
static getEggObject(proto, name) {
|
|
73
|
+
const container = this.getContainer(proto);
|
|
74
|
+
name = name || proto.name;
|
|
75
|
+
return container.getEggObject(name, proto);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { EggContainerFactory };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CreateObjectMethod, EggObject, EggObjectLifeCycleContext, EggObjectName, EggPrototype, EggPrototypeClass } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/factory/EggObjectFactory.d.ts
|
|
4
|
+
interface EggObjectPair {
|
|
5
|
+
obj: EggObject;
|
|
6
|
+
ctx: EggObjectLifeCycleContext;
|
|
7
|
+
}
|
|
8
|
+
declare class EggObjectFactory {
|
|
9
|
+
static eggObjectMap: Map<string, EggObjectPair>;
|
|
10
|
+
static eggObjectCreateMap: Map<EggPrototypeClass, CreateObjectMethod>;
|
|
11
|
+
static registerEggObjectCreateMethod(protoClass: EggPrototypeClass, method: CreateObjectMethod): void;
|
|
12
|
+
static getEggObjectCreateMethod(protoClass: EggPrototypeClass): CreateObjectMethod;
|
|
13
|
+
static createObject(name: EggObjectName, proto: EggPrototype): Promise<EggObject>;
|
|
14
|
+
static destroyObject(obj: EggObject): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { EggObjectFactory };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { EggObjectImpl } from "../impl/EggObjectImpl.js";
|
|
2
|
+
import { LoadUnitInstanceFactory } from "./LoadUnitInstanceFactory.js";
|
|
3
|
+
import "../impl/index.js";
|
|
4
|
+
import { EggObjectLifecycleUtil } from "../model/EggObject.js";
|
|
5
|
+
import "../model/index.js";
|
|
6
|
+
import { LoadUnitFactory } from "@eggjs/tegg-metadata";
|
|
7
|
+
|
|
8
|
+
//#region src/factory/EggObjectFactory.ts
|
|
9
|
+
var EggObjectFactory = class {
|
|
10
|
+
static eggObjectMap = /* @__PURE__ */ new Map();
|
|
11
|
+
static eggObjectCreateMap = /* @__PURE__ */ new Map();
|
|
12
|
+
static registerEggObjectCreateMethod(protoClass, method) {
|
|
13
|
+
this.eggObjectCreateMap.set(protoClass, method);
|
|
14
|
+
}
|
|
15
|
+
static getEggObjectCreateMethod(protoClass) {
|
|
16
|
+
if (this.eggObjectCreateMap.has(protoClass)) return this.eggObjectCreateMap.get(protoClass);
|
|
17
|
+
return EggObjectImpl.createObject;
|
|
18
|
+
}
|
|
19
|
+
static async createObject(name, proto) {
|
|
20
|
+
const loadUnit = LoadUnitFactory.getLoadUnitById(proto.loadUnitId);
|
|
21
|
+
if (!loadUnit) throw new Error(`not found load unit ${proto.loadUnitId}`);
|
|
22
|
+
const loadUnitInstance = LoadUnitInstanceFactory.getLoadUnitInstance(loadUnit);
|
|
23
|
+
const lifecycleContext = {
|
|
24
|
+
loadUnit,
|
|
25
|
+
loadUnitInstance
|
|
26
|
+
};
|
|
27
|
+
const method = this.getEggObjectCreateMethod(proto.constructor);
|
|
28
|
+
const args = [
|
|
29
|
+
name,
|
|
30
|
+
proto,
|
|
31
|
+
lifecycleContext
|
|
32
|
+
];
|
|
33
|
+
const obj = await Reflect.apply(method, null, args);
|
|
34
|
+
this.eggObjectMap.set(obj.id, {
|
|
35
|
+
obj,
|
|
36
|
+
ctx: lifecycleContext
|
|
37
|
+
});
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
static async destroyObject(obj) {
|
|
41
|
+
const { ctx } = this.eggObjectMap.get(obj.id);
|
|
42
|
+
try {
|
|
43
|
+
if (obj.destroy) await obj.destroy(ctx);
|
|
44
|
+
} finally {
|
|
45
|
+
this.eggObjectMap.delete(obj.id);
|
|
46
|
+
EggObjectLifecycleUtil.clearObjectLifecycle(obj);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { EggObjectFactory };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EggLoadUnitTypeLike, EggPrototype, LoadUnit, LoadUnitInstance, LoadUnitInstanceLifecycleContext } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/factory/LoadUnitInstanceFactory.d.ts
|
|
4
|
+
type LoadUnitInstanceCreator = (ctx: LoadUnitInstanceLifecycleContext) => LoadUnitInstance;
|
|
5
|
+
declare class LoadUnitInstanceFactory {
|
|
6
|
+
private static creatorMap;
|
|
7
|
+
private static instanceMap;
|
|
8
|
+
static registerLoadUnitInstanceClass(type: EggLoadUnitTypeLike, creator: LoadUnitInstanceCreator): void;
|
|
9
|
+
static createLoadUnitInstance(loadUnit: LoadUnit): Promise<LoadUnitInstance>;
|
|
10
|
+
static getLoadUnitInstance(loadUnit: LoadUnit): LoadUnitInstance | undefined;
|
|
11
|
+
static destroyLoadUnitInstance(loadUnitInstance: LoadUnitInstance): Promise<void>;
|
|
12
|
+
static getLoadUnitInstanceByProto(proto: EggPrototype): LoadUnitInstance;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { LoadUnitInstanceFactory };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { EggContainerFactory } from "./EggContainerFactory.js";
|
|
2
|
+
import { LoadUnitInstanceLifecycleUtil } from "../model/LoadUnitInstance.js";
|
|
3
|
+
import { ObjectInitType } from "@eggjs/tegg-types";
|
|
4
|
+
import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
|
|
5
|
+
|
|
6
|
+
//#region src/factory/LoadUnitInstanceFactory.ts
|
|
7
|
+
var LoadUnitInstanceFactory = class {
|
|
8
|
+
static creatorMap = /* @__PURE__ */ new Map();
|
|
9
|
+
static instanceMap = /* @__PURE__ */ new Map();
|
|
10
|
+
static registerLoadUnitInstanceClass(type, creator) {
|
|
11
|
+
this.creatorMap.set(type, creator);
|
|
12
|
+
}
|
|
13
|
+
static async createLoadUnitInstance(loadUnit) {
|
|
14
|
+
const creator = this.creatorMap.get(loadUnit.type);
|
|
15
|
+
if (!creator) throw new Error(`load unit instance type ${loadUnit.type} is not implement`);
|
|
16
|
+
const instanceId = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
17
|
+
if (!this.instanceMap.has(instanceId)) {
|
|
18
|
+
const ctx = { loadUnit };
|
|
19
|
+
const instance = creator(ctx);
|
|
20
|
+
this.instanceMap.set(instanceId, {
|
|
21
|
+
instance,
|
|
22
|
+
ctx
|
|
23
|
+
});
|
|
24
|
+
if (instance.init) await instance.init(ctx);
|
|
25
|
+
}
|
|
26
|
+
return this.instanceMap.get(instanceId).instance;
|
|
27
|
+
}
|
|
28
|
+
static getLoadUnitInstance(loadUnit) {
|
|
29
|
+
const instanceId = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
30
|
+
return this.instanceMap.get(instanceId)?.instance;
|
|
31
|
+
}
|
|
32
|
+
static async destroyLoadUnitInstance(loadUnitInstance) {
|
|
33
|
+
const { ctx } = this.instanceMap.get(loadUnitInstance.id);
|
|
34
|
+
await LoadUnitInstanceLifecycleUtil.objectPreDestroy(ctx, loadUnitInstance);
|
|
35
|
+
if (loadUnitInstance.destroy) await loadUnitInstance.destroy(ctx);
|
|
36
|
+
this.instanceMap.delete(loadUnitInstance.id);
|
|
37
|
+
LoadUnitInstanceLifecycleUtil.clearObjectLifecycle(loadUnitInstance);
|
|
38
|
+
}
|
|
39
|
+
static getLoadUnitInstanceByProto(proto) {
|
|
40
|
+
for (const { instance } of this.instanceMap.values()) if (instance.loadUnit.containPrototype(proto)) return instance;
|
|
41
|
+
throw new Error(`not found load unit for proto ${proto.id}`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.SINGLETON, (proto) => {
|
|
45
|
+
return LoadUnitInstanceFactory.getLoadUnitInstanceByProto(proto);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { LoadUnitInstanceFactory };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EggObject, EggRuntimeContext } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/ContextInitiator.d.ts
|
|
4
|
+
declare class ContextInitiator {
|
|
5
|
+
private readonly eggContext;
|
|
6
|
+
private readonly eggObjectInitRecorder;
|
|
7
|
+
constructor(eggContext: EggRuntimeContext);
|
|
8
|
+
init(obj: EggObject): Promise<void>;
|
|
9
|
+
static createContextInitiator(context: EggRuntimeContext): ContextInitiator;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ContextInitiator };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
+
import { ContextObjectGraph } from "./ContextObjectGraph.js";
|
|
3
|
+
import "../factory/index.js";
|
|
4
|
+
import { LoadUnitFactory } from "@eggjs/tegg-metadata";
|
|
5
|
+
|
|
6
|
+
//#region src/impl/ContextInitiator.ts
|
|
7
|
+
const CONTEXT_INITIATOR = Symbol("EggContext#ContextInitiator");
|
|
8
|
+
var ContextInitiator = class ContextInitiator {
|
|
9
|
+
eggContext;
|
|
10
|
+
eggObjectInitRecorder;
|
|
11
|
+
constructor(eggContext) {
|
|
12
|
+
this.eggContext = eggContext;
|
|
13
|
+
this.eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
14
|
+
this.eggContext.set(CONTEXT_INITIATOR, this);
|
|
15
|
+
}
|
|
16
|
+
async init(obj) {
|
|
17
|
+
if (this.eggObjectInitRecorder.get(obj) === true) return;
|
|
18
|
+
this.eggObjectInitRecorder.set(obj, true);
|
|
19
|
+
const injectObjectProtos = ContextObjectGraph.getContextProto(obj.proto);
|
|
20
|
+
await Promise.all(injectObjectProtos.map(async (injectObject) => {
|
|
21
|
+
const proto = injectObject.proto;
|
|
22
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
23
|
+
await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
static createContextInitiator(context) {
|
|
27
|
+
let initiator = context.get(CONTEXT_INITIATOR);
|
|
28
|
+
if (!initiator) {
|
|
29
|
+
initiator = new ContextInitiator(context);
|
|
30
|
+
context.set(CONTEXT_INITIATOR, initiator);
|
|
31
|
+
}
|
|
32
|
+
return initiator;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
export { ContextInitiator };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EggPrototype, InjectObjectProto } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/ContextObjectGraph.d.ts
|
|
4
|
+
declare class ContextObjectGraph {
|
|
5
|
+
private static eggObjectInitRecorder;
|
|
6
|
+
static getContextProto(proto: EggPrototype): InjectObjectProto[];
|
|
7
|
+
private static doGetContextProto;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ContextObjectGraph };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ObjectInitType } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/ContextObjectGraph.ts
|
|
4
|
+
var InjectProtoHolder = class {
|
|
5
|
+
idSet = /* @__PURE__ */ new Set();
|
|
6
|
+
injectProtos = [];
|
|
7
|
+
addInjectProto(injectObjectProto) {
|
|
8
|
+
const id = `${String(injectObjectProto.objName)}:${injectObjectProto.proto.id}`;
|
|
9
|
+
if (this.idSet.has(id)) return;
|
|
10
|
+
this.idSet.add(id);
|
|
11
|
+
this.injectProtos.push(injectObjectProto);
|
|
12
|
+
}
|
|
13
|
+
dumpProtos() {
|
|
14
|
+
return this.injectProtos;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var ContextObjectGraph = class ContextObjectGraph {
|
|
18
|
+
static eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
19
|
+
static getContextProto(proto) {
|
|
20
|
+
if (ContextObjectGraph.eggObjectInitRecorder.has(proto)) return ContextObjectGraph.eggObjectInitRecorder.get(proto);
|
|
21
|
+
const holder = new InjectProtoHolder();
|
|
22
|
+
this.doGetContextProto(proto, holder);
|
|
23
|
+
const injectObjectProtos = holder.dumpProtos();
|
|
24
|
+
ContextObjectGraph.eggObjectInitRecorder.set(proto, injectObjectProtos);
|
|
25
|
+
return injectObjectProtos;
|
|
26
|
+
}
|
|
27
|
+
static doGetContextProto(proto, holder) {
|
|
28
|
+
for (const injectObject of proto.injectObjects) {
|
|
29
|
+
if (injectObject.proto.initType === ObjectInitType.CONTEXT && proto.initType !== ObjectInitType.CONTEXT) holder.addInjectProto(injectObject);
|
|
30
|
+
ContextObjectGraph.doGetContextProto(injectObject.proto, holder);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { ContextObjectGraph };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { EggContainer, EggObject, EggObjectName, EggPrototype, Id, LifecycleContext } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/EggAlwaysNewObjectContainer.d.ts
|
|
4
|
+
declare class EggAlwaysNewObjectContainer implements EggContainer<LifecycleContext> {
|
|
5
|
+
static instance: EggAlwaysNewObjectContainer;
|
|
6
|
+
readonly id: Id;
|
|
7
|
+
constructor();
|
|
8
|
+
addProtoToCreate(): void;
|
|
9
|
+
deleteProtoToCreate(): void;
|
|
10
|
+
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
11
|
+
getEggObject(): EggObject;
|
|
12
|
+
getOrCreateEggObject(name: string, proto: EggPrototype): Promise<EggObject>;
|
|
13
|
+
destroy(): Promise<void>;
|
|
14
|
+
init(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { EggAlwaysNewObjectContainer };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
+
import { EggObjectFactory } from "../factory/EggObjectFactory.js";
|
|
3
|
+
import "../factory/index.js";
|
|
4
|
+
import { ObjectInitType } from "@eggjs/tegg-types";
|
|
5
|
+
|
|
6
|
+
//#region src/impl/EggAlwaysNewObjectContainer.ts
|
|
7
|
+
var EggAlwaysNewObjectContainer = class EggAlwaysNewObjectContainer {
|
|
8
|
+
static instance = new EggAlwaysNewObjectContainer();
|
|
9
|
+
id;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.id = "ALWAYS_NEW_OBJECT_CONTAINER";
|
|
12
|
+
}
|
|
13
|
+
addProtoToCreate() {}
|
|
14
|
+
deleteProtoToCreate() {}
|
|
15
|
+
iterateProtoToCreate() {
|
|
16
|
+
return (/* @__PURE__ */ new Map()).entries();
|
|
17
|
+
}
|
|
18
|
+
getEggObject() {
|
|
19
|
+
throw new Error("Always Object can not use getEggObject, should use getOrCreateEggObject");
|
|
20
|
+
}
|
|
21
|
+
async getOrCreateEggObject(name, proto) {
|
|
22
|
+
return EggObjectFactory.createObject(name, proto);
|
|
23
|
+
}
|
|
24
|
+
async destroy() {}
|
|
25
|
+
async init() {}
|
|
26
|
+
};
|
|
27
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.ALWAYS_NEW, () => EggAlwaysNewObjectContainer.instance);
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { EggAlwaysNewObjectContainer };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EggObject, EggObjectLifeCycleContext, EggObjectName, EggPrototype } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/EggObjectImpl.d.ts
|
|
4
|
+
declare class EggObjectImpl implements EggObject {
|
|
5
|
+
private _obj;
|
|
6
|
+
private status;
|
|
7
|
+
readonly proto: EggPrototype;
|
|
8
|
+
readonly name: EggObjectName;
|
|
9
|
+
readonly id: string;
|
|
10
|
+
constructor(name: EggObjectName, proto: EggPrototype);
|
|
11
|
+
initWithInjectProperty(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
12
|
+
initWithInjectConstructor(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
13
|
+
init(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
14
|
+
destroy(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
15
|
+
injectProperty(name: EggObjectName, descriptor: PropertyDescriptor): void;
|
|
16
|
+
get obj(): object;
|
|
17
|
+
get isReady(): boolean;
|
|
18
|
+
static createObject(name: EggObjectName, proto: EggPrototype, lifecycleContext: EggObjectLifeCycleContext): Promise<EggObjectImpl>;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { EggObjectImpl };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
+
import { EggObjectUtil } from "./EggObjectUtil.js";
|
|
3
|
+
import "../factory/index.js";
|
|
4
|
+
import { ContextHandler } from "../model/ContextHandler.js";
|
|
5
|
+
import { EggObjectLifecycleUtil } from "../model/EggObject.js";
|
|
6
|
+
import "../model/index.js";
|
|
7
|
+
import { EggObjectStatus, InjectType, ObjectInitType } from "@eggjs/tegg-types";
|
|
8
|
+
import { LoadUnitFactory } from "@eggjs/tegg-metadata";
|
|
9
|
+
import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
|
|
10
|
+
|
|
11
|
+
//#region src/impl/EggObjectImpl.ts
|
|
12
|
+
var EggObjectImpl = class EggObjectImpl {
|
|
13
|
+
_obj;
|
|
14
|
+
status = EggObjectStatus.PENDING;
|
|
15
|
+
proto;
|
|
16
|
+
name;
|
|
17
|
+
id;
|
|
18
|
+
constructor(name, proto) {
|
|
19
|
+
this.name = name;
|
|
20
|
+
this.proto = proto;
|
|
21
|
+
const ctx = ContextHandler.getContext();
|
|
22
|
+
this.id = IdenticalUtil.createObjectId(this.proto.id, ctx?.id);
|
|
23
|
+
}
|
|
24
|
+
async initWithInjectProperty(ctx) {
|
|
25
|
+
try {
|
|
26
|
+
this._obj = this.proto.constructEggObject();
|
|
27
|
+
const objLifecycleHook = this._obj;
|
|
28
|
+
await EggObjectLifecycleUtil.objectPreCreate(ctx, this);
|
|
29
|
+
const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook("postConstruct", this.proto) ?? "postConstruct";
|
|
30
|
+
if (objLifecycleHook[postConstructMethod]) await objLifecycleHook[postConstructMethod](ctx, this);
|
|
31
|
+
const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("preInject", this.proto) ?? "preInject";
|
|
32
|
+
if (objLifecycleHook[preInjectMethod]) await objLifecycleHook[preInjectMethod](ctx, this);
|
|
33
|
+
await Promise.all(this.proto.injectObjects.map(async (injectObject) => {
|
|
34
|
+
const proto = injectObject.proto;
|
|
35
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
36
|
+
if (this.proto.initType !== ObjectInitType.CONTEXT && injectObject.proto.initType === ObjectInitType.CONTEXT) this.injectProperty(injectObject.refName, EggObjectUtil.contextEggObjectGetProperty(proto, injectObject.objName));
|
|
37
|
+
else {
|
|
38
|
+
const injectObj = await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
39
|
+
this.injectProperty(injectObject.refName, EggObjectUtil.eggObjectGetProperty(injectObj));
|
|
40
|
+
}
|
|
41
|
+
}));
|
|
42
|
+
await EggObjectLifecycleUtil.objectPostCreate(ctx, this);
|
|
43
|
+
const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("postInject", this.proto) ?? "postInject";
|
|
44
|
+
if (objLifecycleHook[postInjectMethod]) await objLifecycleHook[postInjectMethod](ctx, this);
|
|
45
|
+
const initMethod = EggObjectLifecycleUtil.getLifecycleHook("init", this.proto) ?? "init";
|
|
46
|
+
if (objLifecycleHook[initMethod]) await objLifecycleHook[initMethod](ctx, this);
|
|
47
|
+
this.status = EggObjectStatus.READY;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
this.status = EggObjectStatus.ERROR;
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async initWithInjectConstructor(ctx) {
|
|
54
|
+
try {
|
|
55
|
+
const constructArgs = await Promise.all(this.proto.injectObjects.map(async (injectObject) => {
|
|
56
|
+
const proto = injectObject.proto;
|
|
57
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
58
|
+
if (this.proto.initType !== ObjectInitType.CONTEXT && injectObject.proto.initType === ObjectInitType.CONTEXT) return EggObjectUtil.contextEggObjectProxy(proto, injectObject.objName);
|
|
59
|
+
const injectObj = await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
60
|
+
return EggObjectUtil.eggObjectProxy(injectObj);
|
|
61
|
+
}));
|
|
62
|
+
if (typeof this.proto.multiInstanceConstructorIndex !== "undefined") {
|
|
63
|
+
const qualifiers = this.proto.multiInstanceConstructorAttributes?.map((t) => {
|
|
64
|
+
return {
|
|
65
|
+
attribute: t,
|
|
66
|
+
value: this.proto.getQualifier(t)
|
|
67
|
+
};
|
|
68
|
+
})?.filter((t) => typeof t.value !== "undefined") ?? [];
|
|
69
|
+
const objInfo = {
|
|
70
|
+
name: this.proto.name,
|
|
71
|
+
qualifiers
|
|
72
|
+
};
|
|
73
|
+
constructArgs.splice(this.proto.multiInstanceConstructorIndex, 0, objInfo);
|
|
74
|
+
}
|
|
75
|
+
this._obj = this.proto.constructEggObject(...constructArgs);
|
|
76
|
+
const objLifecycleHook = this._obj;
|
|
77
|
+
await EggObjectLifecycleUtil.objectPreCreate(ctx, this);
|
|
78
|
+
const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook("postConstruct", this.proto) ?? "postConstruct";
|
|
79
|
+
if (objLifecycleHook[postConstructMethod]) await objLifecycleHook[postConstructMethod](ctx, this);
|
|
80
|
+
const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("preInject", this.proto) ?? "preInject";
|
|
81
|
+
if (objLifecycleHook[preInjectMethod]) await objLifecycleHook[preInjectMethod](ctx, this);
|
|
82
|
+
await EggObjectLifecycleUtil.objectPostCreate(ctx, this);
|
|
83
|
+
const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("postInject", this.proto) ?? "postInject";
|
|
84
|
+
if (objLifecycleHook[postInjectMethod]) await objLifecycleHook[postInjectMethod](ctx, this);
|
|
85
|
+
const initMethod = EggObjectLifecycleUtil.getLifecycleHook("init", this.proto) ?? "init";
|
|
86
|
+
if (objLifecycleHook[initMethod]) await objLifecycleHook[initMethod](ctx, this);
|
|
87
|
+
this.status = EggObjectStatus.READY;
|
|
88
|
+
} catch (e) {
|
|
89
|
+
this.status = EggObjectStatus.ERROR;
|
|
90
|
+
throw e;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async init(ctx) {
|
|
94
|
+
if (this.proto.injectType === InjectType.CONSTRUCTOR) await this.initWithInjectConstructor(ctx);
|
|
95
|
+
else await this.initWithInjectProperty(ctx);
|
|
96
|
+
}
|
|
97
|
+
async destroy(ctx) {
|
|
98
|
+
if (this.status === EggObjectStatus.READY) {
|
|
99
|
+
this.status = EggObjectStatus.DESTROYING;
|
|
100
|
+
await EggObjectLifecycleUtil.objectPreDestroy(ctx, this);
|
|
101
|
+
const objLifecycleHook = this._obj;
|
|
102
|
+
const preDestroyMethod = EggObjectLifecycleUtil.getLifecycleHook("preDestroy", this.proto) ?? "preDestroy";
|
|
103
|
+
if (objLifecycleHook[preDestroyMethod]) await objLifecycleHook[preDestroyMethod](ctx, this);
|
|
104
|
+
const destroyMethod = EggObjectLifecycleUtil.getLifecycleHook("destroy", this.proto) ?? "destroy";
|
|
105
|
+
if (objLifecycleHook[destroyMethod]) await objLifecycleHook[destroyMethod](ctx, this);
|
|
106
|
+
this.status = EggObjectStatus.DESTROYED;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
injectProperty(name, descriptor) {
|
|
110
|
+
Reflect.defineProperty(this._obj, name, descriptor);
|
|
111
|
+
}
|
|
112
|
+
get obj() {
|
|
113
|
+
return this._obj;
|
|
114
|
+
}
|
|
115
|
+
get isReady() {
|
|
116
|
+
return this.status === EggObjectStatus.READY;
|
|
117
|
+
}
|
|
118
|
+
static async createObject(name, proto, lifecycleContext) {
|
|
119
|
+
const obj = new EggObjectImpl(name, proto);
|
|
120
|
+
await obj.init(lifecycleContext);
|
|
121
|
+
return obj;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
export { EggObjectImpl };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EggObject, EggPrototype } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/impl/EggObjectUtil.d.ts
|
|
4
|
+
declare class EggObjectUtil {
|
|
5
|
+
static eggObjectGetProperty(eggObject: EggObject): PropertyDescriptor;
|
|
6
|
+
static contextEggObjectGetProperty(proto: EggPrototype, objName: PropertyKey): PropertyDescriptor;
|
|
7
|
+
static eggObjectProxy(eggObject: EggObject): PropertyDescriptor;
|
|
8
|
+
static contextEggObjectProxy(proto: EggPrototype, objName: PropertyKey): PropertyDescriptor;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { EggObjectUtil };
|