Skip to content

Commit d4080c0

Browse files
authored
feat: add legacy timing (#249)
- beforeStart with name - readyCallback timing
1 parent 444b168 commit d4080c0

4 files changed

Lines changed: 49 additions & 17 deletions

File tree

lib/egg.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ class EggCore extends KoaApplication {
212212
* @see https://eggjs.org/en/advanced/loader.html#beforestart
213213
*
214214
* @param {Function|GeneratorFunction|AsyncFunction} scope function will execute before app start
215+
* @param {string} [name] scope name, default is empty string
215216
*/
216-
beforeStart(scope) {
217-
this.lifecycle.registerBeforeStart(scope);
217+
beforeStart(scope, name) {
218+
this.lifecycle.registerBeforeStart(scope, name || '');
218219
}
219220

220221
/**

lib/lifecycle.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,23 @@ class Lifecycle extends EventEmitter {
7171
}
7272

7373
legacyReadyCallback(name, opt) {
74-
return this.loadReady.readyCallback(name, opt);
74+
const timingKeyPrefix = 'Did Load';
75+
if (!opt) {
76+
const timing = this.timing;
77+
const cb = this.loadReady.readyCallback(name);
78+
const timingkey = `${timingKeyPrefix} in ` + utils.getResolvedFilename(name, this.app.baseDir);
79+
this.timing.start(timingkey);
80+
return function legacyReadyCallback(...args) {
81+
timing.end(timingkey);
82+
cb(...args);
83+
};
84+
}
85+
this[REGISTER_READY_CALLBACK]({
86+
scope: opt,
87+
ready: this.loadReady,
88+
timingKeyPrefix,
89+
scopeFullName: name,
90+
});
7591
}
7692

7793
addBootHook(hook) {
@@ -102,11 +118,12 @@ class Lifecycle extends EventEmitter {
102118
this[BOOTS] = this[BOOT_HOOKS].map(t => new t(this.app));
103119
}
104120

105-
registerBeforeStart(scope) {
121+
registerBeforeStart(scope, name) {
106122
this[REGISTER_READY_CALLBACK]({
107123
scope,
108124
ready: this.loadReady,
109125
timingKeyPrefix: 'Before Start',
126+
scopeFullName: name,
110127
});
111128
}
112129

test/egg.test.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ describe('test/egg.test.js', () => {
480480
yield app.ready();
481481

482482
const json = app.timing.toJSON();
483-
assert(json.length === 25);
483+
assert(json.length === 28);
484484

485485
assert(json[1].name === 'Application Start');
486486
assert(json[1].end - json[1].start === json[1].duration);
@@ -502,25 +502,29 @@ describe('test/egg.test.js', () => {
502502
assert(json[11].name === 'Load app.js');
503503
assert(json[12].name === 'Require(6) app.js');
504504
assert(json[13].name === 'Before Start in app.js:6:9');
505-
assert(json[14].name === 'Load "proxy" to Context');
506-
assert(json[15].name === 'Load Controller');
507-
assert(json[16].name === 'Load "controller" to Application');
505+
assert(json[14].name === 'Before Start in mock Block');
506+
assert(json[15].name === 'Did Load in mockReadyCallbackWithFunction');
507+
assert(json[16].name === 'Did Load in mockReadyCallbackWithoutFunction');
508+
509+
assert(json[17].name === 'Load "proxy" to Context');
510+
assert(json[18].name === 'Load Controller');
511+
assert(json[19].name === 'Load "controller" to Application');
508512

509513
// loadService
510-
assert(json[17].name === 'Load Service');
511-
assert(json[18].name === 'Load "service" to Context');
514+
assert(json[20].name === 'Load Service');
515+
assert(json[21].name === 'Load "service" to Context');
512516

513517
// loadMiddleware
514-
assert(json[19].name === 'Load Middleware');
515-
assert(json[20].name === 'Load "middlewares" to Application');
518+
assert(json[22].name === 'Load Middleware');
519+
assert(json[23].name === 'Load "middlewares" to Application');
516520

517521
// loadController
518-
assert(json[21].name === 'Load Controller');
519-
assert(json[22].name === 'Load "controller" to Application');
522+
assert(json[24].name === 'Load Controller');
523+
assert(json[25].name === 'Load "controller" to Application');
520524

521525
// loadRouter
522-
assert(json[23].name === 'Load Router');
523-
assert(json[24].name === 'Require(7) app/router.js');
526+
assert(json[26].name === 'Load Router');
527+
assert(json[27].name === 'Require(7) app/router.js');
524528
});
525529
});
526530

test/fixtures/timing/app.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ module.exports = app => {
88

99
app.beforeStart(function* () {
1010
block();
11-
})
11+
});
12+
13+
app.beforeStart(function* () {
14+
block();
15+
}, 'mock Block');
16+
17+
app.readyCallback('mockReadyCallbackWithFunction', function() {
18+
});
19+
20+
const cb = app.readyCallback('mockReadyCallbackWithoutFunction');
21+
setTimeout(cb, 1000);
1222

1323
const directory = path.join(app.baseDir, 'app/proxy');
1424
app.loader.loadToContext(directory, 'proxy');

0 commit comments

Comments
 (0)