@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,10 +1,25 @@
|
|
|
1
|
-
import { ContextHandler } from "../model/ContextHandler.js";
|
|
2
|
-
import "../model/index.js";
|
|
3
1
|
import { debuglog } from "node:util";
|
|
4
|
-
import {
|
|
2
|
+
import { ObjectInitType } from "@eggjs/tegg-types";
|
|
3
|
+
import { EggPrototypeFactory, LoadUnitFactory } from "@eggjs/tegg-metadata";
|
|
5
4
|
import { NameUtil } from "@eggjs/tegg-common-util";
|
|
6
5
|
import { PrototypeUtil } from "@eggjs/core-decorator";
|
|
6
|
+
import assert from "node:assert";
|
|
7
7
|
|
|
8
|
+
//#region src/model/ContextHandler.ts
|
|
9
|
+
var ContextHandler = class {
|
|
10
|
+
static getContextCallback;
|
|
11
|
+
static runInContextCallback;
|
|
12
|
+
static getContext() {
|
|
13
|
+
assert(this.getContextCallback, "getContextCallback not set");
|
|
14
|
+
return this.getContextCallback ? this.getContextCallback() : void 0;
|
|
15
|
+
}
|
|
16
|
+
static run(context, fn) {
|
|
17
|
+
assert(this.runInContextCallback, "runInContextCallback not set");
|
|
18
|
+
return this.runInContextCallback(context, fn);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
8
23
|
//#region src/factory/EggContainerFactory.ts
|
|
9
24
|
const debug = debuglog("tegg/core/runtime/EggContainerFactory");
|
|
10
25
|
var EggContainerFactory = class EggContainerFactory {
|
|
@@ -31,8 +46,9 @@ var EggContainerFactory = class EggContainerFactory {
|
|
|
31
46
|
const ctx = ContextHandler.getContext();
|
|
32
47
|
if (ctx) {
|
|
33
48
|
if (!EggContainerFactory.ContextInitiatorClass) {
|
|
34
|
-
const { ContextInitiator } = await import("
|
|
35
|
-
|
|
49
|
+
const { ContextInitiator: ContextInitiator$1 } = await import("./ContextInitiator-GmZYvMSm.js");
|
|
50
|
+
debug("import ContextInitiator to fix dependency cycle");
|
|
51
|
+
EggContainerFactory.ContextInitiatorClass = ContextInitiator$1;
|
|
36
52
|
}
|
|
37
53
|
await EggContainerFactory.ContextInitiatorClass.createContextInitiator(ctx).init(obj);
|
|
38
54
|
debug("getOrCreateEggObject with context eggObject:%o, from proto:%o, name:%s", obj.name, proto.name, name);
|
|
@@ -77,4 +93,68 @@ var EggContainerFactory = class EggContainerFactory {
|
|
|
77
93
|
};
|
|
78
94
|
|
|
79
95
|
//#endregion
|
|
80
|
-
|
|
96
|
+
//#region src/impl/ContextObjectGraph.ts
|
|
97
|
+
var InjectProtoHolder = class {
|
|
98
|
+
idSet = /* @__PURE__ */ new Set();
|
|
99
|
+
injectProtos = [];
|
|
100
|
+
addInjectProto(injectObjectProto) {
|
|
101
|
+
const id = `${String(injectObjectProto.objName)}:${injectObjectProto.proto.id}`;
|
|
102
|
+
if (this.idSet.has(id)) return;
|
|
103
|
+
this.idSet.add(id);
|
|
104
|
+
this.injectProtos.push(injectObjectProto);
|
|
105
|
+
}
|
|
106
|
+
dumpProtos() {
|
|
107
|
+
return this.injectProtos;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var ContextObjectGraph = class ContextObjectGraph {
|
|
111
|
+
static eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
112
|
+
static getContextProto(proto) {
|
|
113
|
+
if (ContextObjectGraph.eggObjectInitRecorder.has(proto)) return ContextObjectGraph.eggObjectInitRecorder.get(proto);
|
|
114
|
+
const holder = new InjectProtoHolder();
|
|
115
|
+
this.doGetContextProto(proto, holder);
|
|
116
|
+
const injectObjectProtos = holder.dumpProtos();
|
|
117
|
+
ContextObjectGraph.eggObjectInitRecorder.set(proto, injectObjectProtos);
|
|
118
|
+
return injectObjectProtos;
|
|
119
|
+
}
|
|
120
|
+
static doGetContextProto(proto, holder) {
|
|
121
|
+
for (const injectObject of proto.injectObjects) {
|
|
122
|
+
if (injectObject.proto.initType === ObjectInitType.CONTEXT && proto.initType !== ObjectInitType.CONTEXT) holder.addInjectProto(injectObject);
|
|
123
|
+
ContextObjectGraph.doGetContextProto(injectObject.proto, holder);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/impl/ContextInitiator.ts
|
|
130
|
+
const CONTEXT_INITIATOR = Symbol("EggContext#ContextInitiator");
|
|
131
|
+
var ContextInitiator = class ContextInitiator {
|
|
132
|
+
eggContext;
|
|
133
|
+
eggObjectInitRecorder;
|
|
134
|
+
constructor(eggContext) {
|
|
135
|
+
this.eggContext = eggContext;
|
|
136
|
+
this.eggObjectInitRecorder = /* @__PURE__ */ new WeakMap();
|
|
137
|
+
this.eggContext.set(CONTEXT_INITIATOR, this);
|
|
138
|
+
}
|
|
139
|
+
async init(obj) {
|
|
140
|
+
if (this.eggObjectInitRecorder.get(obj) === true) return;
|
|
141
|
+
this.eggObjectInitRecorder.set(obj, true);
|
|
142
|
+
const injectObjectProtos = ContextObjectGraph.getContextProto(obj.proto);
|
|
143
|
+
await Promise.all(injectObjectProtos.map(async (injectObject) => {
|
|
144
|
+
const proto = injectObject.proto;
|
|
145
|
+
if (!LoadUnitFactory.getLoadUnitById(proto.loadUnitId)) throw new Error(`can not find load unit: ${proto.loadUnitId}`);
|
|
146
|
+
await EggContainerFactory.getOrCreateEggObject(proto, injectObject.objName);
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
static createContextInitiator(context) {
|
|
150
|
+
let initiator = context.get(CONTEXT_INITIATOR);
|
|
151
|
+
if (!initiator) {
|
|
152
|
+
initiator = new ContextInitiator(context);
|
|
153
|
+
context.set(CONTEXT_INITIATOR, initiator);
|
|
154
|
+
}
|
|
155
|
+
return initiator;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
//#endregion
|
|
160
|
+
export { ContextHandler, ContextInitiator, ContextObjectGraph, EggContainerFactory };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,171 @@
|
|
|
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
1
|
import { EggRuntimeContext as EggContext } from "@eggjs/tegg-types/runtime";
|
|
2
|
+
import { ContainerGetMethod, CreateObjectMethod, EggContainer, EggContextLifecycleContext, EggLoadUnitTypeLike, EggObject, EggObjectLifeCycleContext, EggObjectName, EggProtoImplClass, EggPrototype, EggPrototypeClass, EggPrototypeName, EggRuntimeContext, Id, InjectObjectProto, LifecycleContext, LoadUnit, LoadUnitInstance, LoadUnitInstanceLifecycleContext, ObjectInitTypeLike, QualifierInfo } from "@eggjs/tegg-types";
|
|
3
|
+
import { LifecycleUtil } from "@eggjs/tegg-lifecycle";
|
|
16
4
|
export * from "@eggjs/tegg-types/runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/model/AbstractEggContext.d.ts
|
|
7
|
+
declare abstract class AbstractEggContext implements EggRuntimeContext {
|
|
8
|
+
private contextData;
|
|
9
|
+
private protoToCreate;
|
|
10
|
+
private eggObjectMap;
|
|
11
|
+
private eggObjectPromiseMap;
|
|
12
|
+
private destroyed;
|
|
13
|
+
abstract id: string;
|
|
14
|
+
addProtoToCreate(name: string, proto: EggPrototype): void;
|
|
15
|
+
deleteProtoToCreate(name: string): void;
|
|
16
|
+
destroy(ctx: EggContextLifecycleContext): Promise<void>;
|
|
17
|
+
get(key: string | symbol): any | undefined;
|
|
18
|
+
getEggObject(name: EggPrototypeName, proto: EggPrototype): EggObject;
|
|
19
|
+
getOrCreateEggObject(name: EggPrototypeName, proto: EggPrototype): Promise<EggObject>;
|
|
20
|
+
init(ctx: EggContextLifecycleContext): Promise<void>;
|
|
21
|
+
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
22
|
+
set(key: string | symbol, val: any): void;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/model/ContextHandler.d.ts
|
|
26
|
+
type runInContextCallback<R = any> = (context: EggRuntimeContext, fn: () => Promise<R>) => Promise<R>;
|
|
27
|
+
declare class ContextHandler {
|
|
28
|
+
static getContextCallback: () => EggRuntimeContext | undefined;
|
|
29
|
+
static runInContextCallback: runInContextCallback;
|
|
30
|
+
static getContext(): EggRuntimeContext | undefined;
|
|
31
|
+
static run<R = any>(context: EggRuntimeContext, fn: () => Promise<R>): Promise<R>;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/model/EggContext.d.ts
|
|
35
|
+
declare const EggContextLifecycleUtil: LifecycleUtil<EggContextLifecycleContext, EggRuntimeContext>;
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/model/EggObject.d.ts
|
|
38
|
+
declare const EggObjectLifecycleUtil: LifecycleUtil<EggObjectLifeCycleContext, EggObject>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/model/LoadUnitInstance.d.ts
|
|
41
|
+
declare const LoadUnitInstanceLifecycleUtil: LifecycleUtil<LoadUnitInstanceLifecycleContext, LoadUnitInstance>;
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/impl/ContextInitiator.d.ts
|
|
44
|
+
declare class ContextInitiator {
|
|
45
|
+
private readonly eggContext;
|
|
46
|
+
private readonly eggObjectInitRecorder;
|
|
47
|
+
constructor(eggContext: EggRuntimeContext);
|
|
48
|
+
init(obj: EggObject): Promise<void>;
|
|
49
|
+
static createContextInitiator(context: EggRuntimeContext): ContextInitiator;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/impl/ContextObjectGraph.d.ts
|
|
53
|
+
declare class ContextObjectGraph {
|
|
54
|
+
private static eggObjectInitRecorder;
|
|
55
|
+
static getContextProto(proto: EggPrototype): InjectObjectProto[];
|
|
56
|
+
private static doGetContextProto;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/impl/EggAlwaysNewObjectContainer.d.ts
|
|
60
|
+
declare class EggAlwaysNewObjectContainer implements EggContainer<LifecycleContext> {
|
|
61
|
+
static instance: EggAlwaysNewObjectContainer;
|
|
62
|
+
readonly id: Id;
|
|
63
|
+
constructor();
|
|
64
|
+
addProtoToCreate(): void;
|
|
65
|
+
deleteProtoToCreate(): void;
|
|
66
|
+
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
67
|
+
getEggObject(): EggObject;
|
|
68
|
+
getOrCreateEggObject(name: string, proto: EggPrototype): Promise<EggObject>;
|
|
69
|
+
destroy(): Promise<void>;
|
|
70
|
+
init(): Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/impl/EggObjectImpl.d.ts
|
|
74
|
+
declare class EggObjectImpl implements EggObject {
|
|
75
|
+
private _obj;
|
|
76
|
+
private status;
|
|
77
|
+
readonly proto: EggPrototype;
|
|
78
|
+
readonly name: EggObjectName;
|
|
79
|
+
readonly id: string;
|
|
80
|
+
constructor(name: EggObjectName, proto: EggPrototype);
|
|
81
|
+
initWithInjectProperty(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
82
|
+
initWithInjectConstructor(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
83
|
+
init(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
84
|
+
destroy(ctx: EggObjectLifeCycleContext): Promise<void>;
|
|
85
|
+
injectProperty(name: EggObjectName, descriptor: PropertyDescriptor): void;
|
|
86
|
+
get obj(): object;
|
|
87
|
+
get isReady(): boolean;
|
|
88
|
+
static createObject(name: EggObjectName, proto: EggPrototype, lifecycleContext: EggObjectLifeCycleContext): Promise<EggObjectImpl>;
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/impl/EggObjectUtil.d.ts
|
|
92
|
+
declare class EggObjectUtil {
|
|
93
|
+
static eggObjectGetProperty(eggObject: EggObject): PropertyDescriptor;
|
|
94
|
+
static contextEggObjectGetProperty(proto: EggPrototype, objName: PropertyKey): PropertyDescriptor;
|
|
95
|
+
static eggObjectProxy(eggObject: EggObject): PropertyDescriptor;
|
|
96
|
+
static contextEggObjectProxy(proto: EggPrototype, objName: PropertyKey): PropertyDescriptor;
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/impl/ModuleLoadUnitInstance.d.ts
|
|
100
|
+
declare class ModuleLoadUnitInstance implements LoadUnitInstance {
|
|
101
|
+
readonly loadUnit: LoadUnit;
|
|
102
|
+
readonly id: string;
|
|
103
|
+
readonly name: string;
|
|
104
|
+
private protoToCreateMap;
|
|
105
|
+
private eggObjectMap;
|
|
106
|
+
private eggObjectPromiseMap;
|
|
107
|
+
constructor(loadUnit: LoadUnit);
|
|
108
|
+
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
109
|
+
addProtoToCreate(name: string, proto: EggPrototype): void;
|
|
110
|
+
deleteProtoToCreate(name: string): void;
|
|
111
|
+
init(ctx: LoadUnitInstanceLifecycleContext): Promise<void>;
|
|
112
|
+
destroy(): Promise<void>;
|
|
113
|
+
getOrCreateEggObject(name: EggPrototypeName, proto: EggPrototype): Promise<EggObject>;
|
|
114
|
+
getEggObject(name: EggPrototypeName, proto: EggPrototype): EggObject;
|
|
115
|
+
static createModuleLoadUnitInstance(ctx: LoadUnitInstanceLifecycleContext): LoadUnitInstance;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/factory/EggContainerFactory.d.ts
|
|
119
|
+
declare class EggContainerFactory {
|
|
120
|
+
private static containerGetMethodMap;
|
|
121
|
+
private static ContextInitiatorClass;
|
|
122
|
+
static registerContainerGetMethod(initType: ObjectInitTypeLike, method: ContainerGetMethod): void;
|
|
123
|
+
static getContainer(proto: EggPrototype): EggContainer<LifecycleContext>;
|
|
124
|
+
/**
|
|
125
|
+
* get or create egg object
|
|
126
|
+
* If get singleton egg object in context,
|
|
127
|
+
* will create context egg object for it.
|
|
128
|
+
*/
|
|
129
|
+
static getOrCreateEggObject(proto: EggPrototype, name?: EggObjectName): Promise<EggObject>;
|
|
130
|
+
/**
|
|
131
|
+
* get or create egg object from the Class
|
|
132
|
+
* If get singleton egg object in context,
|
|
133
|
+
* will create context egg object for it.
|
|
134
|
+
*/
|
|
135
|
+
static getOrCreateEggObjectFromClazz(clazz: EggProtoImplClass, name?: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject>;
|
|
136
|
+
/**
|
|
137
|
+
* get or create egg object from the Name
|
|
138
|
+
* If get singleton egg object in context,
|
|
139
|
+
* will create context egg object for it.
|
|
140
|
+
*/
|
|
141
|
+
static getOrCreateEggObjectFromName(name: EggObjectName, qualifiers?: QualifierInfo[]): Promise<EggObject>;
|
|
142
|
+
static getEggObject(proto: EggPrototype, name?: EggObjectName): EggObject;
|
|
143
|
+
}
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/factory/EggObjectFactory.d.ts
|
|
146
|
+
interface EggObjectPair {
|
|
147
|
+
obj: EggObject;
|
|
148
|
+
ctx: EggObjectLifeCycleContext;
|
|
149
|
+
}
|
|
150
|
+
declare class EggObjectFactory {
|
|
151
|
+
static eggObjectMap: Map<string, EggObjectPair>;
|
|
152
|
+
static eggObjectCreateMap: Map<EggPrototypeClass, CreateObjectMethod>;
|
|
153
|
+
static registerEggObjectCreateMethod(protoClass: EggPrototypeClass, method: CreateObjectMethod): void;
|
|
154
|
+
static getEggObjectCreateMethod(protoClass: EggPrototypeClass): CreateObjectMethod;
|
|
155
|
+
static createObject(name: EggObjectName, proto: EggPrototype): Promise<EggObject>;
|
|
156
|
+
static destroyObject(obj: EggObject): Promise<void>;
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/factory/LoadUnitInstanceFactory.d.ts
|
|
160
|
+
type LoadUnitInstanceCreator = (ctx: LoadUnitInstanceLifecycleContext) => LoadUnitInstance;
|
|
161
|
+
declare class LoadUnitInstanceFactory {
|
|
162
|
+
private static creatorMap;
|
|
163
|
+
private static instanceMap;
|
|
164
|
+
static registerLoadUnitInstanceClass(type: EggLoadUnitTypeLike, creator: LoadUnitInstanceCreator): void;
|
|
165
|
+
static createLoadUnitInstance(loadUnit: LoadUnit): Promise<LoadUnitInstance>;
|
|
166
|
+
static getLoadUnitInstance(loadUnit: LoadUnit): LoadUnitInstance | undefined;
|
|
167
|
+
static destroyLoadUnitInstance(loadUnitInstance: LoadUnitInstance): Promise<void>;
|
|
168
|
+
static getLoadUnitInstanceByProto(proto: EggPrototype): LoadUnitInstance;
|
|
169
|
+
}
|
|
170
|
+
//#endregion
|
|
17
171
|
export { AbstractEggContext, ContextHandler, ContextInitiator, ContextObjectGraph, EggAlwaysNewObjectContainer, EggContainerFactory, type EggContext, EggContextLifecycleUtil, EggObjectFactory, EggObjectImpl, EggObjectLifecycleUtil, EggObjectUtil, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil, ModuleLoadUnitInstance };
|