@eggjs/tegg-plugin 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.
Files changed (99) hide show
  1. package/README.md +13 -1
  2. package/dist/app/extend/application.d.ts +57 -0
  3. package/dist/app/extend/application.js +72 -0
  4. package/dist/app/extend/application.unittest.d.ts +26 -0
  5. package/dist/app/extend/application.unittest.js +43 -0
  6. package/dist/app/extend/context.d.ts +24 -0
  7. package/dist/app/extend/context.js +30 -0
  8. package/dist/app/middleware/tegg_ctx_lifecycle_middleware.d.ts +6 -0
  9. package/dist/app/middleware/tegg_ctx_lifecycle_middleware.js +9 -0
  10. package/dist/app.d.ts +19 -0
  11. package/dist/app.js +57 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/index.js +5 -0
  14. package/dist/lib/AppLoadUnit.d.ts +23 -0
  15. package/dist/lib/AppLoadUnit.js +78 -0
  16. package/dist/lib/AppLoadUnitInstance.d.ts +24 -0
  17. package/dist/lib/AppLoadUnitInstance.js +69 -0
  18. package/dist/lib/CompatibleUtil.d.ts +18 -0
  19. package/dist/lib/CompatibleUtil.js +96 -0
  20. package/dist/lib/ConfigSourceLoadUnitHook.d.ts +15 -0
  21. package/dist/lib/ConfigSourceLoadUnitHook.js +22 -0
  22. package/dist/lib/EggAppLoader.d.ts +20 -0
  23. package/dist/lib/EggAppLoader.js +106 -0
  24. package/dist/lib/EggCompatibleObject.d.ts +21 -0
  25. package/dist/lib/EggCompatibleObject.js +37 -0
  26. package/dist/lib/EggCompatibleProtoImpl.d.ts +25 -0
  27. package/dist/lib/EggCompatibleProtoImpl.js +50 -0
  28. package/dist/lib/EggContextCompatibleHook.d.ts +14 -0
  29. package/dist/lib/EggContextCompatibleHook.js +32 -0
  30. package/dist/lib/EggContextHandler.d.ts +13 -0
  31. package/dist/lib/EggContextHandler.js +29 -0
  32. package/dist/lib/EggContextImpl.d.ts +10 -0
  33. package/dist/lib/EggContextImpl.js +18 -0
  34. package/dist/lib/EggModuleLoader.d.ts +15 -0
  35. package/dist/lib/EggModuleLoader.js +50 -0
  36. package/dist/lib/EggQualifierProtoHook.d.ts +14 -0
  37. package/dist/lib/EggQualifierProtoHook.js +48 -0
  38. package/dist/lib/ModuleConfigLoader.d.ts +12 -0
  39. package/dist/lib/ModuleConfigLoader.js +84 -0
  40. package/dist/lib/ModuleHandler.d.ts +17 -0
  41. package/dist/lib/ModuleHandler.js +46 -0
  42. package/dist/lib/Utils.d.ts +4 -0
  43. package/dist/lib/Utils.js +33 -0
  44. package/dist/lib/ctx_lifecycle_middleware.d.ts +6 -0
  45. package/dist/lib/ctx_lifecycle_middleware.js +27 -0
  46. package/dist/lib/run_in_background.d.ts +7 -0
  47. package/dist/lib/run_in_background.js +52 -0
  48. package/dist/types.d.ts +7 -0
  49. package/dist/types.js +6 -0
  50. package/package.json +63 -45
  51. package/app/extend/application.d.ts +0 -24
  52. package/app/extend/application.js +0 -76
  53. package/app/extend/application.unittest.d.ts +0 -8
  54. package/app/extend/application.unittest.js +0 -51
  55. package/app/extend/context.d.ts +0 -15
  56. package/app/extend/context.js +0 -32
  57. package/app/middleware/tegg_ctx_lifecycle_middleware.d.ts +0 -3
  58. package/app/middleware/tegg_ctx_lifecycle_middleware.js +0 -5
  59. package/app.d.ts +0 -17
  60. package/app.js +0 -63
  61. package/index.d.ts +0 -3
  62. package/index.js +0 -4
  63. package/lib/AppLoadUnit.d.ts +0 -19
  64. package/lib/AppLoadUnit.js +0 -85
  65. package/lib/AppLoadUnitInstance.d.ts +0 -20
  66. package/lib/AppLoadUnitInstance.js +0 -82
  67. package/lib/CompatibleUtil.d.ts +0 -14
  68. package/lib/CompatibleUtil.js +0 -94
  69. package/lib/ConfigSourceLoadUnitHook.d.ts +0 -10
  70. package/lib/ConfigSourceLoadUnitHook.js +0 -20
  71. package/lib/EggAppLoader.d.ts +0 -16
  72. package/lib/EggAppLoader.js +0 -127
  73. package/lib/EggCompatibleObject.d.ts +0 -18
  74. package/lib/EggCompatibleObject.js +0 -41
  75. package/lib/EggCompatibleProtoImpl.d.ts +0 -21
  76. package/lib/EggCompatibleProtoImpl.js +0 -51
  77. package/lib/EggContextCompatibleHook.d.ts +0 -10
  78. package/lib/EggContextCompatibleHook.js +0 -42
  79. package/lib/EggContextHandler.d.ts +0 -9
  80. package/lib/EggContextHandler.js +0 -25
  81. package/lib/EggContextImpl.d.ts +0 -6
  82. package/lib/EggContextImpl.js +0 -15
  83. package/lib/EggModuleLoader.d.ts +0 -11
  84. package/lib/EggModuleLoader.js +0 -53
  85. package/lib/EggQualifierProtoHook.d.ts +0 -10
  86. package/lib/EggQualifierProtoHook.js +0 -50
  87. package/lib/ModuleConfigLoader.d.ts +0 -8
  88. package/lib/ModuleConfigLoader.js +0 -80
  89. package/lib/ModuleHandler.d.ts +0 -13
  90. package/lib/ModuleHandler.js +0 -53
  91. package/lib/Utils.d.ts +0 -1
  92. package/lib/Utils.js +0 -36
  93. package/lib/ctx_lifecycle_middleware.d.ts +0 -2
  94. package/lib/ctx_lifecycle_middleware.js +0 -32
  95. package/lib/run_in_background.d.ts +0 -3
  96. package/lib/run_in_background.js +0 -55
  97. package/lib/types.d.ts +0 -43
  98. package/lib/types.js +0 -2
  99. package/typings/index.d.ts +0 -1
