@eggjs/tegg-runtime 4.0.0-beta.7 → 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} +85 -5
- 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 -50
- 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 -36
- 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 -29
- package/dist/impl/EggObjectImpl.d.ts +0 -21
- package/dist/impl/EggObjectImpl.js +0 -124
- package/dist/impl/EggObjectUtil.d.ts +0 -11
- package/dist/impl/EggObjectUtil.js +0 -146
- 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 -82
- 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
package/dist/index.js
CHANGED
|
@@ -1,21 +1,528 @@
|
|
|
1
|
-
import { ContextHandler } from "./
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { LoadUnitInstanceFactory } from "./factory/LoadUnitInstanceFactory.js";
|
|
8
|
-
import { EggObjectFactory } from "./factory/EggObjectFactory.js";
|
|
9
|
-
import { EggContextLifecycleUtil } from "./model/EggContext.js";
|
|
10
|
-
import { AbstractEggContext } from "./model/AbstractEggContext.js";
|
|
11
|
-
import "./model/index.js";
|
|
12
|
-
import { ContextObjectGraph } from "./impl/ContextObjectGraph.js";
|
|
13
|
-
import { ContextInitiator } from "./impl/ContextInitiator.js";
|
|
14
|
-
import { EggAlwaysNewObjectContainer } from "./impl/EggAlwaysNewObjectContainer.js";
|
|
15
|
-
import { ModuleLoadUnitInstance } from "./impl/ModuleLoadUnitInstance.js";
|
|
16
|
-
import "./impl/index.js";
|
|
17
|
-
import "./factory/index.js";
|
|
1
|
+
import { ContextHandler, ContextInitiator, ContextObjectGraph, EggContainerFactory } from "./ContextInitiator-DazHfq05.js";
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
import { EggLoadUnitType, EggObjectStatus, InjectType, ObjectInitType } from "@eggjs/tegg-types";
|
|
4
|
+
import { LoadUnitFactory, TeggError } from "@eggjs/tegg-metadata";
|
|
5
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
6
|
+
import { IdenticalUtil, LifecycleUtil } from "@eggjs/tegg-lifecycle";
|
|
18
7
|
|
|
19
8
|
export * from "@eggjs/tegg-types/runtime"
|
|
20
9
|
|
|
10
|
+
//#region src/model/EggObject.ts
|
|
11
|
+
const EggObjectLifecycleUtil = new LifecycleUtil();
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/impl/EggObjectUtil.ts
|
|
15
|
+
var EggObjectUtil = class {
|
|
16
|
+
static eggObjectGetProperty(eggObject) {
|
|
17
|
+
return {
|
|
18
|
+
get() {
|
|
19
|
+
return eggObject.obj;
|
|
20
|
+
},
|
|
21
|
+
configurable: true,
|
|
22
|
+
enumerable: true
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
static contextEggObjectGetProperty(proto, objName) {
|
|
26
|
+
const PROTO_OBJ_GETTER = Symbol(`EggPrototype#objGetter#${String(objName)}`);
|
|
27
|
+
if (!proto[PROTO_OBJ_GETTER]) proto[PROTO_OBJ_GETTER] = {
|
|
28
|
+
get() {
|
|
29
|
+
return EggContainerFactory.getEggObject(proto, objName).obj;
|
|
30
|
+
},
|
|
31
|
+
configurable: true,
|
|
32
|
+
enumerable: true
|
|
33
|
+
};
|
|
34
|
+
return proto[PROTO_OBJ_GETTER];
|
|
35
|
+
}
|
|
36
|
+
static eggObjectProxy(eggObject) {
|
|
37
|
+
let _obj;
|
|
38
|
+
function getObj() {
|
|
39
|
+
if (!_obj) _obj = eggObject.obj;
|
|
40
|
+
return _obj;
|
|
41
|
+
}
|
|
42
|
+
return new Proxy({}, {
|
|
43
|
+
defineProperty(_target, property, attributes) {
|
|
44
|
+
const obj = getObj();
|
|
45
|
+
Object.defineProperty(obj, property, attributes);
|
|
46
|
+
return true;
|
|
47
|
+
},
|
|
48
|
+
deleteProperty(_target, p) {
|
|
49
|
+
const obj = getObj();
|
|
50
|
+
delete obj[p];
|
|
51
|
+
return true;
|
|
52
|
+
},
|
|
53
|
+
get(target, p) {
|
|
54
|
+
if (p === "then") return;
|
|
55
|
+
if (Object.prototype[p]) return target[p];
|
|
56
|
+
const obj = getObj();
|
|
57
|
+
const val = obj[p];
|
|
58
|
+
if (typeof val === "function") return val.bind(obj);
|
|
59
|
+
return val;
|
|
60
|
+
},
|
|
61
|
+
getOwnPropertyDescriptor(_target, p) {
|
|
62
|
+
const obj = getObj();
|
|
63
|
+
return Object.getOwnPropertyDescriptor(obj, p);
|
|
64
|
+
},
|
|
65
|
+
getPrototypeOf() {
|
|
66
|
+
const obj = getObj();
|
|
67
|
+
return Object.getPrototypeOf(obj);
|
|
68
|
+
},
|
|
69
|
+
has(_target, p) {
|
|
70
|
+
const obj = getObj();
|
|
71
|
+
return p in obj;
|
|
72
|
+
},
|
|
73
|
+
isExtensible() {
|
|
74
|
+
const obj = getObj();
|
|
75
|
+
return Object.isExtensible(obj);
|
|
76
|
+
},
|
|
77
|
+
ownKeys() {
|
|
78
|
+
const obj = getObj();
|
|
79
|
+
return Reflect.ownKeys(obj);
|
|
80
|
+
},
|
|
81
|
+
preventExtensions() {
|
|
82
|
+
const obj = getObj();
|
|
83
|
+
Object.preventExtensions(obj);
|
|
84
|
+
return true;
|
|
85
|
+
},
|
|
86
|
+
set(_target, p, newValue) {
|
|
87
|
+
const obj = getObj();
|
|
88
|
+
obj[p] = newValue;
|
|
89
|
+
return true;
|
|
90
|
+
},
|
|
91
|
+
setPrototypeOf(_target, v) {
|
|
92
|
+
const obj = getObj();
|
|
93
|
+
Object.setPrototypeOf(obj, v);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
static contextEggObjectProxy(proto, objName) {
|
|
99
|
+
const PROTO_OBJ_PROXY = Symbol(`EggPrototype#objProxy#${String(objName)}`);
|
|
100
|
+
if (!proto[PROTO_OBJ_PROXY]) proto[PROTO_OBJ_PROXY] = new Proxy({}, {
|
|
101
|
+
defineProperty(_target, property, attributes) {
|
|
102
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
103
|
+
Object.defineProperty(obj, property, attributes);
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
deleteProperty(_target, p) {
|
|
107
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
108
|
+
delete obj[p];
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
111
|
+
get(target, p) {
|
|
112
|
+
if (p === "then") return;
|
|
113
|
+
if (Object.prototype[p]) return target[p];
|
|
114
|
+
return EggContainerFactory.getEggObject(proto, objName).obj[p];
|
|
115
|
+
},
|
|
116
|
+
getOwnPropertyDescriptor(_target, p) {
|
|
117
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
118
|
+
return Object.getOwnPropertyDescriptor(obj, p);
|
|
119
|
+
},
|
|
120
|
+
getPrototypeOf() {
|
|
121
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
122
|
+
return Object.getPrototypeOf(obj);
|
|
123
|
+
},
|
|
124
|
+
has(_target, p) {
|
|
125
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
126
|
+
return p in obj;
|
|
127
|
+
},
|
|
128
|
+
isExtensible() {
|
|
129
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
130
|
+
return Object.isExtensible(obj);
|
|
131
|
+
},
|
|
132
|
+
ownKeys() {
|
|
133
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
134
|
+
return Reflect.ownKeys(obj);
|
|
135
|
+
},
|
|
136
|
+
preventExtensions() {
|
|
137
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
138
|
+
Object.preventExtensions(obj);
|
|
139
|
+
return true;
|
|
140
|
+
},
|
|
141
|
+
set(_target, p, newValue) {
|
|
142
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
143
|
+
obj[p] = newValue;
|
|
144
|
+
return true;
|
|
145
|
+
},
|
|
146
|
+
setPrototypeOf(_target, v) {
|
|
147
|
+
const obj = EggContainerFactory.getEggObject(proto, objName).obj;
|
|
148
|
+
Object.setPrototypeOf(obj, v);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
return proto[PROTO_OBJ_PROXY];
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/impl/EggObjectImpl.ts
|
|
158
|
+
var EggObjectImpl = class EggObjectImpl {
|
|
159
|
+
_obj;
|
|
160
|
+
status = EggObjectStatus.PENDING;
|
|
161
|
+
proto;
|
|
162
|
+
name;
|
|
163
|
+
id;
|
|
164
|
+
constructor(name, proto) {
|
|
165
|
+
this.name = name;
|
|
166
|
+
this.proto = proto;
|
|
167
|
+
const ctx = ContextHandler.getContext();
|
|
168
|
+
this.id = IdenticalUtil.createObjectId(this.proto.id, ctx?.id);
|
|
169
|
+
}
|
|
170
|
+
async initWithInjectProperty(ctx) {
|
|
171
|
+
try {
|
|
172
|
+
this._obj = this.proto.constructEggObject();
|
|
173
|
+
const objLifecycleHook = this._obj;
|
|
174
|
+
await EggObjectLifecycleUtil.objectPreCreate(ctx, this);
|
|
175
|
+
const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook("postConstruct", this.proto) ?? "postConstruct";
|
|
176
|
+
if (objLifecycleHook[postConstructMethod]) await objLifecycleHook[postConstructMethod](ctx, this);
|
|
177
|
+
const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("preInject", this.proto) ?? "preInject";
|
|
178
|
+
if (objLifecycleHook[preInjectMethod]) await objLifecycleHook[preInjectMethod](ctx, this);
|
|
179
|
+
await Promise.all(this.proto.injectObjects.map(async (injectObject) => {
|
|
180
|
+
const proto = injectObject.proto;
|
|
181
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
182
|
+
if (this.proto.initType !== ObjectInitType.CONTEXT && injectObject.proto.initType === ObjectInitType.CONTEXT) this.injectProperty(injectObject.refName, EggObjectUtil.contextEggObjectGetProperty(proto, injectObject.objName));
|
|
183
|
+
else {
|
|
184
|
+
const injectObj = await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
185
|
+
this.injectProperty(injectObject.refName, EggObjectUtil.eggObjectGetProperty(injectObj));
|
|
186
|
+
}
|
|
187
|
+
}));
|
|
188
|
+
await EggObjectLifecycleUtil.objectPostCreate(ctx, this);
|
|
189
|
+
const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("postInject", this.proto) ?? "postInject";
|
|
190
|
+
if (objLifecycleHook[postInjectMethod]) await objLifecycleHook[postInjectMethod](ctx, this);
|
|
191
|
+
const initMethod = EggObjectLifecycleUtil.getLifecycleHook("init", this.proto) ?? "init";
|
|
192
|
+
if (objLifecycleHook[initMethod]) await objLifecycleHook[initMethod](ctx, this);
|
|
193
|
+
this.status = EggObjectStatus.READY;
|
|
194
|
+
} catch (e) {
|
|
195
|
+
this.status = EggObjectStatus.ERROR;
|
|
196
|
+
throw e;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
async initWithInjectConstructor(ctx) {
|
|
200
|
+
try {
|
|
201
|
+
const constructArgs = await Promise.all(this.proto.injectObjects.map(async (injectObject) => {
|
|
202
|
+
const proto = injectObject.proto;
|
|
203
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
204
|
+
if (this.proto.initType !== ObjectInitType.CONTEXT && injectObject.proto.initType === ObjectInitType.CONTEXT) return EggObjectUtil.contextEggObjectProxy(proto, injectObject.objName);
|
|
205
|
+
const injectObj = await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
206
|
+
return EggObjectUtil.eggObjectProxy(injectObj);
|
|
207
|
+
}));
|
|
208
|
+
if (typeof this.proto.multiInstanceConstructorIndex !== "undefined") {
|
|
209
|
+
const qualifiers = this.proto.multiInstanceConstructorAttributes?.map((t) => {
|
|
210
|
+
return {
|
|
211
|
+
attribute: t,
|
|
212
|
+
value: this.proto.getQualifier(t)
|
|
213
|
+
};
|
|
214
|
+
})?.filter((t) => typeof t.value !== "undefined") ?? [];
|
|
215
|
+
const objInfo = {
|
|
216
|
+
name: this.proto.name,
|
|
217
|
+
qualifiers
|
|
218
|
+
};
|
|
219
|
+
constructArgs.splice(this.proto.multiInstanceConstructorIndex, 0, objInfo);
|
|
220
|
+
}
|
|
221
|
+
this._obj = this.proto.constructEggObject(...constructArgs);
|
|
222
|
+
const objLifecycleHook = this._obj;
|
|
223
|
+
await EggObjectLifecycleUtil.objectPreCreate(ctx, this);
|
|
224
|
+
const postConstructMethod = EggObjectLifecycleUtil.getLifecycleHook("postConstruct", this.proto) ?? "postConstruct";
|
|
225
|
+
if (objLifecycleHook[postConstructMethod]) await objLifecycleHook[postConstructMethod](ctx, this);
|
|
226
|
+
const preInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("preInject", this.proto) ?? "preInject";
|
|
227
|
+
if (objLifecycleHook[preInjectMethod]) await objLifecycleHook[preInjectMethod](ctx, this);
|
|
228
|
+
await EggObjectLifecycleUtil.objectPostCreate(ctx, this);
|
|
229
|
+
const postInjectMethod = EggObjectLifecycleUtil.getLifecycleHook("postInject", this.proto) ?? "postInject";
|
|
230
|
+
if (objLifecycleHook[postInjectMethod]) await objLifecycleHook[postInjectMethod](ctx, this);
|
|
231
|
+
const initMethod = EggObjectLifecycleUtil.getLifecycleHook("init", this.proto) ?? "init";
|
|
232
|
+
if (objLifecycleHook[initMethod]) await objLifecycleHook[initMethod](ctx, this);
|
|
233
|
+
this.status = EggObjectStatus.READY;
|
|
234
|
+
} catch (e) {
|
|
235
|
+
this.status = EggObjectStatus.ERROR;
|
|
236
|
+
throw e;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
async init(ctx) {
|
|
240
|
+
if (this.proto.injectType === InjectType.CONSTRUCTOR) await this.initWithInjectConstructor(ctx);
|
|
241
|
+
else await this.initWithInjectProperty(ctx);
|
|
242
|
+
}
|
|
243
|
+
async destroy(ctx) {
|
|
244
|
+
if (this.status === EggObjectStatus.READY) {
|
|
245
|
+
this.status = EggObjectStatus.DESTROYING;
|
|
246
|
+
await EggObjectLifecycleUtil.objectPreDestroy(ctx, this);
|
|
247
|
+
const objLifecycleHook = this._obj;
|
|
248
|
+
const preDestroyMethod = EggObjectLifecycleUtil.getLifecycleHook("preDestroy", this.proto) ?? "preDestroy";
|
|
249
|
+
if (objLifecycleHook[preDestroyMethod]) await objLifecycleHook[preDestroyMethod](ctx, this);
|
|
250
|
+
const destroyMethod = EggObjectLifecycleUtil.getLifecycleHook("destroy", this.proto) ?? "destroy";
|
|
251
|
+
if (objLifecycleHook[destroyMethod]) await objLifecycleHook[destroyMethod](ctx, this);
|
|
252
|
+
this.status = EggObjectStatus.DESTROYED;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
injectProperty(name, descriptor) {
|
|
256
|
+
Reflect.defineProperty(this._obj, name, descriptor);
|
|
257
|
+
}
|
|
258
|
+
get obj() {
|
|
259
|
+
return this._obj;
|
|
260
|
+
}
|
|
261
|
+
get isReady() {
|
|
262
|
+
return this.status === EggObjectStatus.READY;
|
|
263
|
+
}
|
|
264
|
+
static async createObject(name, proto, lifecycleContext) {
|
|
265
|
+
const obj = new EggObjectImpl(name, proto);
|
|
266
|
+
await obj.init(lifecycleContext);
|
|
267
|
+
return obj;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/model/LoadUnitInstance.ts
|
|
273
|
+
const LoadUnitInstanceLifecycleUtil = new LifecycleUtil();
|
|
274
|
+
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/factory/LoadUnitInstanceFactory.ts
|
|
277
|
+
var LoadUnitInstanceFactory = class {
|
|
278
|
+
static creatorMap = /* @__PURE__ */ new Map();
|
|
279
|
+
static instanceMap = /* @__PURE__ */ new Map();
|
|
280
|
+
static registerLoadUnitInstanceClass(type, creator) {
|
|
281
|
+
this.creatorMap.set(type, creator);
|
|
282
|
+
}
|
|
283
|
+
static async createLoadUnitInstance(loadUnit) {
|
|
284
|
+
const creator = this.creatorMap.get(loadUnit.type);
|
|
285
|
+
if (!creator) throw new Error(`load unit instance type ${loadUnit.type} is not implement`);
|
|
286
|
+
const instanceId = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
287
|
+
if (!this.instanceMap.has(instanceId)) {
|
|
288
|
+
const ctx = { loadUnit };
|
|
289
|
+
const instance = creator(ctx);
|
|
290
|
+
this.instanceMap.set(instanceId, {
|
|
291
|
+
instance,
|
|
292
|
+
ctx
|
|
293
|
+
});
|
|
294
|
+
if (instance.init) await instance.init(ctx);
|
|
295
|
+
}
|
|
296
|
+
return this.instanceMap.get(instanceId).instance;
|
|
297
|
+
}
|
|
298
|
+
static getLoadUnitInstance(loadUnit) {
|
|
299
|
+
const instanceId = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
300
|
+
return this.instanceMap.get(instanceId)?.instance;
|
|
301
|
+
}
|
|
302
|
+
static async destroyLoadUnitInstance(loadUnitInstance) {
|
|
303
|
+
const { ctx } = this.instanceMap.get(loadUnitInstance.id);
|
|
304
|
+
await LoadUnitInstanceLifecycleUtil.objectPreDestroy(ctx, loadUnitInstance);
|
|
305
|
+
if (loadUnitInstance.destroy) await loadUnitInstance.destroy(ctx);
|
|
306
|
+
this.instanceMap.delete(loadUnitInstance.id);
|
|
307
|
+
LoadUnitInstanceLifecycleUtil.clearObjectLifecycle(loadUnitInstance);
|
|
308
|
+
}
|
|
309
|
+
static getLoadUnitInstanceByProto(proto) {
|
|
310
|
+
for (const { instance } of this.instanceMap.values()) if (instance.loadUnit.containPrototype(proto)) return instance;
|
|
311
|
+
throw new Error(`not found load unit for proto ${proto.id}`);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.SINGLETON, (proto) => {
|
|
315
|
+
return LoadUnitInstanceFactory.getLoadUnitInstanceByProto(proto);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/factory/EggObjectFactory.ts
|
|
320
|
+
var EggObjectFactory = class {
|
|
321
|
+
static eggObjectMap = /* @__PURE__ */ new Map();
|
|
322
|
+
static eggObjectCreateMap = /* @__PURE__ */ new Map();
|
|
323
|
+
static registerEggObjectCreateMethod(protoClass, method) {
|
|
324
|
+
this.eggObjectCreateMap.set(protoClass, method);
|
|
325
|
+
}
|
|
326
|
+
static getEggObjectCreateMethod(protoClass) {
|
|
327
|
+
if (this.eggObjectCreateMap.has(protoClass)) return this.eggObjectCreateMap.get(protoClass);
|
|
328
|
+
return EggObjectImpl.createObject;
|
|
329
|
+
}
|
|
330
|
+
static async createObject(name, proto) {
|
|
331
|
+
const loadUnit = LoadUnitFactory.getLoadUnitById(proto.loadUnitId);
|
|
332
|
+
if (!loadUnit) throw new Error(`not found load unit ${proto.loadUnitId}`);
|
|
333
|
+
const loadUnitInstance = LoadUnitInstanceFactory.getLoadUnitInstance(loadUnit);
|
|
334
|
+
const lifecycleContext = {
|
|
335
|
+
loadUnit,
|
|
336
|
+
loadUnitInstance
|
|
337
|
+
};
|
|
338
|
+
const method = this.getEggObjectCreateMethod(proto.constructor);
|
|
339
|
+
const args = [
|
|
340
|
+
name,
|
|
341
|
+
proto,
|
|
342
|
+
lifecycleContext
|
|
343
|
+
];
|
|
344
|
+
const obj = await Reflect.apply(method, null, args);
|
|
345
|
+
this.eggObjectMap.set(obj.id, {
|
|
346
|
+
obj,
|
|
347
|
+
ctx: lifecycleContext
|
|
348
|
+
});
|
|
349
|
+
return obj;
|
|
350
|
+
}
|
|
351
|
+
static async destroyObject(obj) {
|
|
352
|
+
const { ctx } = this.eggObjectMap.get(obj.id);
|
|
353
|
+
try {
|
|
354
|
+
if (obj.destroy) await obj.destroy(ctx);
|
|
355
|
+
} finally {
|
|
356
|
+
this.eggObjectMap.delete(obj.id);
|
|
357
|
+
EggObjectLifecycleUtil.clearObjectLifecycle(obj);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/model/EggContext.ts
|
|
364
|
+
const EggContextLifecycleUtil = new LifecycleUtil();
|
|
365
|
+
|
|
366
|
+
//#endregion
|
|
367
|
+
//#region src/model/AbstractEggContext.ts
|
|
368
|
+
const debug = debuglog("tegg/core/runtime/AbstractEggContext");
|
|
369
|
+
var AbstractEggContext = class {
|
|
370
|
+
contextData = /* @__PURE__ */ new Map();
|
|
371
|
+
protoToCreate = /* @__PURE__ */ new Map();
|
|
372
|
+
eggObjectMap = /* @__PURE__ */ new Map();
|
|
373
|
+
eggObjectPromiseMap = /* @__PURE__ */ new Map();
|
|
374
|
+
destroyed = false;
|
|
375
|
+
addProtoToCreate(name, proto) {
|
|
376
|
+
this.protoToCreate.set(name, proto);
|
|
377
|
+
}
|
|
378
|
+
deleteProtoToCreate(name) {
|
|
379
|
+
this.protoToCreate.delete(name);
|
|
380
|
+
}
|
|
381
|
+
async destroy(ctx) {
|
|
382
|
+
await EggContextLifecycleUtil.objectPreDestroy(ctx, this);
|
|
383
|
+
const objs = [];
|
|
384
|
+
for (const protoObjMap of this.eggObjectMap.values()) for (const protoObj of protoObjMap.values()) objs.push(protoObj);
|
|
385
|
+
this.eggObjectMap.clear();
|
|
386
|
+
await Promise.all(objs.map(async (obj) => {
|
|
387
|
+
await EggObjectFactory.destroyObject(obj);
|
|
388
|
+
}));
|
|
389
|
+
this.contextData.clear();
|
|
390
|
+
this.destroyed = true;
|
|
391
|
+
await EggContextLifecycleUtil.clearObjectLifecycle(this);
|
|
392
|
+
}
|
|
393
|
+
get(key) {
|
|
394
|
+
return this.contextData.get(key);
|
|
395
|
+
}
|
|
396
|
+
getEggObject(name, proto) {
|
|
397
|
+
if (this.destroyed) throw TeggError.create(`Can not read property \`${String(name)}\` because egg ctx has been destroyed`, "read_after_ctx_destroyed");
|
|
398
|
+
const protoObjMap = this.eggObjectMap.get(proto.id);
|
|
399
|
+
if (!protoObjMap || !protoObjMap.has(name)) throw new Error(`EggObject ${String(proto.name)} not found`);
|
|
400
|
+
return protoObjMap.get(name);
|
|
401
|
+
}
|
|
402
|
+
async getOrCreateEggObject(name, proto) {
|
|
403
|
+
const protoObjMap = MapUtil.getOrStore(this.eggObjectMap, proto.id, /* @__PURE__ */ new Map());
|
|
404
|
+
if (!protoObjMap.has(name)) {
|
|
405
|
+
const protoObjPromiseMap = MapUtil.getOrStore(this.eggObjectPromiseMap, proto.id, /* @__PURE__ */ new Map());
|
|
406
|
+
if (!protoObjPromiseMap.has(name)) {
|
|
407
|
+
const objPromise = EggObjectFactory.createObject(name, proto);
|
|
408
|
+
protoObjPromiseMap.set(name, objPromise);
|
|
409
|
+
const obj = await objPromise;
|
|
410
|
+
protoObjPromiseMap.delete(name);
|
|
411
|
+
if (!protoObjPromiseMap.size) this.eggObjectPromiseMap.delete(proto.id);
|
|
412
|
+
protoObjMap.set(name, obj);
|
|
413
|
+
} else await protoObjPromiseMap.get(name);
|
|
414
|
+
}
|
|
415
|
+
return protoObjMap.get(name);
|
|
416
|
+
}
|
|
417
|
+
async init(ctx) {
|
|
418
|
+
await EggContextLifecycleUtil.objectPreCreate(ctx, this);
|
|
419
|
+
for (const [name, proto] of this.protoToCreate) await this.getOrCreateEggObject(name, proto);
|
|
420
|
+
await EggContextLifecycleUtil.objectPostCreate(ctx, this);
|
|
421
|
+
}
|
|
422
|
+
iterateProtoToCreate() {
|
|
423
|
+
return this.protoToCreate.entries();
|
|
424
|
+
}
|
|
425
|
+
set(key, val) {
|
|
426
|
+
this.contextData.set(key, val);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.CONTEXT, () => {
|
|
430
|
+
const ctx = ContextHandler.getContext();
|
|
431
|
+
if (!ctx) {
|
|
432
|
+
debug("can not get teggCtx from ContextHandler");
|
|
433
|
+
throw new Error("ctx is required");
|
|
434
|
+
}
|
|
435
|
+
return ctx;
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
//#endregion
|
|
439
|
+
//#region src/impl/EggAlwaysNewObjectContainer.ts
|
|
440
|
+
var EggAlwaysNewObjectContainer = class EggAlwaysNewObjectContainer {
|
|
441
|
+
static instance = new EggAlwaysNewObjectContainer();
|
|
442
|
+
id;
|
|
443
|
+
constructor() {
|
|
444
|
+
this.id = "ALWAYS_NEW_OBJECT_CONTAINER";
|
|
445
|
+
}
|
|
446
|
+
addProtoToCreate() {}
|
|
447
|
+
deleteProtoToCreate() {}
|
|
448
|
+
iterateProtoToCreate() {
|
|
449
|
+
return (/* @__PURE__ */ new Map()).entries();
|
|
450
|
+
}
|
|
451
|
+
getEggObject() {
|
|
452
|
+
throw new Error("Always Object can not use getEggObject, should use getOrCreateEggObject");
|
|
453
|
+
}
|
|
454
|
+
async getOrCreateEggObject(name, proto) {
|
|
455
|
+
return EggObjectFactory.createObject(name, proto);
|
|
456
|
+
}
|
|
457
|
+
async destroy() {}
|
|
458
|
+
async init() {}
|
|
459
|
+
};
|
|
460
|
+
EggContainerFactory.registerContainerGetMethod(ObjectInitType.ALWAYS_NEW, () => EggAlwaysNewObjectContainer.instance);
|
|
461
|
+
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/impl/ModuleLoadUnitInstance.ts
|
|
464
|
+
var ModuleLoadUnitInstance = class ModuleLoadUnitInstance {
|
|
465
|
+
loadUnit;
|
|
466
|
+
id;
|
|
467
|
+
name;
|
|
468
|
+
protoToCreateMap = [];
|
|
469
|
+
eggObjectMap = /* @__PURE__ */ new Map();
|
|
470
|
+
eggObjectPromiseMap = /* @__PURE__ */ new Map();
|
|
471
|
+
constructor(loadUnit) {
|
|
472
|
+
this.loadUnit = loadUnit;
|
|
473
|
+
this.name = loadUnit.name;
|
|
474
|
+
const iterator = this.loadUnit.iterateEggPrototype();
|
|
475
|
+
for (const proto of iterator) if (proto.initType === ObjectInitType.SINGLETON) this.protoToCreateMap.push([proto.name, proto]);
|
|
476
|
+
this.id = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
477
|
+
}
|
|
478
|
+
iterateProtoToCreate() {
|
|
479
|
+
return this.protoToCreateMap[Symbol.iterator]();
|
|
480
|
+
}
|
|
481
|
+
addProtoToCreate(name, proto) {
|
|
482
|
+
this.protoToCreateMap.push([name, proto]);
|
|
483
|
+
}
|
|
484
|
+
deleteProtoToCreate(name) {
|
|
485
|
+
const index = this.protoToCreateMap.findIndex(([protoName]) => protoName === name);
|
|
486
|
+
if (index !== -1) this.protoToCreateMap.splice(index, 1);
|
|
487
|
+
}
|
|
488
|
+
async init(ctx) {
|
|
489
|
+
await LoadUnitInstanceLifecycleUtil.objectPreCreate(ctx, this);
|
|
490
|
+
for (const [name, proto] of this.protoToCreateMap) await this.getOrCreateEggObject(name, proto);
|
|
491
|
+
await LoadUnitInstanceLifecycleUtil.objectPostCreate(ctx, this);
|
|
492
|
+
}
|
|
493
|
+
async destroy() {
|
|
494
|
+
const objs = [];
|
|
495
|
+
for (const protoObjMap of this.eggObjectMap.values()) for (const obj of protoObjMap.values()) objs.push(obj);
|
|
496
|
+
this.eggObjectMap.clear();
|
|
497
|
+
await Promise.all(objs.map(async (obj) => {
|
|
498
|
+
await EggObjectFactory.destroyObject(obj);
|
|
499
|
+
}));
|
|
500
|
+
}
|
|
501
|
+
async getOrCreateEggObject(name, proto) {
|
|
502
|
+
if (!this.loadUnit.containPrototype(proto)) throw new Error("load unit not contain proto");
|
|
503
|
+
const protoObjMap = MapUtil.getOrStore(this.eggObjectMap, proto.id, /* @__PURE__ */ new Map());
|
|
504
|
+
if (!protoObjMap.has(name)) {
|
|
505
|
+
const protoObjPromiseMap = MapUtil.getOrStore(this.eggObjectPromiseMap, proto.id, /* @__PURE__ */ new Map());
|
|
506
|
+
if (!protoObjPromiseMap.has(name)) {
|
|
507
|
+
const objPromise = EggObjectFactory.createObject(name, proto);
|
|
508
|
+
protoObjPromiseMap.set(name, objPromise);
|
|
509
|
+
const obj = await objPromise;
|
|
510
|
+
protoObjPromiseMap.delete(name);
|
|
511
|
+
protoObjMap.set(name, obj);
|
|
512
|
+
} else await protoObjPromiseMap.get(name);
|
|
513
|
+
}
|
|
514
|
+
return protoObjMap.get(name);
|
|
515
|
+
}
|
|
516
|
+
getEggObject(name, proto) {
|
|
517
|
+
const protoObjMap = this.eggObjectMap.get(proto.id);
|
|
518
|
+
if (!protoObjMap || !protoObjMap.has(name)) throw new Error(`EggObject ${String(proto.name)} not found`);
|
|
519
|
+
return protoObjMap.get(name);
|
|
520
|
+
}
|
|
521
|
+
static createModuleLoadUnitInstance(ctx) {
|
|
522
|
+
return new ModuleLoadUnitInstance(ctx.loadUnit);
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
LoadUnitInstanceFactory.registerLoadUnitInstanceClass(EggLoadUnitType.MODULE, ModuleLoadUnitInstance.createModuleLoadUnitInstance);
|
|
526
|
+
|
|
527
|
+
//#endregion
|
|
21
528
|
export { AbstractEggContext, ContextHandler, ContextInitiator, ContextObjectGraph, EggAlwaysNewObjectContainer, EggContainerFactory, EggContextLifecycleUtil, EggObjectFactory, EggObjectImpl, EggObjectLifecycleUtil, EggObjectUtil, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil, ModuleLoadUnitInstance };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/tegg-runtime",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.8",
|
|
4
4
|
"description": "tegg runtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -31,11 +31,11 @@
|
|
|
31
31
|
"node": ">=22.18.0"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@eggjs/core-decorator": "4.0.0-beta.
|
|
35
|
-
"@eggjs/tegg-common-util": "4.0.0-beta.
|
|
36
|
-
"@eggjs/tegg-lifecycle": "4.0.0-beta.
|
|
37
|
-
"@eggjs/tegg-
|
|
38
|
-
"@eggjs/tegg-
|
|
34
|
+
"@eggjs/core-decorator": "4.0.0-beta.8",
|
|
35
|
+
"@eggjs/tegg-common-util": "4.0.0-beta.8",
|
|
36
|
+
"@eggjs/tegg-lifecycle": "4.0.0-beta.8",
|
|
37
|
+
"@eggjs/tegg-metadata": "4.0.0-beta.8",
|
|
38
|
+
"@eggjs/tegg-types": "4.0.0-beta.8"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^22.10.5",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"tsdown": "^0.15.6",
|
|
44
44
|
"unplugin-unused": "^0.5.3",
|
|
45
45
|
"@eggjs/module-test-util": "4.0.0-beta.4",
|
|
46
|
-
"@eggjs/tegg-loader": "4.0.0-beta.
|
|
46
|
+
"@eggjs/tegg-loader": "4.0.0-beta.8"
|
|
47
47
|
},
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
|
@@ -1,30 +0,0 @@
|
|
|
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,17 +0,0 @@
|
|
|
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 };
|
|
@@ -1,50 +0,0 @@
|
|
|
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/tegg-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 loadUnitInstance = LoadUnitInstanceFactory.getLoadUnitInstance(loadUnit);
|
|
21
|
-
const lifecycleContext = {
|
|
22
|
-
loadUnit,
|
|
23
|
-
loadUnitInstance
|
|
24
|
-
};
|
|
25
|
-
const method = this.getEggObjectCreateMethod(proto.constructor);
|
|
26
|
-
const args = [
|
|
27
|
-
name,
|
|
28
|
-
proto,
|
|
29
|
-
lifecycleContext
|
|
30
|
-
];
|
|
31
|
-
const obj = await Reflect.apply(method, null, args);
|
|
32
|
-
this.eggObjectMap.set(obj.id, {
|
|
33
|
-
obj,
|
|
34
|
-
ctx: lifecycleContext
|
|
35
|
-
});
|
|
36
|
-
return obj;
|
|
37
|
-
}
|
|
38
|
-
static async destroyObject(obj) {
|
|
39
|
-
const { ctx } = this.eggObjectMap.get(obj.id);
|
|
40
|
-
try {
|
|
41
|
-
if (obj.destroy) await obj.destroy(ctx);
|
|
42
|
-
} finally {
|
|
43
|
-
this.eggObjectMap.delete(obj.id);
|
|
44
|
-
EggObjectLifecycleUtil.clearObjectLifecycle(obj);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
//#endregion
|
|
50
|
-
export { EggObjectFactory };
|