@eggjs/tegg-runtime 4.0.0-beta.6 → 4.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{factory/EggContainerFactory.js → ContextInitiator-DazHfq05.js} +86 -6
- package/dist/ContextInitiator-GmZYvMSm.js +3 -0
- package/dist/index.d.ts +168 -14
- package/dist/index.js +524 -17
- package/package.json +7 -7
- package/dist/factory/EggContainerFactory.d.ts +0 -30
- package/dist/factory/EggObjectFactory.d.ts +0 -17
- package/dist/factory/EggObjectFactory.js +0 -52
- package/dist/factory/LoadUnitInstanceFactory.d.ts +0 -15
- package/dist/factory/LoadUnitInstanceFactory.js +0 -49
- package/dist/factory/index.js +0 -5
- package/dist/impl/ContextInitiator.d.ts +0 -12
- package/dist/impl/ContextInitiator.js +0 -37
- package/dist/impl/ContextObjectGraph.d.ts +0 -10
- package/dist/impl/ContextObjectGraph.js +0 -36
- package/dist/impl/EggAlwaysNewObjectContainer.d.ts +0 -17
- package/dist/impl/EggAlwaysNewObjectContainer.js +0 -30
- package/dist/impl/EggObjectImpl.d.ts +0 -21
- package/dist/impl/EggObjectImpl.js +0 -126
- package/dist/impl/EggObjectUtil.d.ts +0 -11
- package/dist/impl/EggObjectUtil.js +0 -147
- package/dist/impl/ModuleLoadUnitInstance.d.ts +0 -22
- package/dist/impl/ModuleLoadUnitInstance.js +0 -73
- package/dist/impl/index.js +0 -8
- package/dist/model/AbstractEggContext.d.ts +0 -22
- package/dist/model/AbstractEggContext.js +0 -83
- package/dist/model/ContextHandler.d.ts +0 -12
- package/dist/model/ContextHandler.js +0 -18
- package/dist/model/EggContext.d.ts +0 -7
- package/dist/model/EggContext.js +0 -7
- package/dist/model/EggObject.d.ts +0 -7
- package/dist/model/EggObject.js +0 -7
- package/dist/model/LoadUnitInstance.d.ts +0 -7
- package/dist/model/LoadUnitInstance.js +0 -7
- package/dist/model/index.js +0 -7
|
@@ -1,15 +0,0 @@
|
|
|
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 };
|
|
@@ -1,49 +0,0 @@
|
|
|
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 };
|
package/dist/factory/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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 };
|
|
@@ -1,37 +0,0 @@
|
|
|
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 };
|
|
@@ -1,10 +0,0 @@
|
|
|
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 };
|
|
@@ -1,36 +0,0 @@
|
|
|
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 };
|
|
@@ -1,17 +0,0 @@
|
|
|
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 };
|
|
@@ -1,30 +0,0 @@
|
|
|
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 };
|
|
@@ -1,21 +0,0 @@
|
|
|
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 };
|
|
@@ -1,126 +0,0 @@
|
|
|
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 };
|
|
@@ -1,11 +0,0 @@
|
|
|
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 };
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
-
import "../factory/index.js";
|
|
3
|
-
|
|
4
|
-
//#region src/impl/EggObjectUtil.ts
|
|
5
|
-
var EggObjectUtil = class {
|
|
6
|
-
static eggObjectGetProperty(eggObject) {
|
|
7
|
-
return {
|
|
8
|
-
get() {
|
|
9
|
-
return eggObject.obj;
|
|
10
|
-
},
|
|
11
|
-
configurable: true,
|
|
12
|
-
enumerable: true
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
static contextEggObjectGetProperty(proto, objName) {
|
|
16
|
-
const PROTO_OBJ_GETTER = Symbol(`EggPrototype#objGetter#${String(objName)}`);
|
|
17
|
-
if (!proto[PROTO_OBJ_GETTER]) proto[PROTO_OBJ_GETTER] = {
|
|
18
|
-
get() {
|
|
19
|
-
return EggContainerFactory.getEggObject(proto, objName).obj;
|
|
20
|
-
},
|
|
21
|
-
configurable: true,
|
|
22
|
-
enumerable: true
|
|
23
|
-
};
|
|
24
|
-
return proto[PROTO_OBJ_GETTER];
|
|
25
|
-
}
|
|
26
|
-
static eggObjectProxy(eggObject) {
|
|
27
|
-
let _obj;
|
|
28
|
-
function getObj() {
|
|
29
|
-
if (!_obj) _obj = eggObject.obj;
|
|
30
|
-
return _obj;
|
|
31
|
-
}
|
|
32
|
-
return new Proxy({}, {
|
|
33
|
-
defineProperty(_target, property, attributes) {
|
|
34
|
-
const obj = getObj();
|
|
35
|
-
Object.defineProperty(obj, property, attributes);
|
|
36
|
-
return true;
|
|
37
|
-
},
|
|
38
|
-
deleteProperty(_target, p) {
|
|
39
|
-
const obj = getObj();
|
|
40
|
-
delete obj[p];
|
|
41
|
-
return true;
|
|
42
|
-
},
|
|
43
|
-
get(target, p) {
|
|
44
|
-
if (p === "then") return;
|
|
45
|
-
if (Object.prototype[p]) return target[p];
|
|
46
|
-
const obj = getObj();
|
|
47
|
-
const val = obj[p];
|
|
48
|
-
if (typeof val === "function") return val.bind(obj);
|
|
49
|
-
return val;
|
|
50
|
-
},
|
|
51
|
-
getOwnPropertyDescriptor(_target, p) {
|
|
52
|
-
const obj = getObj();
|
|
53
|
-
return Object.getOwnPropertyDescriptor(obj, p);
|
|
54
|
-
},
|
|
55
|
-
getPrototypeOf() {
|
|
56
|
-
const obj = getObj();
|
|
57
|
-
return Object.getPrototypeOf(obj);
|
|
58
|
-
},
|
|
59
|
-
has(_target, p) {
|
|
60
|
-
const obj = getObj();
|
|
61
|
-
return p in obj;
|
|
62
|
-
},
|
|
63
|
-
isExtensible() {
|
|
64
|
-
const obj = getObj();
|
|
65
|
-
return Object.isExtensible(obj);
|
|
66
|
-
},
|
|
67
|
-
ownKeys() {
|
|
68
|
-
const obj = getObj();
|
|
69
|
-
return Reflect.ownKeys(obj);
|
|
70
|
-
},
|
|
71
|
-
preventExtensions() {
|
|
72
|
-
const obj = getObj();
|
|
73
|
-
Object.preventExtensions(obj);
|
|
74
|
-
return true;
|
|
75
|
-
},
|
|
76
|
-
set(_target, p, newValue) {
|
|
77
|
-
const obj = getObj();
|
|
78
|
-
obj[p] = newValue;
|
|
79
|
-
return true;
|
|
80
|
-
},
|
|
81
|
-
setPrototypeOf(_target, v) {
|
|
82
|
-
const obj = getObj();
|
|
83
|
-
Object.setPrototypeOf(obj, v);
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
static contextEggObjectProxy(proto, objName) {
|
|
89
|
-
const PROTO_OBJ_PROXY = Symbol(`EggPrototype#objProxy#${String(objName)}`);
|
|
90
|
-
if (!proto[PROTO_OBJ_PROXY]) proto[PROTO_OBJ_PROXY] = new Proxy({}, {
|
|
91
|
-
defineProperty(_target, property, attributes) {
|
|
92
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
93
|
-
Object.defineProperty(obj, property, attributes);
|
|
94
|
-
return true;
|
|
95
|
-
},
|
|
96
|
-
deleteProperty(_target, p) {
|
|
97
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
98
|
-
delete obj[p];
|
|
99
|
-
return true;
|
|
100
|
-
},
|
|
101
|
-
get(target, p) {
|
|
102
|
-
if (p === "then") return;
|
|
103
|
-
if (Object.prototype[p]) return target[p];
|
|
104
|
-
return EggContainerFactory.getEggObject(proto, objName).obj[p];
|
|
105
|
-
},
|
|
106
|
-
getOwnPropertyDescriptor(_target, p) {
|
|
107
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
108
|
-
return Object.getOwnPropertyDescriptor(obj, p);
|
|
109
|
-
},
|
|
110
|
-
getPrototypeOf() {
|
|
111
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
112
|
-
return Object.getPrototypeOf(obj);
|
|
113
|
-
},
|
|
114
|
-
has(_target, p) {
|
|
115
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
116
|
-
return p in obj;
|
|
117
|
-
},
|
|
118
|
-
isExtensible() {
|
|
119
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
120
|
-
return Object.isExtensible(obj);
|
|
121
|
-
},
|
|
122
|
-
ownKeys() {
|
|
123
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
124
|
-
return Reflect.ownKeys(obj);
|
|
125
|
-
},
|
|
126
|
-
preventExtensions() {
|
|
127
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
128
|
-
Object.preventExtensions(obj);
|
|
129
|
-
return true;
|
|
130
|
-
},
|
|
131
|
-
set(_target, p, newValue) {
|
|
132
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
133
|
-
obj[p] = newValue;
|
|
134
|
-
return true;
|
|
135
|
-
},
|
|
136
|
-
setPrototypeOf(_target, v) {
|
|
137
|
-
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
138
|
-
Object.setPrototypeOf(obj, v);
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
return proto[PROTO_OBJ_PROXY];
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
//#endregion
|
|
147
|
-
export { EggObjectUtil };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { EggObject, EggObjectName, EggPrototype, EggPrototypeName, LoadUnit, LoadUnitInstance, LoadUnitInstanceLifecycleContext } from "@eggjs/tegg-types";
|
|
2
|
-
|
|
3
|
-
//#region src/impl/ModuleLoadUnitInstance.d.ts
|
|
4
|
-
declare class ModuleLoadUnitInstance implements LoadUnitInstance {
|
|
5
|
-
readonly loadUnit: LoadUnit;
|
|
6
|
-
readonly id: string;
|
|
7
|
-
readonly name: string;
|
|
8
|
-
private protoToCreateMap;
|
|
9
|
-
private eggObjectMap;
|
|
10
|
-
private eggObjectPromiseMap;
|
|
11
|
-
constructor(loadUnit: LoadUnit);
|
|
12
|
-
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
13
|
-
addProtoToCreate(name: string, proto: EggPrototype): void;
|
|
14
|
-
deleteProtoToCreate(name: string): void;
|
|
15
|
-
init(ctx: LoadUnitInstanceLifecycleContext): Promise<void>;
|
|
16
|
-
destroy(): Promise<void>;
|
|
17
|
-
getOrCreateEggObject(name: EggPrototypeName, proto: EggPrototype): Promise<EggObject>;
|
|
18
|
-
getEggObject(name: EggPrototypeName, proto: EggPrototype): EggObject;
|
|
19
|
-
static createModuleLoadUnitInstance(ctx: LoadUnitInstanceLifecycleContext): LoadUnitInstance;
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { ModuleLoadUnitInstance };
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { LoadUnitInstanceLifecycleUtil } from "../model/LoadUnitInstance.js";
|
|
2
|
-
import { LoadUnitInstanceFactory } from "../factory/LoadUnitInstanceFactory.js";
|
|
3
|
-
import { EggObjectFactory } from "../factory/EggObjectFactory.js";
|
|
4
|
-
import { EggLoadUnitType, ObjectInitType } from "@eggjs/tegg-types";
|
|
5
|
-
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
6
|
-
import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
|
|
7
|
-
|
|
8
|
-
//#region src/impl/ModuleLoadUnitInstance.ts
|
|
9
|
-
var ModuleLoadUnitInstance = class ModuleLoadUnitInstance {
|
|
10
|
-
loadUnit;
|
|
11
|
-
id;
|
|
12
|
-
name;
|
|
13
|
-
protoToCreateMap = [];
|
|
14
|
-
eggObjectMap = /* @__PURE__ */ new Map();
|
|
15
|
-
eggObjectPromiseMap = /* @__PURE__ */ new Map();
|
|
16
|
-
constructor(loadUnit) {
|
|
17
|
-
this.loadUnit = loadUnit;
|
|
18
|
-
this.name = loadUnit.name;
|
|
19
|
-
const iterator = this.loadUnit.iterateEggPrototype();
|
|
20
|
-
for (const proto of iterator) if (proto.initType === ObjectInitType.SINGLETON) this.protoToCreateMap.push([proto.name, proto]);
|
|
21
|
-
this.id = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
22
|
-
}
|
|
23
|
-
iterateProtoToCreate() {
|
|
24
|
-
return this.protoToCreateMap[Symbol.iterator]();
|
|
25
|
-
}
|
|
26
|
-
addProtoToCreate(name, proto) {
|
|
27
|
-
this.protoToCreateMap.push([name, proto]);
|
|
28
|
-
}
|
|
29
|
-
deleteProtoToCreate(name) {
|
|
30
|
-
const index = this.protoToCreateMap.findIndex(([protoName]) => protoName === name);
|
|
31
|
-
if (index !== -1) this.protoToCreateMap.splice(index, 1);
|
|
32
|
-
}
|
|
33
|
-
async init(ctx) {
|
|
34
|
-
await LoadUnitInstanceLifecycleUtil.objectPreCreate(ctx, this);
|
|
35
|
-
for (const [name, proto] of this.protoToCreateMap) await this.getOrCreateEggObject(name, proto);
|
|
36
|
-
await LoadUnitInstanceLifecycleUtil.objectPostCreate(ctx, this);
|
|
37
|
-
}
|
|
38
|
-
async destroy() {
|
|
39
|
-
const objs = [];
|
|
40
|
-
for (const protoObjMap of this.eggObjectMap.values()) for (const obj of protoObjMap.values()) objs.push(obj);
|
|
41
|
-
this.eggObjectMap.clear();
|
|
42
|
-
await Promise.all(objs.map(async (obj) => {
|
|
43
|
-
await EggObjectFactory.destroyObject(obj);
|
|
44
|
-
}));
|
|
45
|
-
}
|
|
46
|
-
async getOrCreateEggObject(name, proto) {
|
|
47
|
-
if (!this.loadUnit.containPrototype(proto)) throw new Error("load unit not contain proto");
|
|
48
|
-
const protoObjMap = MapUtil.getOrStore(this.eggObjectMap, proto.id, /* @__PURE__ */ new Map());
|
|
49
|
-
if (!protoObjMap.has(name)) {
|
|
50
|
-
const protoObjPromiseMap = MapUtil.getOrStore(this.eggObjectPromiseMap, proto.id, /* @__PURE__ */ new Map());
|
|
51
|
-
if (!protoObjPromiseMap.has(name)) {
|
|
52
|
-
const objPromise = EggObjectFactory.createObject(name, proto);
|
|
53
|
-
protoObjPromiseMap.set(name, objPromise);
|
|
54
|
-
const obj = await objPromise;
|
|
55
|
-
protoObjPromiseMap.delete(name);
|
|
56
|
-
protoObjMap.set(name, obj);
|
|
57
|
-
} else await protoObjPromiseMap.get(name);
|
|
58
|
-
}
|
|
59
|
-
return protoObjMap.get(name);
|
|
60
|
-
}
|
|
61
|
-
getEggObject(name, proto) {
|
|
62
|
-
const protoObjMap = this.eggObjectMap.get(proto.id);
|
|
63
|
-
if (!protoObjMap || !protoObjMap.has(name)) throw new Error(`EggObject ${String(proto.name)} not found`);
|
|
64
|
-
return protoObjMap.get(name);
|
|
65
|
-
}
|
|
66
|
-
static createModuleLoadUnitInstance(ctx) {
|
|
67
|
-
return new ModuleLoadUnitInstance(ctx.loadUnit);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
LoadUnitInstanceFactory.registerLoadUnitInstanceClass(EggLoadUnitType.MODULE, ModuleLoadUnitInstance.createModuleLoadUnitInstance);
|
|
71
|
-
|
|
72
|
-
//#endregion
|
|
73
|
-
export { ModuleLoadUnitInstance };
|
package/dist/impl/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ContextObjectGraph } from "./ContextObjectGraph.js";
|
|
2
|
-
import { ContextInitiator } from "./ContextInitiator.js";
|
|
3
|
-
import { EggAlwaysNewObjectContainer } from "./EggAlwaysNewObjectContainer.js";
|
|
4
|
-
import { EggObjectUtil } from "./EggObjectUtil.js";
|
|
5
|
-
import { EggObjectImpl } from "./EggObjectImpl.js";
|
|
6
|
-
import { ModuleLoadUnitInstance } from "./ModuleLoadUnitInstance.js";
|
|
7
|
-
|
|
8
|
-
export { };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { EggContextLifecycleContext, EggObject, EggObjectName, EggPrototype, EggPrototypeName, EggRuntimeContext } from "@eggjs/tegg-types";
|
|
2
|
-
|
|
3
|
-
//#region src/model/AbstractEggContext.d.ts
|
|
4
|
-
declare abstract class AbstractEggContext implements EggRuntimeContext {
|
|
5
|
-
private contextData;
|
|
6
|
-
private protoToCreate;
|
|
7
|
-
private eggObjectMap;
|
|
8
|
-
private eggObjectPromiseMap;
|
|
9
|
-
private destroyed;
|
|
10
|
-
abstract id: string;
|
|
11
|
-
addProtoToCreate(name: string, proto: EggPrototype): void;
|
|
12
|
-
deleteProtoToCreate(name: string): void;
|
|
13
|
-
destroy(ctx: EggContextLifecycleContext): Promise<void>;
|
|
14
|
-
get(key: string | symbol): any | undefined;
|
|
15
|
-
getEggObject(name: EggPrototypeName, proto: EggPrototype): EggObject;
|
|
16
|
-
getOrCreateEggObject(name: EggPrototypeName, proto: EggPrototype): Promise<EggObject>;
|
|
17
|
-
init(ctx: EggContextLifecycleContext): Promise<void>;
|
|
18
|
-
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
19
|
-
set(key: string | symbol, val: any): void;
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { AbstractEggContext };
|