package/README.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # `@eggjs/tegg-plugin`
2
2
 
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Known Vulnerabilities][snyk-image]][snyk-url]
5
+ [![npm download][download-image]][download-url]
6
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/tegg-plugin.svg?style=flat)](https://nodejs.org/en/download/)
7
+
8
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-plugin.svg?style=flat-square
9
+ [npm-url]: https://npmjs.org/package/@eggjs/tegg-plugin
10
+ [snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-plugin/badge.svg?style=flat-square
11
+ [snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-plugin
12
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-plugin.svg?style=flat-square
13
+ [download-url]: https://npmjs.org/package/@eggjs/tegg-plugin
14
+
3
15
  ## Usage
4
16
 
5
- Please read [../../README.md](../..)
17
+ Please read [../../README.md](../../README.md)
@@ -0,0 +1,57 @@
1
+ import { ModuleHandler } from "../../lib/ModuleHandler.js";
2
+ import { EggContextHandler } from "../../lib/EggContextHandler.js";
3
+ import * as _eggjs_tegg_metadata0 from "@eggjs/tegg-metadata";
4
+ import { EggPrototypeCreatorFactory, EggPrototypeFactory, EggPrototypeLifecycleUtil, LoadUnitFactory, LoadUnitLifecycleUtil } from "@eggjs/tegg-metadata";
5
+ import * as _eggjs_tegg0 from "@eggjs/tegg";
6
+ import { EggProtoImplClass, IdenticalUtil, QualifierInfo, RuntimeConfig } from "@eggjs/tegg";
7
+ import * as _eggjs_tegg_runtime0 from "@eggjs/tegg-runtime";
8
+ import { AbstractEggContext, EggContainerFactory, EggContext, EggContextLifecycleUtil, EggObjectFactory, EggObjectLifecycleUtil, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil } from "@eggjs/tegg-runtime";
9
+ import { Application } from "egg";
10
+ import { LoaderFactory } from "@eggjs/tegg-loader";
11
+
12
+ //#region src/app/extend/application.d.ts
13
+ declare class TEggPluginApplication extends Application {
14
+ get eggPrototypeCreatorFactory(): typeof EggPrototypeCreatorFactory;
15
+ get eggPrototypeFactory(): EggPrototypeFactory;
16
+ get loadUnitLifecycleUtil(): _eggjs_tegg0.LifecycleUtil<_eggjs_tegg_metadata0.LoadUnitLifecycleContext, _eggjs_tegg_metadata0.LoadUnit>;
17
+ get loadUnitFactory(): typeof LoadUnitFactory;
18
+ get eggObjectFactory(): typeof EggObjectFactory;
19
+ get loadUnitInstanceFactory(): typeof LoadUnitInstanceFactory;
20
+ get loadUnitInstanceLifecycleUtil(): _eggjs_tegg0.LifecycleUtil<_eggjs_tegg_runtime0.LoadUnitInstanceLifecycleContext, _eggjs_tegg_runtime0.LoadUnitInstance>;
21
+ get eggContainerFactory(): typeof EggContainerFactory;
22
+ get loaderFactory(): typeof LoaderFactory;
23
+ get eggPrototypeLifecycleUtil(): _eggjs_tegg0.LifecycleUtil<_eggjs_tegg_metadata0.EggPrototypeLifecycleContext, _eggjs_tegg_metadata0.EggPrototype>;
24
+ get eggContextLifecycleUtil(): _eggjs_tegg0.LifecycleUtil<_eggjs_tegg_runtime0.EggContextLifecycleContext, EggContext>;
25
+ get eggObjectLifecycleUtil(): _eggjs_tegg0.LifecycleUtil<_eggjs_tegg_runtime0.EggObjectLifeCycleContext, _eggjs_tegg_runtime0.EggObject>;
26
+ get abstractEggContext(): typeof AbstractEggContext;
27
+ get identicalUtil(): typeof IdenticalUtil;
28
+ get runtimeConfig(): RuntimeConfig;
29
+ getEggObject<T>(clazz: EggProtoImplClass<T>, name?: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
30
+ getEggObjectFromName<T extends object>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
31
+ }
32
+ declare module 'egg' {
33
+ interface Application {
34
+ eggPrototypeCreatorFactory: typeof EggPrototypeCreatorFactory;
35
+ eggPrototypeFactory: EggPrototypeFactory;
36
+ eggContainerFactory: typeof EggContainerFactory;
37
+ loadUnitFactory: typeof LoadUnitFactory;
38
+ eggObjectFactory: typeof EggObjectFactory;
39
+ loadUnitInstanceFactory: typeof LoadUnitInstanceFactory;
40
+ abstractEggContext: typeof AbstractEggContext;
41
+ identicalUtil: typeof IdenticalUtil;
42
+ loaderFactory: typeof LoaderFactory;
43
+ loadUnitLifecycleUtil: typeof LoadUnitLifecycleUtil;
44
+ loadUnitInstanceLifecycleUtil: typeof LoadUnitInstanceLifecycleUtil;
45
+ eggPrototypeLifecycleUtil: typeof EggPrototypeLifecycleUtil;
46
+ eggContextLifecycleUtil: typeof EggContextLifecycleUtil;
47
+ eggObjectLifecycleUtil: typeof EggObjectLifecycleUtil;
48
+ teggContext: EggContext;
49
+ moduleHandler: ModuleHandler;
50
+ eggContextHandler: EggContextHandler;
51
+ getEggObject<T>(clazz: new (...args: any[]) => T, name?: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
52
+ getEggObjectFromName<T extends object>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
53
+ module: EggModule;
54
+ }
55
+ }
56
+ //#endregion
57
+ export { TEggPluginApplication as default };
@@ -0,0 +1,72 @@
1
+ import "../../lib/ModuleHandler.js";
2
+ import "../../lib/EggContextHandler.js";
3
+ import { EggPrototypeCreatorFactory, EggPrototypeFactory, EggPrototypeLifecycleUtil, LoadUnitFactory, LoadUnitLifecycleUtil } from "@eggjs/tegg-metadata";
4
+ import { IdenticalUtil } from "@eggjs/tegg";
5
+ import { AbstractEggContext, EggContainerFactory, EggContextLifecycleUtil, EggObjectFactory, EggObjectLifecycleUtil, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil } from "@eggjs/tegg-runtime";
6
+ import { Application } from "egg";
7
+ import { LoaderFactory } from "@eggjs/tegg-loader";
8
+
9
+ //#region src/app/extend/application.ts
10
+ var TEggPluginApplication = class extends Application {
11
+ get eggPrototypeCreatorFactory() {
12
+ return EggPrototypeCreatorFactory;
13
+ }
14
+ get eggPrototypeFactory() {
15
+ return EggPrototypeFactory.instance;
16
+ }
17
+ get loadUnitLifecycleUtil() {
18
+ return LoadUnitLifecycleUtil;
19
+ }
20
+ get loadUnitFactory() {
21
+ return LoadUnitFactory;
22
+ }
23
+ get eggObjectFactory() {
24
+ return EggObjectFactory;
25
+ }
26
+ get loadUnitInstanceFactory() {
27
+ return LoadUnitInstanceFactory;
28
+ }
29
+ get loadUnitInstanceLifecycleUtil() {
30
+ return LoadUnitInstanceLifecycleUtil;
31
+ }
32
+ get eggContainerFactory() {
33
+ return EggContainerFactory;
34
+ }
35
+ get loaderFactory() {
36
+ return LoaderFactory;
37
+ }
38
+ get eggPrototypeLifecycleUtil() {
39
+ return EggPrototypeLifecycleUtil;
40
+ }
41
+ get eggContextLifecycleUtil() {
42
+ return EggContextLifecycleUtil;
43
+ }
44
+ get eggObjectLifecycleUtil() {
45
+ return EggObjectLifecycleUtil;
46
+ }
47
+ get abstractEggContext() {
48
+ return AbstractEggContext;
49
+ }
50
+ get identicalUtil() {
51
+ return IdenticalUtil;
52
+ }
53
+ get runtimeConfig() {
54
+ const config = this.config;
55
+ return {
56
+ baseDir: config.baseDir,
57
+ env: config.env,
58
+ name: config.name
59
+ };
60
+ }
61
+ async getEggObject(clazz, name, qualifiers) {
62
+ if (qualifiers) qualifiers = Array.isArray(qualifiers) ? qualifiers : [qualifiers];
63
+ return (await EggContainerFactory.getOrCreateEggObjectFromClazz(clazz, name, qualifiers)).obj;
64
+ }
65
+ async getEggObjectFromName(name, qualifiers) {
66
+ if (qualifiers) qualifiers = Array.isArray(qualifiers) ? qualifiers : [qualifiers];
67
+ return (await EggContainerFactory.getOrCreateEggObjectFromName(name, qualifiers)).obj;
68
+ }
69
+ };
70
+
71
+ //#endregion
72
+ export { TEggPluginApplication as default };
@@ -0,0 +1,26 @@
1
+ import { Application, Context } from "egg";
2
+
3
+ //#region src/app/extend/application.unittest.d.ts
4
+ declare class TEggPluginApplicationUnittest extends Application {
5
+ mockModuleContext(data?: any): Promise<Context>;
6
+ destroyModuleContext(ctx: Context): Promise<void>;
7
+ mockModuleContextScope<R = any>(fn: (ctx: Context) => Promise<R>, data?: any): Promise<R>;
8
+ }
9
+ declare module 'egg' {
10
+ interface Application {
11
+ /**
12
+ * Mock the module context, only for unittest
13
+ */
14
+ mockModuleContext(data?: any): Promise<Context>;
15
+ /**
16
+ * Mock the module context scope, only for unittest
17
+ */
18
+ mockModuleContextScope<R = any>(fn: (ctx: Context) => Promise<R>, data?: any): Promise<R>;
19
+ /**
20
+ * Destroy the module context, only for unittest
21
+ */
22
+ destroyModuleContext(context: Context): Promise<void>;
23
+ }
24
+ }
25
+ //#endregion
26
+ export { TEggPluginApplicationUnittest as default };
@@ -0,0 +1,43 @@
1
+ import { EggContextImpl } from "../../lib/EggContextImpl.js";
2
+ import "@eggjs/tegg-runtime";
3
+ import { Application } from "egg";
4
+
5
+ //#region src/app/extend/application.unittest.ts
6
+ const TEGG_LIFECYCLE_CACHE = /* @__PURE__ */ new Map();
7
+ let hasMockModuleContext = false;
8
+ var TEggPluginApplicationUnittest = class extends Application {
9
+ async mockModuleContext(data) {
10
+ this.deprecate("app.mockModuleContext is deprecated, use mockModuleContextScope.");
11
+ if (hasMockModuleContext) throw new Error("should not call mockModuleContext twice.");
12
+ const ctx = this.mockContext(data);
13
+ const teggCtx = new EggContextImpl(ctx);
14
+ const lifecycle = {};
15
+ TEGG_LIFECYCLE_CACHE.set(teggCtx, lifecycle);
16
+ if (teggCtx.init) await teggCtx.init(lifecycle);
17
+ hasMockModuleContext = true;
18
+ return ctx;
19
+ }
20
+ async destroyModuleContext(ctx) {
21
+ hasMockModuleContext = false;
22
+ const teggCtx = ctx.teggContext;
23
+ if (!teggCtx) return;
24
+ const lifecycle = TEGG_LIFECYCLE_CACHE.get(teggCtx);
25
+ if (teggCtx.destroy && lifecycle) await teggCtx.destroy(lifecycle);
26
+ }
27
+ async mockModuleContextScope(fn, data) {
28
+ if (hasMockModuleContext) throw new Error("mockModuleContextScope can not use with mockModuleContext, should use mockModuleContextScope only.");
29
+ return this.mockContextScope(async (ctx) => {
30
+ const teggCtx = new EggContextImpl(ctx);
31
+ const lifecycle = {};
32
+ if (teggCtx.init) await teggCtx.init(lifecycle);
33
+ try {
34
+ return await fn(ctx);
35
+ } finally {
36
+ await teggCtx.destroy(lifecycle);
37
+ }
38
+ }, data);
39
+ }
40
+ };
41
+
42
+ //#endregion
43
+ export { TEggPluginApplicationUnittest as default };
@@ -0,0 +1,24 @@
1
+ import { EggProtoImplClass, QualifierInfo } from "@eggjs/tegg";
2
+ import { EggContext } from "@eggjs/tegg-runtime";
3
+ import { TEGG_CONTEXT } from "@eggjs/egg-module-common";
4
+ import { Context } from "egg";
5
+
6
+ //#region src/app/extend/context.d.ts
7
+ declare class TEggPluginContext extends Context {
8
+ [TEGG_CONTEXT]: EggContext | undefined;
9
+ beginModuleScope(func: () => Promise<void>): Promise<void>;
10
+ get teggContext(): EggContext;
11
+ getEggObject<T>(clazz: EggProtoImplClass<T>, name?: string): Promise<T>;
12
+ getEggObjectFromName<T>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
13
+ }
14
+ declare module 'egg' {
15
+ interface Context {
16
+ beginModuleScope(func: () => Promise<void>): Promise<void>;
17
+ getEggObject<T>(clazz: new (...args: any[]) => T, name?: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
18
+ getEggObjectFromName<T>(name: string, qualifiers?: QualifierInfo | QualifierInfo[]): Promise<T>;
19
+ teggContext: EggContext;
20
+ module: EggModule;
21
+ }
22
+ }
23
+ //#endregion
24
+ export { TEggPluginContext as default };
@@ -0,0 +1,30 @@
1
+ import { ctxLifecycleMiddleware } from "../../lib/ctx_lifecycle_middleware.js";
2
+ import "@eggjs/tegg-metadata";
3
+ import { PrototypeUtil } from "@eggjs/tegg";
4
+ import { TEGG_CONTEXT } from "@eggjs/egg-module-common";
5
+ import { Context } from "egg";
6
+
7
+ //#region src/app/extend/context.ts
8
+ var TEggPluginContext = class extends Context {
9
+ [TEGG_CONTEXT];
10
+ async beginModuleScope(func) {
11
+ await ctxLifecycleMiddleware(this, func);
12
+ }
13
+ get teggContext() {
14
+ if (!this[TEGG_CONTEXT]) throw new Error("tegg context have not ready, should call after teggCtxLifecycleMiddleware");
15
+ return this[TEGG_CONTEXT];
16
+ }
17
+ async getEggObject(clazz, name) {
18
+ const protoObj = PrototypeUtil.getClazzProto(clazz);
19
+ if (!protoObj) throw new Error(`can not get proto for clazz ${clazz.name}`);
20
+ const proto = protoObj;
21
+ return (await this.app.eggContainerFactory.getOrCreateEggObject(proto, name ?? proto.name)).obj;
22
+ }
23
+ async getEggObjectFromName(name, qualifiers) {
24
+ if (qualifiers) qualifiers = Array.isArray(qualifiers) ? qualifiers : [qualifiers];
25
+ return (await this.app.eggContainerFactory.getOrCreateEggObjectFromName(name, qualifiers)).obj;
26
+ }
27
+ };
28
+
29
+ //#endregion
30
+ export { TEggPluginContext as default };
@@ -0,0 +1,6 @@
1
+ import { ctxLifecycleMiddleware } from "../../lib/ctx_lifecycle_middleware.js";
2
+
3
+ //#region src/app/middleware/tegg_ctx_lifecycle_middleware.d.ts
4
+ declare const _default: () => typeof ctxLifecycleMiddleware;
5
+ //#endregion
6
+ export { _default as default };
@@ -0,0 +1,9 @@
1
+ import { ctxLifecycleMiddleware } from "../../lib/ctx_lifecycle_middleware.js";
2
+
3
+ //#region src/app/middleware/tegg_ctx_lifecycle_middleware.ts
4
+ var tegg_ctx_lifecycle_middleware_default = () => {
5
+ return ctxLifecycleMiddleware;
6
+ };
7
+
8
+ //#endregion
9
+ export { tegg_ctx_lifecycle_middleware_default as default };
package/dist/app.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { Application, ILifecycleBoot } from "egg";
2
+
3
+ //#region src/app.d.ts
4
+
5
+ declare class App implements ILifecycleBoot {
6
+ private readonly app;
7
+ private compatibleHook?;
8
+ private eggContextHandler;
9
+ private eggQualifierProtoHook;
10
+ private loadUnitMultiInstanceProtoHook;
11
+ private configSourceEggPrototypeHook;
12
+ constructor(app: Application);
13
+ configWillLoad(): void;
14
+ configDidLoad(): void;
15
+ didLoad(): Promise<void>;
16
+ beforeClose(): Promise<void>;
17
+ }
18
+ //#endregion
19
+ export { App as default };
package/dist/app.js ADDED
@@ -0,0 +1,57 @@
1
+ import "./lib/AppLoadUnit.js";
2
+ import "./lib/AppLoadUnitInstance.js";
3
+ import "./lib/EggCompatibleObject.js";
4
+ import { CompatibleUtil } from "./lib/CompatibleUtil.js";
5
+ import { ModuleHandler } from "./lib/ModuleHandler.js";
6
+ import { EggContextCompatibleHook } from "./lib/EggContextCompatibleHook.js";
7
+ import { EggContextHandler } from "./lib/EggContextHandler.js";
8
+ import { hijackRunInBackground } from "./lib/run_in_background.js";
9
+ import { EggQualifierProtoHook } from "./lib/EggQualifierProtoHook.js";
10
+ import { ConfigSourceLoadUnitHook } from "./lib/ConfigSourceLoadUnitHook.js";
11
+ import { LoadUnitMultiInstanceProtoHook } from "@eggjs/tegg-metadata";
12
+
13
+ //#region src/app.ts
14
+ var App = class {
15
+ app;
16
+ compatibleHook;
17
+ eggContextHandler;
18
+ eggQualifierProtoHook;
19
+ loadUnitMultiInstanceProtoHook;
20
+ configSourceEggPrototypeHook;
21
+ constructor(app) {
22
+ this.app = app;
23
+ }
24
+ configWillLoad() {
25
+ this.app.config.coreMiddleware.push("teggCtxLifecycleMiddleware");
26
+ }
27
+ configDidLoad() {
28
+ this.eggContextHandler = new EggContextHandler(this.app);
29
+ this.app.eggContextHandler = this.eggContextHandler;
30
+ this.eggContextHandler.register();
31
+ this.app.moduleHandler = new ModuleHandler(this.app);
32
+ }
33
+ async didLoad() {
34
+ hijackRunInBackground(this.app);
35
+ this.loadUnitMultiInstanceProtoHook = new LoadUnitMultiInstanceProtoHook();
36
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.loadUnitMultiInstanceProtoHook);
37
+ this.eggQualifierProtoHook = new EggQualifierProtoHook(this.app);
38
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.eggQualifierProtoHook);
39
+ this.configSourceEggPrototypeHook = new ConfigSourceLoadUnitHook();
40
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.configSourceEggPrototypeHook);
41
+ await this.app.moduleHandler.init();
42
+ this.compatibleHook = new EggContextCompatibleHook(this.app.moduleHandler);
43
+ this.app.eggContextLifecycleUtil.registerLifecycle(this.compatibleHook);
44
+ }
45
+ async beforeClose() {
46
+ CompatibleUtil.clean();
47
+ await this.app.moduleHandler.destroy();
48
+ if (this.compatibleHook) this.app.eggContextLifecycleUtil.deleteLifecycle(this.compatibleHook);
49
+ if (this.eggQualifierProtoHook) this.app.loadUnitLifecycleUtil.deleteLifecycle(this.eggQualifierProtoHook);
50
+ if (this.configSourceEggPrototypeHook) this.app.loadUnitLifecycleUtil.deleteLifecycle(this.configSourceEggPrototypeHook);
51
+ if (this.loadUnitMultiInstanceProtoHook) this.app.loadUnitLifecycleUtil.deleteLifecycle(this.loadUnitMultiInstanceProtoHook);
52
+ LoadUnitMultiInstanceProtoHook.clear();
53
+ }
54
+ };
55
+
56
+ //#endregion
57
+ export { App as default };
@@ -0,0 +1 @@
1
+ export { };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import "./app/extend/context.js";
2
+ import "./app/extend/application.js";
3
+ import "./types.js";
4
+
5
+ export { };
@@ -0,0 +1,23 @@
1
+ import { EggLoadUnitTypeLike, EggPrototype, LoadUnit, LoadUnitLifecycleContext, Loader } from "@eggjs/tegg-metadata";
2
+ import { Id, QualifierInfo } from "@eggjs/tegg";
3
+
4
+ //#region src/lib/AppLoadUnit.d.ts
5
+ declare class AppLoadUnit implements LoadUnit {
6
+ private readonly loader;
7
+ id: Id;
8
+ readonly name: string;
9
+ readonly type: EggLoadUnitTypeLike;
10
+ readonly unitPath: string;
11
+ private protoMap;
12
+ constructor(name: string, unitPath: string, loader: Loader);
13
+ init(): Promise<void>;
14
+ containPrototype(proto: EggPrototype): boolean;
15
+ getEggPrototype(name: string, qualifiers: QualifierInfo[]): EggPrototype[];
16
+ registerEggPrototype(proto: EggPrototype): void;
17
+ deletePrototype(proto: EggPrototype): void;
18
+ destroy(): Promise<void>;
19
+ iterateEggPrototype(): IterableIterator<EggPrototype>;
20
+ static createModule(ctx: LoadUnitLifecycleContext): AppLoadUnit;
21
+ }
22
+ //#endregion
23
+ export { AppLoadUnit };
@@ -0,0 +1,78 @@
1
+ import { debuglog } from "node:util";
2
+ import { EggLoadUnitType, EggPrototypeCreatorFactory, EggPrototypeFactory, LoadUnitFactory } from "@eggjs/tegg-metadata";
3
+ import { IdenticalUtil, InitTypeQualifierAttribute, LoadUnitNameQualifierAttribute, PrototypeUtil, QualifierUtil } from "@eggjs/tegg";
4
+ import { MapUtil } from "@eggjs/tegg-common-util";
5
+
6
+ //#region src/lib/AppLoadUnit.ts
7
+ const debug = debuglog("tegg/plugin/tegg/lib/AppLoadUnit");
8
+ var AppLoadUnit = class AppLoadUnit {
9
+ loader;
10
+ id;
11
+ name;
12
+ type = EggLoadUnitType.APP;
13
+ unitPath;
14
+ protoMap = /* @__PURE__ */ new Map();
15
+ constructor(name, unitPath, loader) {
16
+ this.id = IdenticalUtil.createLoadUnitId(name);
17
+ this.name = name;
18
+ this.unitPath = unitPath;
19
+ this.loader = loader;
20
+ }
21
+ async init() {
22
+ const clazzList = await this.loader.load();
23
+ if (debug.enabled) debug("init, get clazzList:%j, from unitPath:%o:%o:%o", clazzList.map((t) => t.name), this.type, this.name, this.unitPath);
24
+ for (const clazz of clazzList) {
25
+ const initTypeQualifierAttributeValue = await PrototypeUtil.getInitType(clazz, {
26
+ unitPath: this.unitPath,
27
+ moduleName: this.name
28
+ });
29
+ [{
30
+ attribute: InitTypeQualifierAttribute,
31
+ value: initTypeQualifierAttributeValue
32
+ }, {
33
+ attribute: LoadUnitNameQualifierAttribute,
34
+ value: this.name
35
+ }].forEach((qualifier) => {
36
+ QualifierUtil.addProtoQualifier(clazz, qualifier.attribute, qualifier.value);
37
+ });
38
+ const protos = await EggPrototypeCreatorFactory.createProto(clazz, this);
39
+ for (const proto of protos) EggPrototypeFactory.instance.registerPrototype(proto, this);
40
+ }
41
+ }
42
+ containPrototype(proto) {
43
+ return !!this.protoMap.get(proto.name)?.find((t) => t === proto);
44
+ }
45
+ getEggPrototype(name, qualifiers) {
46
+ return this.protoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
47
+ }
48
+ registerEggPrototype(proto) {
49
+ MapUtil.getOrStore(this.protoMap, proto.name, []).push(proto);
50
+ }
51
+ deletePrototype(proto) {
52
+ const protos = this.protoMap.get(proto.name);
53
+ if (protos) {
54
+ const index = protos.indexOf(proto);
55
+ if (index !== -1) protos.splice(index, 1);
56
+ }
57
+ }
58
+ async destroy() {
59
+ for (const namedProtos of this.protoMap.values()) {
60
+ const protos = namedProtos.slice();
61
+ for (const proto of protos) EggPrototypeFactory.instance.deletePrototype(proto, this);
62
+ }
63
+ this.protoMap.clear();
64
+ }
65
+ iterateEggPrototype() {
66
+ return Array.from(this.protoMap.values()).reduce((p, c) => {
67
+ p = p.concat(c);
68
+ return p;
69
+ }, []).values();
70
+ }
71
+ static createModule(ctx) {
72
+ return new AppLoadUnit("tegg-app", ctx.unitPath, ctx.loader);
73
+ }
74
+ };
75
+ LoadUnitFactory.registerLoadUnitCreator(EggLoadUnitType.APP, AppLoadUnit.createModule);
76
+
77
+ //#endregion
78
+ export { AppLoadUnit };
@@ -0,0 +1,24 @@
1
+ import { EggPrototype, LoadUnit } from "@eggjs/tegg-metadata";
2
+ import { EggObjectName, EggPrototypeName } from "@eggjs/tegg";
3
+ import { EggObject, LoadUnitInstance, LoadUnitInstanceLifecycleContext } from "@eggjs/tegg-runtime";
4
+
5
+ //#region src/lib/AppLoadUnitInstance.d.ts
6
+ declare class AppLoadUnitInstance implements LoadUnitInstance {
7
+ readonly loadUnit: LoadUnit;
8
+ readonly id: string;
9
+ readonly name: string;
10
+ private protoToCreateMap;
11
+ private eggObjectMap;
12
+ private eggObjectPromiseMap;
13
+ constructor(loadUnit: LoadUnit);
14
+ iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
15
+ addProtoToCreate(name: string, proto: EggPrototype): void;
16
+ deleteProtoToCreate(name: string): void;
17
+ init(ctx: LoadUnitInstanceLifecycleContext): Promise<void>;
18
+ destroy(): Promise<void>;
19
+ getOrCreateEggObject(name: EggPrototypeName, proto: EggPrototype): Promise<EggObject>;
20
+ getEggObject(name: EggPrototypeName, proto: EggPrototype): EggObject;
21
+ static createModuleLoadUnitInstance(ctx: LoadUnitInstanceLifecycleContext): LoadUnitInstance;
22
+ }
23
+ //#endregion
24
+ export { AppLoadUnitInstance };
@@ -0,0 +1,69 @@
1
+ import { EggLoadUnitType } from "@eggjs/tegg-metadata";
2
+ import { IdenticalUtil, ObjectInitType } from "@eggjs/tegg";
3
+ import { MapUtil } from "@eggjs/tegg-common-util";
4
+ import { EggObjectFactory, LoadUnitInstanceFactory, LoadUnitInstanceLifecycleUtil } from "@eggjs/tegg-runtime";
5
+
6
+ //#region src/lib/AppLoadUnitInstance.ts
7
+ var AppLoadUnitInstance = class AppLoadUnitInstance {
8
+ loadUnit;
9
+ id;
10
+ name;
11
+ protoToCreateMap = /* @__PURE__ */ new Map();
12
+ eggObjectMap = /* @__PURE__ */ new Map();
13
+ eggObjectPromiseMap = /* @__PURE__ */ new Map();
14
+ constructor(loadUnit) {
15
+ this.loadUnit = loadUnit;
16
+ this.name = loadUnit.name;
17
+ const iterator = this.loadUnit.iterateEggPrototype();
18
+ for (const proto of iterator) if (proto.initType === ObjectInitType.SINGLETON) this.protoToCreateMap.set(proto.name, proto);
19
+ this.id = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
20
+ }
21
+ iterateProtoToCreate() {
22
+ return this.protoToCreateMap.entries();
23
+ }
24
+ addProtoToCreate(name, proto) {
25
+ this.protoToCreateMap.set(name, proto);
26
+ }
27
+ deleteProtoToCreate(name) {
28
+ this.protoToCreateMap.delete(name);
29
+ }
30
+ async init(ctx) {
31
+ await LoadUnitInstanceLifecycleUtil.objectPreCreate(ctx, this);
32
+ for (const [name, proto] of this.protoToCreateMap) await this.getOrCreateEggObject(name, proto);
33
+ }
34
+ async destroy() {
35
+ const objs = [];
36
+ for (const protoObjMap of this.eggObjectMap.values()) for (const protoObj of protoObjMap.values()) objs.push(protoObj);
37
+ this.eggObjectMap.clear();
38
+ await Promise.all(objs.map(async (obj) => {
39
+ await EggObjectFactory.destroyObject(obj);
40
+ }));
41
+ }
42
+ async getOrCreateEggObject(name, proto) {
43
+ if (!this.loadUnit.containPrototype(proto)) throw new Error("load unit not contain proto");
44
+ const protoObjMap = MapUtil.getOrStore(this.eggObjectMap, proto.id, /* @__PURE__ */ new Map());
45
+ if (!protoObjMap.has(name)) {
46
+ const protoObjPromiseMap = MapUtil.getOrStore(this.eggObjectPromiseMap, proto.id, /* @__PURE__ */ new Map());
47
+ if (!protoObjPromiseMap.has(name)) {
48
+ const objPromise = EggObjectFactory.createObject(name, proto);
49
+ protoObjPromiseMap.set(name, objPromise);
50
+ const obj = await objPromise;
51
+ protoObjPromiseMap.delete(name);
52
+ protoObjMap.set(name, obj);
53
+ } else await protoObjPromiseMap.get(name);
54
+ }
55
+ return protoObjMap.get(name);
56
+ }
57
+ getEggObject(name, proto) {
58
+ const protoObjMap = this.eggObjectMap.get(proto.id);
59
+ if (!protoObjMap || !protoObjMap.has(name)) throw new Error(`EggObject ${String(proto.name)} not found`);
60
+ return protoObjMap.get(name);
61
+ }
62
+ static createModuleLoadUnitInstance(ctx) {
63
+ return new AppLoadUnitInstance(ctx.loadUnit);
64
+ }
65
+ };
66
+ LoadUnitInstanceFactory.registerLoadUnitInstanceClass(EggLoadUnitType.APP, AppLoadUnitInstance.createModuleLoadUnitInstance);
67
+
68
+ //#endregion
69
+ export { AppLoadUnitInstance };
@@ -0,0 +1,18 @@
1
+ import { EggPrototype } from "@eggjs/tegg-metadata";
2
+ import { LoadUnitInstance } from "@eggjs/tegg-runtime";
3
+ import { Application, Context } from "egg";
4
+
5
+ //#region src/lib/CompatibleUtil.d.ts
6
+ declare class CompatibleUtil {
7
+ static singletonProtoCache: Map<PropertyKey, EggPrototype>;
8
+ static requestProtoCache: Map<PropertyKey, EggPrototype>;
9
+ static getSingletonProto(name: PropertyKey): EggPrototype;
10
+ static getRequestProto(name: PropertyKey): EggPrototype;
11
+ private static singletonModuleProxyFactory;
12
+ static appCompatible(app: Application, loadUnitInstance: LoadUnitInstance): void;
13
+ static contextModuleProxyFactory(holder: Record<string, any>, ctx: Context, loadUnitInstance: LoadUnitInstance): any;
14
+ static contextModuleCompatible(ctx: Context, loadUnitInstances: LoadUnitInstance[]): void;
15
+ static clean(): void;
16
+ }
17
+ //#endregion
18
+ export { CompatibleUtil };