@eggjs/core 7.0.0-beta.35 → 7.0.0-beta.36
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/base_context_class.d.ts +19 -14
- package/dist/base_context_class.js +39 -36
- package/dist/egg.d.ts +279 -277
- package/dist/egg.js +394 -426
- package/dist/index.d.ts +12 -12
- package/dist/index.js +12 -12
- package/dist/lifecycle.d.ts +79 -75
- package/dist/lifecycle.js +280 -306
- package/dist/loader/context_loader.d.ts +34 -30
- package/dist/loader/context_loader.js +76 -99
- package/dist/loader/egg_loader.d.ts +370 -366
- package/dist/loader/egg_loader.js +1169 -1567
- package/dist/loader/file_loader.d.ts +99 -95
- package/dist/loader/file_loader.js +190 -241
- package/dist/singleton.d.ts +31 -27
- package/dist/singleton.js +107 -117
- package/dist/types.d.ts +54 -51
- package/dist/utils/index.d.ts +16 -14
- package/dist/utils/index.js +96 -105
- package/dist/utils/sequencify.d.ts +13 -10
- package/dist/utils/sequencify.js +44 -58
- package/dist/utils/timing.d.ts +22 -19
- package/dist/utils/timing.js +85 -92
- package/package.json +35 -40
- package/dist/types.js +0 -2
|
@@ -1,35 +1,39 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { FileLoader, FileLoaderOptions } from "./file_loader.js";
|
|
2
|
+
import { Context } from "../egg.js";
|
|
3
|
+
|
|
4
|
+
//#region src/loader/context_loader.d.ts
|
|
5
|
+
interface ClassLoaderOptions {
|
|
6
|
+
ctx: Context;
|
|
7
|
+
properties: any;
|
|
6
8
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
declare class ClassLoader {
|
|
10
|
+
#private;
|
|
11
|
+
readonly _cache: Map<string, any>;
|
|
12
|
+
_ctx: Context;
|
|
13
|
+
constructor(options: ClassLoaderOptions);
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
interface ContextLoaderOptions extends Omit<FileLoaderOptions, "target"> {
|
|
16
|
+
/** required inject */
|
|
17
|
+
inject: Record<string, any>;
|
|
18
|
+
/** property name defined to target */
|
|
19
|
+
property: string | symbol;
|
|
20
|
+
/** determine the field name of inject object. */
|
|
21
|
+
fieldClass?: string;
|
|
20
22
|
}
|
|
21
23
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`.
|
|
25
|
+
* The exports will be lazy loaded, such as `ctx.group.repository`.
|
|
26
|
+
* @augments FileLoader
|
|
27
|
+
* @since 1.0.0
|
|
28
|
+
*/
|
|
29
|
+
declare class ContextLoader extends FileLoader {
|
|
30
|
+
#private;
|
|
31
|
+
/**
|
|
32
|
+
* @class
|
|
33
|
+
* @param {Object} options - options same as {@link FileLoader}
|
|
34
|
+
* @param {String} options.fieldClass - determine the field name of inject object.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options: ContextLoaderOptions);
|
|
35
37
|
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { ClassLoader, ClassLoaderOptions, ContextLoader, ContextLoaderOptions };
|
|
@@ -1,102 +1,79 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
}
|
|
1
|
+
import { EXPORTS, FileLoader } from "./file_loader.js";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { isClass, isPrimitive } from "is-type-of";
|
|
4
|
+
|
|
5
|
+
//#region src/loader/context_loader.ts
|
|
6
|
+
const CLASS_LOADER = Symbol("classLoader");
|
|
7
|
+
var ClassLoader = class {
|
|
8
|
+
_cache = /* @__PURE__ */ new Map();
|
|
9
|
+
_ctx;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
assert(options.ctx, "options.ctx is required");
|
|
12
|
+
const properties = options.properties;
|
|
13
|
+
this._ctx = options.ctx;
|
|
14
|
+
for (const property in properties) this.#defineProperty(property, properties[property]);
|
|
15
|
+
}
|
|
16
|
+
#defineProperty(property, values) {
|
|
17
|
+
Object.defineProperty(this, property, { get() {
|
|
18
|
+
let instance = this._cache.get(property);
|
|
19
|
+
if (!instance) {
|
|
20
|
+
instance = getInstance(values, this._ctx);
|
|
21
|
+
this._cache.set(property, instance);
|
|
22
|
+
}
|
|
23
|
+
return instance;
|
|
24
|
+
} });
|
|
25
|
+
}
|
|
26
|
+
};
|
|
29
27
|
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const classLoader = ctx[CLASS_LOADER];
|
|
68
|
-
let instance = classLoader.get(property);
|
|
69
|
-
if (!instance) {
|
|
70
|
-
instance = getInstance(target, ctx);
|
|
71
|
-
classLoader.set(property, instance);
|
|
72
|
-
}
|
|
73
|
-
return instance;
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
28
|
+
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`.
|
|
29
|
+
* The exports will be lazy loaded, such as `ctx.group.repository`.
|
|
30
|
+
* @augments FileLoader
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
*/
|
|
33
|
+
var ContextLoader = class extends FileLoader {
|
|
34
|
+
#inject;
|
|
35
|
+
/**
|
|
36
|
+
* @class
|
|
37
|
+
* @param {Object} options - options same as {@link FileLoader}
|
|
38
|
+
* @param {String} options.fieldClass - determine the field name of inject object.
|
|
39
|
+
*/
|
|
40
|
+
constructor(options) {
|
|
41
|
+
assert(options.property, "options.property is required");
|
|
42
|
+
assert(options.inject, "options.inject is required");
|
|
43
|
+
const target = {};
|
|
44
|
+
if (options.fieldClass) options.inject[options.fieldClass] = target;
|
|
45
|
+
super({
|
|
46
|
+
...options,
|
|
47
|
+
target
|
|
48
|
+
});
|
|
49
|
+
this.#inject = this.options.inject;
|
|
50
|
+
const app = this.#inject;
|
|
51
|
+
const property = options.property;
|
|
52
|
+
Object.defineProperty(app.context, property, { get() {
|
|
53
|
+
const ctx = this;
|
|
54
|
+
if (!ctx[CLASS_LOADER]) ctx[CLASS_LOADER] = /* @__PURE__ */ new Map();
|
|
55
|
+
const classLoader = ctx[CLASS_LOADER];
|
|
56
|
+
let instance = classLoader.get(property);
|
|
57
|
+
if (!instance) {
|
|
58
|
+
instance = getInstance(target, ctx);
|
|
59
|
+
classLoader.set(property, instance);
|
|
60
|
+
}
|
|
61
|
+
return instance;
|
|
62
|
+
} });
|
|
63
|
+
}
|
|
64
|
+
};
|
|
78
65
|
function getInstance(values, ctx) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
instance = Class;
|
|
90
|
-
}
|
|
91
|
-
// Can't set property to primitive, so check again
|
|
92
|
-
// e.x. module.exports = 1;
|
|
93
|
-
}
|
|
94
|
-
else if (isPrimitive(values)) {
|
|
95
|
-
instance = values;
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
instance = new ClassLoader({ ctx, properties: values });
|
|
99
|
-
}
|
|
100
|
-
return instance;
|
|
66
|
+
const Class = values[EXPORTS] ? values : null;
|
|
67
|
+
let instance;
|
|
68
|
+
if (Class) if (isClass(Class)) instance = new Class(ctx);
|
|
69
|
+
else instance = Class;
|
|
70
|
+
else if (isPrimitive(values)) instance = values;
|
|
71
|
+
else instance = new ClassLoader({
|
|
72
|
+
ctx,
|
|
73
|
+
properties: values
|
|
74
|
+
});
|
|
75
|
+
return instance;
|
|
101
76
|
}
|
|
102
|
-
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { ClassLoader, ContextLoader };
|