@eggjs/tegg-runtime 4.0.0-beta.8 → 4.0.1-beta.0
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/factory/EggContainerFactory.d.ts +30 -0
- package/dist/{ContextInitiator-DazHfq05.js → factory/EggContainerFactory.js} +5 -85
- package/dist/factory/EggObjectFactory.d.ts +17 -0
- package/dist/factory/EggObjectFactory.js +49 -0
- package/dist/factory/LoadUnitInstanceFactory.d.ts +15 -0
- package/dist/factory/LoadUnitInstanceFactory.js +49 -0
- package/dist/factory/index.d.ts +3 -0
- package/dist/factory/index.js +5 -0
- package/dist/impl/ContextInitiator.d.ts +13 -0
- package/dist/impl/ContextInitiator.js +44 -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 +29 -0
- package/dist/impl/EggObjectImpl.d.ts +21 -0
- package/dist/impl/EggObjectImpl.js +124 -0
- package/dist/impl/EggObjectUtil.d.ts +11 -0
- package/dist/impl/EggObjectUtil.js +144 -0
- package/dist/impl/ModuleLoadUnitInstance.d.ts +22 -0
- package/dist/impl/ModuleLoadUnitInstance.js +73 -0
- package/dist/impl/index.d.ts +6 -0
- package/dist/impl/index.js +8 -0
- package/dist/index.d.ts +17 -168
- package/dist/index.js +17 -524
- package/dist/model/AbstractEggContext.d.ts +22 -0
- package/dist/model/AbstractEggContext.js +82 -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.d.ts +5 -0
- package/dist/model/index.js +7 -0
- package/package.json +33 -37
- package/dist/ContextInitiator-GmZYvMSm.js +0 -3
|
@@ -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 };
|
|
@@ -1,25 +1,9 @@
|
|
|
1
|
+
import { ContextHandler } from "../model/ContextHandler.js";
|
|
1
2
|
import { debuglog } from "node:util";
|
|
2
|
-
import {
|
|
3
|
-
import { EggPrototypeFactory, LoadUnitFactory } from "@eggjs/tegg-metadata";
|
|
3
|
+
import { EggPrototypeFactory } from "@eggjs/metadata";
|
|
4
4
|
import { NameUtil } from "@eggjs/tegg-common-util";
|
|
5
5
|
import { PrototypeUtil } from "@eggjs/core-decorator";
|
|
6
|
-
import assert from "node:assert";
|
|
7
6
|
|
|
8
|
-
//#region src/model/ContextHandler.ts
|
|
9
|
-
var ContextHandler = class {
|
|
10
|
-
static getContextCallback;
|
|
11
|
-
static runInContextCallback;
|
|
12
|
-
static getContext() {
|
|
13
|
-
assert(this.getContextCallback, "getContextCallback not set");
|
|
14
|
-
return this.getContextCallback ? this.getContextCallback() : void 0;
|
|
15
|
-
}
|
|
16
|
-
static run(context, fn) {
|
|
17
|
-
assert(this.runInContextCallback, "runInContextCallback not set");
|
|
18
|
-
return this.runInContextCallback(context, fn);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
7
|
//#region src/factory/EggContainerFactory.ts
|
|
24
8
|
const debug = debuglog("tegg/core/runtime/EggContainerFactory");
|
|
25
9
|
var EggContainerFactory = class EggContainerFactory {
|
|
@@ -46,9 +30,9 @@ var EggContainerFactory = class EggContainerFactory {
|
|
|
46
30
|
const ctx = ContextHandler.getContext();
|
|
47
31
|
if (ctx) {
|
|
48
32
|
if (!EggContainerFactory.ContextInitiatorClass) {
|
|
49
|
-
const { ContextInitiator
|
|
33
|
+
const { ContextInitiator } = await import("../impl/ContextInitiator.js");
|
|
50
34
|
debug("import ContextInitiator to fix dependency cycle");
|
|
51
|
-
EggContainerFactory.ContextInitiatorClass = ContextInitiator
|
|
35
|
+
EggContainerFactory.ContextInitiatorClass = ContextInitiator;
|
|
52
36
|
}
|
|
53
37
|
await EggContainerFactory.ContextInitiatorClass.createContextInitiator(ctx).init(obj);
|
|
54
38
|
debug("getOrCreateEggObject with context eggObject:%o, from proto:%o, name:%s", obj.name, proto.name, name);
|
|
@@ -93,68 +77,4 @@ var EggContainerFactory = class EggContainerFactory {
|
|
|
93
77
|
};
|
|
94
78
|
|
|
95
79
|
//#endregion
|
|
96
|
-
|
|
97
|
-
var InjectProtoHolder = class {
|
|
98
|
-
idSet = /* @__PURE__ */ new Set();
|
|
99
|
-
injectProtos = [];
|
|
100
|
-
addInjectProto(injectObjectProto) {
|
|
101
|
-
const id = `${String(injectObjectProto.objName)}:${injectObjectProto.proto.id}`;
|
|
102
|
-
if (this.idSet.has(id)) return;
|
|
103
|
-
this.idSet.add(id);
|
|
104
|
-
this.injectProtos.push(injectObjectProto);
|
|
105
|
-
}
|
|
106
|
-
dumpProtos() {
|
|
107
|
-
return this.injectProtos;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
var ContextObjectGraph = class ContextObjectGraph {
|
|
111
|
-
static eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
112
|
-
static getContextProto(proto) {
|
|
113
|
-
if (ContextObjectGraph.eggObjectInitRecorder.has(proto)) return ContextObjectGraph.eggObjectInitRecorder.get(proto);
|
|
114
|
-
const holder = new InjectProtoHolder();
|
|
115
|
-
this.doGetContextProto(proto, holder);
|
|
116
|
-
const injectObjectProtos = holder.dumpProtos();
|
|
117
|
-
ContextObjectGraph.eggObjectInitRecorder.set(proto, injectObjectProtos);
|
|
118
|
-
return injectObjectProtos;
|
|
119
|
-
}
|
|
120
|
-
static doGetContextProto(proto, holder) {
|
|
121
|
-
for (const injectObject of proto.injectObjects) {
|
|
122
|
-
if (injectObject.proto.initType === ObjectInitType.CONTEXT && proto.initType !== ObjectInitType.CONTEXT) holder.addInjectProto(injectObject);
|
|
123
|
-
ContextObjectGraph.doGetContextProto(injectObject.proto, holder);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
//#endregion
|
|
129
|
-
//#region src/impl/ContextInitiator.ts
|
|
130
|
-
const CONTEXT_INITIATOR = Symbol("EggContext#ContextInitiator");
|
|
131
|
-
var ContextInitiator = class ContextInitiator {
|
|
132
|
-
eggContext;
|
|
133
|
-
eggObjectInitRecorder;
|
|
134
|
-
constructor(eggContext) {
|
|
135
|
-
this.eggContext = eggContext;
|
|
136
|
-
this.eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
137
|
-
this.eggContext.set(CONTEXT_INITIATOR, this);
|
|
138
|
-
}
|
|
139
|
-
async init(obj) {
|
|
140
|
-
if (this.eggObjectInitRecorder.get(obj) === true) return;
|
|
141
|
-
this.eggObjectInitRecorder.set(obj, true);
|
|
142
|
-
const injectObjectProtos = ContextObjectGraph.getContextProto(obj.proto);
|
|
143
|
-
await Promise.all(injectObjectProtos.map(async (injectObject) => {
|
|
144
|
-
const proto = injectObject.proto;
|
|
145
|
-
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
146
|
-
await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
147
|
-
}));
|
|
148
|
-
}
|
|
149
|
-
static createContextInitiator(context) {
|
|
150
|
-
let initiator = context.get(CONTEXT_INITIATOR);
|
|
151
|
-
if (!initiator) {
|
|
152
|
-
initiator = new ContextInitiator(context);
|
|
153
|
-
context.set(CONTEXT_INITIATOR, initiator);
|
|
154
|
-
}
|
|
155
|
-
return initiator;
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
//#endregion
|
|
160
|
-
export { ContextHandler, ContextInitiator, ContextObjectGraph, EggContainerFactory };
|
|
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,49 @@
|
|
|
1
|
+
import { EggObjectLifecycleUtil } from "../model/EggObject.js";
|
|
2
|
+
import { EggObjectImpl } from "../impl/EggObjectImpl.js";
|
|
3
|
+
import { LoadUnitInstanceFactory } from "./LoadUnitInstanceFactory.js";
|
|
4
|
+
import { LoadUnitFactory } from "@eggjs/metadata";
|
|
5
|
+
|
|
6
|
+
//#region src/factory/EggObjectFactory.ts
|
|
7
|
+
var EggObjectFactory = class {
|
|
8
|
+
static eggObjectMap = /* @__PURE__ */ new Map();
|
|
9
|
+
static eggObjectCreateMap = /* @__PURE__ */ new Map();
|
|
10
|
+
static registerEggObjectCreateMethod(protoClass, method) {
|
|
11
|
+
this.eggObjectCreateMap.set(protoClass, method);
|
|
12
|
+
}
|
|
13
|
+
static getEggObjectCreateMethod(protoClass) {
|
|
14
|
+
if (this.eggObjectCreateMap.has(protoClass)) return this.eggObjectCreateMap.get(protoClass);
|
|
15
|
+
return EggObjectImpl.createObject;
|
|
16
|
+
}
|
|
17
|
+
static async createObject(name, proto) {
|
|
18
|
+
const loadUnit = LoadUnitFactory.getLoadUnitById(proto.loadUnitId);
|
|
19
|
+
if (!loadUnit) throw new Error(`not found load unit ${proto.loadUnitId}`);
|
|
20
|
+
const lifecycleContext = {
|
|
21
|
+
loadUnit,
|
|
22
|
+
loadUnitInstance: LoadUnitInstanceFactory.getLoadUnitInstance(loadUnit)
|
|
23
|
+
};
|
|
24
|
+
const method = this.getEggObjectCreateMethod(proto.constructor);
|
|
25
|
+
const args = [
|
|
26
|
+
name,
|
|
27
|
+
proto,
|
|
28
|
+
lifecycleContext
|
|
29
|
+
];
|
|
30
|
+
const obj = await Reflect.apply(method, null, args);
|
|
31
|
+
this.eggObjectMap.set(obj.id, {
|
|
32
|
+
obj,
|
|
33
|
+
ctx: lifecycleContext
|
|
34
|
+
});
|
|
35
|
+
return obj;
|
|
36
|
+
}
|
|
37
|
+
static async destroyObject(obj) {
|
|
38
|
+
const { ctx } = this.eggObjectMap.get(obj.id);
|
|
39
|
+
try {
|
|
40
|
+
if (obj.destroy) await obj.destroy(ctx);
|
|
41
|
+
} finally {
|
|
42
|
+
this.eggObjectMap.delete(obj.id);
|
|
43
|
+
EggObjectLifecycleUtil.clearObjectLifecycle(obj);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
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/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,13 @@
|
|
|
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
|
+
private readonly eggObjectInitPromise;
|
|
8
|
+
constructor(eggContext: EggRuntimeContext);
|
|
9
|
+
init(obj: EggObject): Promise<void>;
|
|
10
|
+
static createContextInitiator(context: EggRuntimeContext): ContextInitiator;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { ContextInitiator };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
+
import { ContextObjectGraph } from "./ContextObjectGraph.js";
|
|
3
|
+
import { LoadUnitFactory } from "@eggjs/metadata";
|
|
4
|
+
|
|
5
|
+
//#region src/impl/ContextInitiator.ts
|
|
6
|
+
const CONTEXT_INITIATOR = Symbol("EggContext#ContextInitiator");
|
|
7
|
+
var ContextInitiator = class ContextInitiator {
|
|
8
|
+
eggContext;
|
|
9
|
+
eggObjectInitRecorder;
|
|
10
|
+
eggObjectInitPromise;
|
|
11
|
+
constructor(eggContext) {
|
|
12
|
+
this.eggContext = eggContext;
|
|
13
|
+
this.eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
14
|
+
this.eggObjectInitPromise = /* @__PURE__ */ new WeakMap();
|
|
15
|
+
this.eggContext.set(CONTEXT_INITIATOR, this);
|
|
16
|
+
}
|
|
17
|
+
async init(obj) {
|
|
18
|
+
if (this.eggObjectInitRecorder.get(obj) === true) {
|
|
19
|
+
if (this.eggObjectInitPromise.has(obj)) await this.eggObjectInitPromise.get(obj);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
this.eggObjectInitRecorder.set(obj, true);
|
|
23
|
+
const injectObjectProtos = ContextObjectGraph.getContextProto(obj.proto);
|
|
24
|
+
const initPromise = Promise.all(injectObjectProtos.map(async (injectObject) => {
|
|
25
|
+
const proto = injectObject.proto;
|
|
26
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
27
|
+
await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
28
|
+
}));
|
|
29
|
+
this.eggObjectInitPromise.set(obj, initPromise);
|
|
30
|
+
await initPromise;
|
|
31
|
+
this.eggObjectInitPromise.delete(obj);
|
|
32
|
+
}
|
|
33
|
+
static createContextInitiator(context) {
|
|
34
|
+
let initiator = context.get(CONTEXT_INITIATOR);
|
|
35
|
+
if (!initiator) {
|
|
36
|
+
initiator = new ContextInitiator(context);
|
|
37
|
+
context.set(CONTEXT_INITIATOR, initiator);
|
|
38
|
+
}
|
|
39
|
+
return initiator;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
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,29 @@
|
|
|
1
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
+
import { EggObjectFactory } from "../factory/EggObjectFactory.js";
|
|
3
|
+
import { ObjectInitType } from "@eggjs/tegg-types";
|
|
4
|
+
|
|
5
|
+
//#region src/impl/EggAlwaysNewObjectContainer.ts
|
|
6
|
+
var EggAlwaysNewObjectContainer = class EggAlwaysNewObjectContainer {
|
|
7
|
+
static instance = new EggAlwaysNewObjectContainer();
|
|
8
|
+
id;
|
|
9
|
+
constructor() {
|
|
10
|
+
this.id = "ALWAYS_NEW_OBJECT_CONTAINER";
|
|
11
|
+
}
|
|
12
|
+
addProtoToCreate() {}
|
|
13
|
+
deleteProtoToCreate() {}
|
|
14
|
+
iterateProtoToCreate() {
|
|
15
|
+
return (/* @__PURE__ */ new Map()).entries();
|
|
16
|
+
}
|
|
17
|
+
getEggObject() {
|
|
18
|
+
throw new Error("Always Object can not use getEggObject, should use getOrCreateEggObject");
|
|
19
|
+
}
|
|
20
|
+
async getOrCreateEggObject(name, proto) {
|
|
21
|
+
return EggObjectFactory.createObject(name, proto);
|
|
22
|
+
}
|
|
23
|
+
async destroy() {}
|
|
24
|
+
async init() {}
|
|
25
|
+
};
|
|
26
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.ALWAYS_NEW, () => EggAlwaysNewObjectContainer.instance);
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
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,124 @@
|
|
|
1
|
+
import { ContextHandler } from "../model/ContextHandler.js";
|
|
2
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
3
|
+
import { EggObjectLifecycleUtil } from "../model/EggObject.js";
|
|
4
|
+
import { EggObjectUtil } from "./EggObjectUtil.js";
|
|
5
|
+
import { LoadUnitFactory } from "@eggjs/metadata";
|
|
6
|
+
import { EggObjectStatus, InjectType, ObjectInitType } from "@eggjs/tegg-types";
|
|
7
|
+
import { IdenticalUtil } from "@eggjs/lifecycle";
|
|
8
|
+
|
|
9
|
+
//#region src/impl/EggObjectImpl.ts
|
|
10
|
+
var EggObjectImpl = class EggObjectImpl {
|
|
11
|
+
_obj;
|
|
12
|
+
status = EggObjectStatus.PENDING;
|
|
13
|
+
proto;
|
|
14
|
+
name;
|
|
15
|
+
id;
|
|
16
|
+
constructor(name, proto) {
|
|
17
|
+
this.name = name;
|
|
18
|
+
this.proto = proto;
|
|
19
|
+
const ctx = ContextHandler.getContext();
|
|
20
|
+
this.id = IdenticalUtil.createObjectId(this.proto.id, ctx?.id);
|
|
21
|
+
}
|
|
22
|
+
async initWithInjectProperty(ctx) {
|
|
23
|
+
try {
|
|
24
|
+
this._obj = this.proto.constructEggObject();
|
|
25
|
+
const objLifecycleHook = this._obj;
|
|
26
|
+
await EggObjectLifecycleUtil.objectPreCreate(ctx, this);
|
|
27
|
+
const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook("postConstruct", this.proto) ?? "postConstruct";
|
|
28
|
+
if (objLifecycleHook[postConstructMethod]) await objLifecycleHook[postConstructMethod](ctx, this);
|
|
29
|
+
const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("preInject", this.proto) ?? "preInject";
|
|
30
|
+
if (objLifecycleHook[preInjectMethod]) await objLifecycleHook[preInjectMethod](ctx, this);
|
|
31
|
+
await Promise.all(this.proto.injectObjects.map(async (injectObject) => {
|
|
32
|
+
const proto = injectObject.proto;
|
|
33
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
34
|
+
if (this.proto.initType !== ObjectInitType.CONTEXT && injectObject.proto.initType === ObjectInitType.CONTEXT) this.injectProperty(injectObject.refName, EggObjectUtil.contextEggObjectGetProperty(proto, injectObject.objName));
|
|
35
|
+
else {
|
|
36
|
+
const injectObj = await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
37
|
+
this.injectProperty(injectObject.refName, EggObjectUtil.eggObjectGetProperty(injectObj));
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
await EggObjectLifecycleUtil.objectPostCreate(ctx, this);
|
|
41
|
+
const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("postInject", this.proto) ?? "postInject";
|
|
42
|
+
if (objLifecycleHook[postInjectMethod]) await objLifecycleHook[postInjectMethod](ctx, this);
|
|
43
|
+
const initMethod = EggObjectLifecycleUtil.getLifecycleHook("init", this.proto) ?? "init";
|
|
44
|
+
if (objLifecycleHook[initMethod]) await objLifecycleHook[initMethod](ctx, this);
|
|
45
|
+
this.status = EggObjectStatus.READY;
|
|
46
|
+
} catch (e) {
|
|
47
|
+
this.status = EggObjectStatus.ERROR;
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async initWithInjectConstructor(ctx) {
|
|
52
|
+
try {
|
|
53
|
+
const constructArgs = await Promise.all(this.proto.injectObjects.map(async (injectObject) => {
|
|
54
|
+
const proto = injectObject.proto;
|
|
55
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
56
|
+
if (this.proto.initType !== ObjectInitType.CONTEXT && injectObject.proto.initType === ObjectInitType.CONTEXT) return EggObjectUtil.contextEggObjectProxy(proto, injectObject.objName);
|
|
57
|
+
const injectObj = await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
58
|
+
return EggObjectUtil.eggObjectProxy(injectObj);
|
|
59
|
+
}));
|
|
60
|
+
if (typeof this.proto.multiInstanceConstructorIndex !== "undefined") {
|
|
61
|
+
const qualifiers = this.proto.multiInstanceConstructorAttributes?.map((t) => {
|
|
62
|
+
return {
|
|
63
|
+
attribute: t,
|
|
64
|
+
value: this.proto.getQualifier(t)
|
|
65
|
+
};
|
|
66
|
+
})?.filter((t) => typeof t.value !== "undefined") ?? [];
|
|
67
|
+
const objInfo = {
|
|
68
|
+
name: this.proto.name,
|
|
69
|
+
qualifiers
|
|
70
|
+
};
|
|
71
|
+
constructArgs.splice(this.proto.multiInstanceConstructorIndex, 0, objInfo);
|
|
72
|
+
}
|
|
73
|
+
this._obj = this.proto.constructEggObject(...constructArgs);
|
|
74
|
+
const objLifecycleHook = this._obj;
|
|
75
|
+
await EggObjectLifecycleUtil.objectPreCreate(ctx, this);
|
|
76
|
+
const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook("postConstruct", this.proto) ?? "postConstruct";
|
|
77
|
+
if (objLifecycleHook[postConstructMethod]) await objLifecycleHook[postConstructMethod](ctx, this);
|
|
78
|
+
const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("preInject", this.proto) ?? "preInject";
|
|
79
|
+
if (objLifecycleHook[preInjectMethod]) await objLifecycleHook[preInjectMethod](ctx, this);
|
|
80
|
+
await EggObjectLifecycleUtil.objectPostCreate(ctx, this);
|
|
81
|
+
const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("postInject", this.proto) ?? "postInject";
|
|
82
|
+
if (objLifecycleHook[postInjectMethod]) await objLifecycleHook[postInjectMethod](ctx, this);
|
|
83
|
+
const initMethod = EggObjectLifecycleUtil.getLifecycleHook("init", this.proto) ?? "init";
|
|
84
|
+
if (objLifecycleHook[initMethod]) await objLifecycleHook[initMethod](ctx, this);
|
|
85
|
+
this.status = EggObjectStatus.READY;
|
|
86
|
+
} catch (e) {
|
|
87
|
+
this.status = EggObjectStatus.ERROR;
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async init(ctx) {
|
|
92
|
+
if (this.proto.injectType === InjectType.CONSTRUCTOR) await this.initWithInjectConstructor(ctx);
|
|
93
|
+
else await this.initWithInjectProperty(ctx);
|
|
94
|
+
}
|
|
95
|
+
async destroy(ctx) {
|
|
96
|
+
if (this.status === EggObjectStatus.READY) {
|
|
97
|
+
this.status = EggObjectStatus.DESTROYING;
|
|
98
|
+
await EggObjectLifecycleUtil.objectPreDestroy(ctx, this);
|
|
99
|
+
const objLifecycleHook = this._obj;
|
|
100
|
+
const preDestroyMethod = EggObjectLifecycleUtil.getLifecycleHook("preDestroy", this.proto) ?? "preDestroy";
|
|
101
|
+
if (objLifecycleHook[preDestroyMethod]) await objLifecycleHook[preDestroyMethod](ctx, this);
|
|
102
|
+
const destroyMethod = EggObjectLifecycleUtil.getLifecycleHook("destroy", this.proto) ?? "destroy";
|
|
103
|
+
if (objLifecycleHook[destroyMethod]) await objLifecycleHook[destroyMethod](ctx, this);
|
|
104
|
+
this.status = EggObjectStatus.DESTROYED;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
injectProperty(name, descriptor) {
|
|
108
|
+
Reflect.defineProperty(this._obj, name, descriptor);
|
|
109
|
+
}
|
|
110
|
+
get obj() {
|
|
111
|
+
return this._obj;
|
|
112
|
+
}
|
|
113
|
+
get isReady() {
|
|
114
|
+
return this.status === EggObjectStatus.READY;
|
|
115
|
+
}
|
|
116
|
+
static async createObject(name, proto, lifecycleContext) {
|
|
117
|
+
const obj = new EggObjectImpl(name, proto);
|
|
118
|
+
await obj.init(lifecycleContext);
|
|
119
|
+
return obj;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
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 };
|