@eggjs/tegg-runtime 4.0.0-beta.3 → 4.0.0-beta.5
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
|
@@ -0,0 +1,147 @@
|
|
|
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 };
|
|
@@ -0,0 +1,22 @@
|
|
|
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 };
|
|
@@ -0,0 +1,73 @@
|
|
|
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 };
|
|
@@ -0,0 +1,8 @@
|
|
|
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 { };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AbstractEggContext } from "./model/AbstractEggContext.js";
|
|
2
|
+
import { ContextHandler } from "./model/ContextHandler.js";
|
|
3
|
+
import { EggContextLifecycleUtil } from "./model/EggContext.js";
|
|
4
|
+
import { EggObjectLifecycleUtil } from "./model/EggObject.js";
|
|
5
|
+
import { LoadUnitInstanceLifecycleUtil } from "./model/LoadUnitInstance.js";
|
|
6
|
+
import { ContextInitiator } from "./impl/ContextInitiator.js";
|
|
7
|
+
import { ContextObjectGraph } from "./impl/ContextObjectGraph.js";
|
|
8
|
+
import { EggAlwaysNewObjectContainer } from "./impl/EggAlwaysNewObjectContainer.js";
|
|
9
|
+
import { EggObjectImpl } from "./impl/EggObjectImpl.js";
|
|
10
|
+
import { EggObjectUtil } from "./impl/EggObjectUtil.js";
|
|
11
|
+
import { ModuleLoadUnitInstance } from "./impl/ModuleLoadUnitInstance.js";
|
|
12
|
+
import { EggContainerFactory } from "./factory/EggContainerFactory.js";
|
|
13
|
+
import { EggObjectFactory } from "./factory/EggObjectFactory.js";
|
|
14
|
+
import { LoadUnitInstanceFactory } from "./factory/LoadUnitInstanceFactory.js";
|
|
15
|
+
import { EggRuntimeContext as EggContext } from "@eggjs/tegg-types/runtime";
|
|
16
|
+
export * from "@eggjs/tegg-types/runtime";
|
|
17
|
+
export { AbstractEggContext, ContextHandler, ContextInitiator, ContextObjectGraph, EggAlwaysNewObjectContainer, EggContainerFactory, type EggContext, EggContextLifecycleUtil, EggObjectFactory, EggObjectImpl, EggObjectLifecycleUtil, EggObjectUtil, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil, ModuleLoadUnitInstance };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EggContainerFactory } from "./factory/EggContainerFactory.js";
|
|
2
|
+
import { ContextObjectGraph } from "./impl/ContextObjectGraph.js";
|
|
3
|
+
import { ContextInitiator } from "./impl/ContextInitiator.js";
|
|
4
|
+
import { EggAlwaysNewObjectContainer } from "./impl/EggAlwaysNewObjectContainer.js";
|
|
5
|
+
import { EggObjectUtil } from "./impl/EggObjectUtil.js";
|
|
6
|
+
import { EggObjectImpl } from "./impl/EggObjectImpl.js";
|
|
7
|
+
import { LoadUnitInstanceLifecycleUtil } from "./model/LoadUnitInstance.js";
|
|
8
|
+
import { LoadUnitInstanceFactory } from "./factory/LoadUnitInstanceFactory.js";
|
|
9
|
+
import { ModuleLoadUnitInstance } from "./impl/ModuleLoadUnitInstance.js";
|
|
10
|
+
import "./impl/index.js";
|
|
11
|
+
import { EggObjectFactory } from "./factory/EggObjectFactory.js";
|
|
12
|
+
import "./factory/index.js";
|
|
13
|
+
import { ContextHandler } from "./model/ContextHandler.js";
|
|
14
|
+
import { EggContextLifecycleUtil } from "./model/EggContext.js";
|
|
15
|
+
import { AbstractEggContext } from "./model/AbstractEggContext.js";
|
|
16
|
+
import { EggObjectLifecycleUtil } from "./model/EggObject.js";
|
|
17
|
+
import "./model/index.js";
|
|
18
|
+
|
|
19
|
+
export * from "@eggjs/tegg-types/runtime"
|
|
20
|
+
|
|
21
|
+
export { AbstractEggContext, ContextHandler, ContextInitiator, ContextObjectGraph, EggAlwaysNewObjectContainer, EggContainerFactory, EggContextLifecycleUtil, EggObjectFactory, EggObjectImpl, EggObjectLifecycleUtil, EggObjectUtil, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil, ModuleLoadUnitInstance };
|
|
@@ -0,0 +1,22 @@
|
|
|
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 };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { EggContainerFactory } from "../factory/EggContainerFactory.js";
|
|
2
|
+
import { EggObjectFactory } from "../factory/EggObjectFactory.js";
|
|
3
|
+
import "../factory/index.js";
|
|
4
|
+
import { ContextHandler } from "./ContextHandler.js";
|
|
5
|
+
import { EggContextLifecycleUtil } from "./EggContext.js";
|
|
6
|
+
import { debuglog } from "node:util";
|
|
7
|
+
import { ObjectInitType } from "@eggjs/tegg-types";
|
|
8
|
+
import { TeggError } from "@eggjs/tegg-metadata";
|
|
9
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
10
|
+
|
|
11
|
+
//#region src/model/AbstractEggContext.ts
|
|
12
|
+
const debug = debuglog("tegg/core/runtime/AbstractEggContext");
|
|
13
|
+
var AbstractEggContext = class {
|
|
14
|
+
contextData = /* @__PURE__ */ new Map();
|
|
15
|
+
protoToCreate = /* @__PURE__ */ new Map();
|
|
16
|
+
eggObjectMap = /* @__PURE__ */ new Map();
|
|
17
|
+
eggObjectPromiseMap = /* @__PURE__ */ new Map();
|
|
18
|
+
destroyed = false;
|
|
19
|
+
addProtoToCreate(name, proto) {
|
|
20
|
+
this.protoToCreate.set(name, proto);
|
|
21
|
+
}
|
|
22
|
+
deleteProtoToCreate(name) {
|
|
23
|
+
this.protoToCreate.delete(name);
|
|
24
|
+
}
|
|
25
|
+
async destroy(ctx) {
|
|
26
|
+
await EggContextLifecycleUtil.objectPreDestroy(ctx, this);
|
|
27
|
+
const objs = [];
|
|
28
|
+
for (const protoObjMap of this.eggObjectMap.values()) for (const protoObj of protoObjMap.values()) objs.push(protoObj);
|
|
29
|
+
this.eggObjectMap.clear();
|
|
30
|
+
await Promise.all(objs.map(async (obj) => {
|
|
31
|
+
await EggObjectFactory.destroyObject(obj);
|
|
32
|
+
}));
|
|
33
|
+
this.contextData.clear();
|
|
34
|
+
this.destroyed = true;
|
|
35
|
+
await EggContextLifecycleUtil.clearObjectLifecycle(this);
|
|
36
|
+
}
|
|
37
|
+
get(key) {
|
|
38
|
+
return this.contextData.get(key);
|
|
39
|
+
}
|
|
40
|
+
getEggObject(name, proto) {
|
|
41
|
+
if (this.destroyed) throw TeggError.create(`Can not read property \`${String(name)}\` because egg ctx has been destroyed`, "read_after_ctx_destroyed");
|
|
42
|
+
const protoObjMap = this.eggObjectMap.get(proto.id);
|
|
43
|
+
if (!protoObjMap || !protoObjMap.has(name)) throw new Error(`EggObject ${String(proto.name)} not found`);
|
|
44
|
+
return protoObjMap.get(name);
|
|
45
|
+
}
|
|
46
|
+
async getOrCreateEggObject(name, proto) {
|
|
47
|
+
const protoObjMap = MapUtil.getOrStore(this.eggObjectMap, proto.id, /* @__PURE__ */ new Map());
|
|
48
|
+
if (!protoObjMap.has(name)) {
|
|
49
|
+
const protoObjPromiseMap = MapUtil.getOrStore(this.eggObjectPromiseMap, proto.id, /* @__PURE__ */ new Map());
|
|
50
|
+
if (!protoObjPromiseMap.has(name)) {
|
|
51
|
+
const objPromise = EggObjectFactory.createObject(name, proto);
|
|
52
|
+
protoObjPromiseMap.set(name, objPromise);
|
|
53
|
+
const obj = await objPromise;
|
|
54
|
+
protoObjPromiseMap.delete(name);
|
|
55
|
+
if (!protoObjPromiseMap.size) this.eggObjectPromiseMap.delete(proto.id);
|
|
56
|
+
protoObjMap.set(name, obj);
|
|
57
|
+
} else await protoObjPromiseMap.get(name);
|
|
58
|
+
}
|
|
59
|
+
return protoObjMap.get(name);
|
|
60
|
+
}
|
|
61
|
+
async init(ctx) {
|
|
62
|
+
await EggContextLifecycleUtil.objectPreCreate(ctx, this);
|
|
63
|
+
for (const [name, proto] of this.protoToCreate) await this.getOrCreateEggObject(name, proto);
|
|
64
|
+
await EggContextLifecycleUtil.objectPostCreate(ctx, this);
|
|
65
|
+
}
|
|
66
|
+
iterateProtoToCreate() {
|
|
67
|
+
return this.protoToCreate.entries();
|
|
68
|
+
}
|
|
69
|
+
set(key, val) {
|
|
70
|
+
this.contextData.set(key, val);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.CONTEXT, () => {
|
|
74
|
+
const ctx = ContextHandler.getContext();
|
|
75
|
+
if (!ctx) {
|
|
76
|
+
debug("can not get teggCtx from ContextHandler");
|
|
77
|
+
throw new Error("ctx is required");
|
|
78
|
+
}
|
|
79
|
+
return ctx;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
export { AbstractEggContext };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EggRuntimeContext } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/model/ContextHandler.d.ts
|
|
4
|
+
type runInContextCallback<R = any> = (context: EggRuntimeContext, fn: () => Promise<R>) => Promise<R>;
|
|
5
|
+
declare class ContextHandler {
|
|
6
|
+
static getContextCallback: () => EggRuntimeContext | undefined;
|
|
7
|
+
static runInContextCallback: runInContextCallback;
|
|
8
|
+
static getContext(): EggRuntimeContext | undefined;
|
|
9
|
+
static run<R = any>(context: EggRuntimeContext, fn: () => Promise<R>): Promise<R>;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ContextHandler };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
|
|
3
|
+
//#region src/model/ContextHandler.ts
|
|
4
|
+
var ContextHandler = class {
|
|
5
|
+
static getContextCallback;
|
|
6
|
+
static runInContextCallback;
|
|
7
|
+
static getContext() {
|
|
8
|
+
assert(this.getContextCallback, "getContextCallback not set");
|
|
9
|
+
return this.getContextCallback ? this.getContextCallback() : void 0;
|
|
10
|
+
}
|
|
11
|
+
static run(context, fn) {
|
|
12
|
+
assert(this.runInContextCallback, "runInContextCallback not set");
|
|
13
|
+
return this.runInContextCallback(context, fn);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ContextHandler };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EggContextLifecycleContext, EggRuntimeContext } from "@eggjs/tegg-types";
|
|
2
|
+
import { LifecycleUtil } from "@eggjs/tegg-lifecycle";
|
|
3
|
+
|
|
4
|
+
//#region src/model/EggContext.d.ts
|
|
5
|
+
declare const EggContextLifecycleUtil: LifecycleUtil<EggContextLifecycleContext, EggRuntimeContext>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { EggContextLifecycleUtil };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EggObject, EggObjectLifeCycleContext } from "@eggjs/tegg-types";
|
|
2
|
+
import { LifecycleUtil } from "@eggjs/tegg-lifecycle";
|
|
3
|
+
|
|
4
|
+
//#region src/model/EggObject.d.ts
|
|
5
|
+
declare const EggObjectLifecycleUtil: LifecycleUtil<EggObjectLifeCycleContext, EggObject>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { EggObjectLifecycleUtil };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LoadUnitInstance, LoadUnitInstanceLifecycleContext } from "@eggjs/tegg-types";
|
|
2
|
+
import { LifecycleUtil } from "@eggjs/tegg-lifecycle";
|
|
3
|
+
|
|
4
|
+
//#region src/model/LoadUnitInstance.d.ts
|
|
5
|
+
declare const LoadUnitInstanceLifecycleUtil: LifecycleUtil<LoadUnitInstanceLifecycleContext, LoadUnitInstance>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { LoadUnitInstanceLifecycleUtil };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LoadUnitInstanceLifecycleUtil } from "./LoadUnitInstance.js";
|
|
2
|
+
import { ContextHandler } from "./ContextHandler.js";
|
|
3
|
+
import { EggContextLifecycleUtil } from "./EggContext.js";
|
|
4
|
+
import { AbstractEggContext } from "./AbstractEggContext.js";
|
|
5
|
+
import { EggObjectLifecycleUtil } from "./EggObject.js";
|
|
6
|
+
|
|
7
|
+
export { };
|
package/package.json
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/tegg-runtime",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.5",
|
|
4
4
|
"description": "tegg runtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
-
".":
|
|
8
|
-
|
|
9
|
-
"default": "./src/index.js"
|
|
10
|
-
}
|
|
7
|
+
".": "./dist/index.js",
|
|
8
|
+
"./package.json": "./package.json"
|
|
11
9
|
},
|
|
12
10
|
"files": [
|
|
13
|
-
"
|
|
14
|
-
"src/**/*.d.ts"
|
|
11
|
+
"dist"
|
|
15
12
|
],
|
|
16
13
|
"keywords": [
|
|
17
14
|
"egg",
|
|
@@ -19,15 +16,9 @@
|
|
|
19
16
|
"runtime",
|
|
20
17
|
"tegg"
|
|
21
18
|
],
|
|
22
|
-
"scripts": {
|
|
23
|
-
"clean": "tsc -b --clean",
|
|
24
|
-
"tsc": "npm run clean && tsc -p ./tsconfig.json",
|
|
25
|
-
"tsc:pub": "npm run tsc",
|
|
26
|
-
"prepublishOnly": "npm run tsc"
|
|
27
|
-
},
|
|
28
19
|
"author": "killagu <killa123@126.com>",
|
|
29
20
|
"license": "MIT",
|
|
30
|
-
"homepage": "https://github.com/eggjs/tegg",
|
|
21
|
+
"homepage": "https://github.com/eggjs/tegg/tree/next/core/runtime",
|
|
31
22
|
"bugs": {
|
|
32
23
|
"url": "https://github.com/eggjs/tegg/issues"
|
|
33
24
|
},
|
|
@@ -37,23 +28,32 @@
|
|
|
37
28
|
"directory": "core/runtime"
|
|
38
29
|
},
|
|
39
30
|
"engines": {
|
|
40
|
-
"node": ">=
|
|
31
|
+
"node": ">=22.18.0"
|
|
41
32
|
},
|
|
42
33
|
"dependencies": {
|
|
43
|
-
"@eggjs/core-decorator": "
|
|
44
|
-
"@eggjs/tegg-
|
|
45
|
-
"@eggjs/tegg-
|
|
46
|
-
"@eggjs/tegg-
|
|
47
|
-
"@eggjs/tegg-types": "
|
|
34
|
+
"@eggjs/core-decorator": "4.0.0-beta.5",
|
|
35
|
+
"@eggjs/tegg-lifecycle": "4.0.0-beta.5",
|
|
36
|
+
"@eggjs/tegg-metadata": "4.0.0-beta.5",
|
|
37
|
+
"@eggjs/tegg-common-util": "4.0.0-beta.5",
|
|
38
|
+
"@eggjs/tegg-types": "4.0.0-beta.5"
|
|
48
39
|
},
|
|
49
40
|
"devDependencies": {
|
|
50
|
-
"@
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
41
|
+
"@types/node": "^22.10.5",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"tsdown": "^0.15.6",
|
|
44
|
+
"unplugin-unused": "^0.5.3",
|
|
45
|
+
"@eggjs/module-test-util": "4.0.0-beta.4",
|
|
46
|
+
"@eggjs/tegg-loader": "4.0.0-beta.5"
|
|
54
47
|
},
|
|
55
48
|
"publishConfig": {
|
|
56
49
|
"access": "public"
|
|
57
50
|
},
|
|
58
|
-
"
|
|
59
|
-
|
|
51
|
+
"main": "./dist/index.js",
|
|
52
|
+
"module": "./dist/index.js",
|
|
53
|
+
"types": "./dist/index.d.ts",
|
|
54
|
+
"scripts": {
|
|
55
|
+
"clean": "rimraf dist",
|
|
56
|
+
"build": "tsdown",
|
|
57
|
+
"typecheck": "tsc --noEmit"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { ContainerGetMethod, EggContainer, EggObject, EggObjectName, EggProtoImplClass, EggPrototype, LifecycleContext, ObjectInitTypeLike, QualifierInfo } from '@eggjs/tegg-types';
|
|
2
|
-
export declare class EggContainerFactory {
|
|
3
|
-
private static containerGetMethodMap;
|
|
4
|
-
private static ContextInitiatorClass;
|
|
5
|
-
static registerContainerGetMethod(initType: ObjectInitTypeLike, method: ContainerGetMethod): void;
|
|
6
|
-
static getContainer(proto: EggPrototype): EggContainer<LifecycleContext>;
|
|
7
|
-
/**
|
|
8
|
-
* get or create egg object
|
|
9
|
-
* If get singleton egg object in context,
|
|
10
|
-
* will create context egg object for it.
|
|
11
|
-
*/
|
|
12
|
-
static getOrCreateEggObject(proto: EggPrototype, name?: EggObjectName): Promise<EggObject>;
|
|
13
|
-
/**
|
|
14
|
-
* get or create egg object from the Class
|
|
15
|
-
* If get singleton egg object in context,
|
|
16
|
-
* will create context egg object for it.
|
|
17
|
-
*/
|
|
18
|
-
static getOrCreateEggObjectFromClazz(clazz: EggProtoImplClass, name?: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject>;
|
|
19
|
-
/**
|
|
20
|
-
* get or create egg object from the Name
|
|
21
|
-
* If get singleton egg object in context,
|
|
22
|
-
* will create context egg object for it.
|
|
23
|
-
*/
|
|
24
|
-
static getOrCreateEggObjectFromName(name: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject>;
|
|
25
|
-
static getEggObject(proto: EggPrototype, name?: EggObjectName): EggObject;
|
|
26
|
-
}
|