Skip to content

Commit 33c07db

Browse files
dead-horsefengmk2
authored andcommitted
fix: ensure treat function app.js as configDidLoad (#181)
1 parent c2e1ef6 commit 33c07db

17 files changed

Lines changed: 148 additions & 87 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ Load app/extend/helper.js
120120

121121
#### loadCustomApp
122122

123-
Load app.js
123+
Load app.js, if app.js export boot class, then trigger configDidLoad
124124

125125
#### loadCustomAgent
126126

127-
Load agent.js
127+
Load agent.js, if agent.js export boot class, then trigger configDidLoad
128128

129129
#### loadService
130130

lib/lifecycle.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ class Lifecycle extends EventEmitter {
8080
this[BOOT_HOOKS].push(hook);
8181
}
8282

83+
addFunctionAsBootHook(hook) {
84+
// app.js is export as a funciton
85+
// call this function in configDidLoad
86+
this[BOOT_HOOKS].push(class Hook {
87+
constructor(app) {
88+
this.app = app;
89+
}
90+
configDidLoad() {
91+
hook(this.app);
92+
}
93+
});
94+
}
95+
8396
/**
8497
* init boots and trigger config did config
8598
*/

lib/loader/egg_loader.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ class EggLoader {
9595
: this.getServerScope();
9696
}
9797

98-
get bootFileName() {
99-
return this.app.type === 'application' ? 'app' : 'agent';
100-
}
101-
10298
/**
10399
* Get {@link AppInfo#env}
104100
* @return {String} env
@@ -468,7 +464,6 @@ const loaders = [
468464
require('./mixin/middleware'),
469465
require('./mixin/controller'),
470466
require('./mixin/router'),
471-
require('./mixin/boot'),
472467
];
473468

474469
for (const loader of loaders) {

lib/loader/mixin/boot.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/loader/mixin/custom.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,73 @@
11
'use strict';
22

3+
const is = require('is-type-of');
34
const path = require('path');
45

6+
const LOAD_BOOT_HOOK = Symbol('Loader#loadBootHook');
7+
58
module.exports = {
69

710
/**
811
* load app.js
912
*
1013
* @example
14+
* - old:
15+
*
1116
* ```js
1217
* module.exports = function(app) {
13-
* // can do everything
14-
* do();
15-
*
16-
* // if you will invoke asynchronous, you can use readyCallback
17-
* const done = app.readyCallback();
18-
* doAsync(done);
18+
* doSomething();
1919
* }
2020
* ```
21+
*
22+
* - new:
23+
*
24+
* ```js
25+
* module.exports = class Boot {
26+
* constructor(app) {
27+
* this.app = app;
28+
* }
29+
* configDidLoad() {
30+
* doSomething();
31+
* }
32+
* }
2133
* @since 1.0.0
2234
*/
2335
loadCustomApp() {
24-
this.timing.start('Load app.js');
36+
this[LOAD_BOOT_HOOK]('app');
2537
this.lifecycle.triggerConfigDidLoad();
26-
this.getLoadUnits()
27-
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'app'))));
28-
this.timing.end('Load app.js');
2938
},
3039

3140
/**
3241
* Load agent.js, same as {@link EggLoader#loadCustomApp}
3342
*/
3443
loadCustomAgent() {
35-
this.timing.start('Load agent.js');
44+
this[LOAD_BOOT_HOOK]('agent');
3645
this.lifecycle.triggerConfigDidLoad();
37-
this.getLoadUnits()
38-
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'agent'))));
39-
this.timing.end('Load agent.js');
46+
},
47+
48+
// FIXME: no logger used after egg removed
49+
loadBootHook() {
50+
// do nothing
51+
},
52+
53+
[LOAD_BOOT_HOOK](fileName) {
54+
this.timing.start(`Load ${fileName}.js`);
55+
for (const unit of this.getLoadUnits()) {
56+
const bootFilePath = this.resolveModule(path.join(unit.path, fileName));
57+
if (!bootFilePath) {
58+
continue;
59+
}
60+
const bootHook = this.requireFile(bootFilePath);
61+
if (is.class(bootHook)) {
62+
// if is boot class, add to lifecycle
63+
this.lifecycle.addBootHook(bootHook);
64+
} else {
65+
// if is boot function, wrap to class
66+
this.lifecycle.addFunctionAsBootHook(bootHook);
67+
}
68+
}
69+
// init boots
70+
this.lifecycle.init();
71+
this.timing.end(`Load ${fileName}.js`);
4072
},
4173
};

test/egg.test.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,17 @@ describe('test/egg.test.js', () => {
569569
assert.deepStrictEqual(
570570
app.bootLog,
571571
[
572-
'configDidLoad',
573-
'app.js',
572+
'configDidLoad in plugin',
573+
'app.js in plugin',
574+
'configDidLoad in app',
574575
]);
575576
await app.ready();
576577
assert.deepStrictEqual(
577578
app.bootLog,
578579
[
579-
'configDidLoad',
580-
'app.js',
580+
'configDidLoad in plugin',
581+
'app.js in plugin',
582+
'configDidLoad in app',
581583
'didLoad',
582584
'beforeStart',
583585
'willReady',
@@ -587,8 +589,9 @@ describe('test/egg.test.js', () => {
587589
assert.deepStrictEqual(
588590
app.bootLog,
589591
[
590-
'configDidLoad',
591-
'app.js',
592+
'configDidLoad in plugin',
593+
'app.js in plugin',
594+
'configDidLoad in app',
592595
'didLoad',
593596
'beforeStart',
594597
'willReady',
@@ -600,8 +603,9 @@ describe('test/egg.test.js', () => {
600603
assert.deepStrictEqual(
601604
app.bootLog,
602605
[
603-
'configDidLoad',
604-
'app.js',
606+
'configDidLoad in plugin',
607+
'app.js in plugin',
608+
'configDidLoad in app',
605609
'didLoad',
606610
'beforeStart',
607611
'willReady',
@@ -613,8 +617,9 @@ describe('test/egg.test.js', () => {
613617
assert.deepStrictEqual(
614618
app.bootLog,
615619
[
616-
'configDidLoad',
617-
'app.js',
620+
'configDidLoad in plugin',
621+
'app.js in plugin',
622+
'configDidLoad in app',
618623
'didLoad',
619624
'beforeStart',
620625
'willReady',
@@ -632,20 +637,21 @@ describe('test/egg.test.js', () => {
632637
app.loader.loadPlugin();
633638
app.loader.loadConfig();
634639
app.loader.loadAgentExtend();
635-
app.loader.loadBootHook();
636640
app.loader.loadCustomAgent();
637641
assert.deepStrictEqual(
638642
app.bootLog,
639643
[
640-
'configDidLoad',
641-
'app.js',
644+
'configDidLoad in plugin',
645+
'agent.js in plugin',
646+
'configDidLoad in app',
642647
]);
643648
await app.ready();
644649
assert.deepStrictEqual(
645650
app.bootLog,
646651
[
647-
'configDidLoad',
648-
'app.js',
652+
'configDidLoad in plugin',
653+
'agent.js in plugin',
654+
'configDidLoad in app',
649655
'didLoad',
650656
'beforeStart',
651657
'willReady',
@@ -655,8 +661,9 @@ describe('test/egg.test.js', () => {
655661
assert.deepStrictEqual(
656662
app.bootLog,
657663
[
658-
'configDidLoad',
659-
'app.js',
664+
'configDidLoad in plugin',
665+
'agent.js in plugin',
666+
'configDidLoad in app',
660667
'didLoad',
661668
'beforeStart',
662669
'willReady',
@@ -668,8 +675,9 @@ describe('test/egg.test.js', () => {
668675
assert.deepStrictEqual(
669676
app.bootLog,
670677
[
671-
'configDidLoad',
672-
'app.js',
678+
'configDidLoad in plugin',
679+
'agent.js in plugin',
680+
'configDidLoad in app',
673681
'didLoad',
674682
'beforeStart',
675683
'willReady',
@@ -681,8 +689,9 @@ describe('test/egg.test.js', () => {
681689
assert.deepStrictEqual(
682690
app.bootLog,
683691
[
684-
'configDidLoad',
685-
'app.js',
692+
'configDidLoad in plugin',
693+
'agent.js in plugin',
694+
'configDidLoad in app',
686695
'didLoad',
687696
'beforeStart',
688697
'willReady',
@@ -818,8 +827,9 @@ describe('test/egg.test.js', () => {
818827
assert.deepStrictEqual(
819828
app.bootLog,
820829
[
821-
'configDidLoad',
822-
'app.js',
830+
'configDidLoad in plugin',
831+
'app.js in plugin',
832+
'configDidLoad in app',
823833
'didLoad',
824834
'beforeStart',
825835
'willReady',
@@ -832,8 +842,9 @@ describe('test/egg.test.js', () => {
832842
assert.deepStrictEqual(
833843
app.bootLog,
834844
[
835-
'configDidLoad',
836-
'app.js',
845+
'configDidLoad in plugin',
846+
'app.js in plugin',
847+
'configDidLoad in app',
837848
'didLoad',
838849
'beforeStart',
839850
'willReady',

test/fixtures/boot/agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = class {
99
}
1010

1111
configDidLoad() {
12-
this.app.bootLog.push('configDidLoad');
12+
this.app.bootLog.push('configDidLoad in app');
1313
}
1414

1515
async didLoad() {

test/fixtures/boot/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = class {
99
}
1010

1111
configDidLoad() {
12-
this.app.bootLog.push('configDidLoad');
12+
this.app.bootLog.push('configDidLoad in app');
1313
}
1414

1515
async didLoad() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = class Boot {
4+
constructor(agent) {
5+
this.agent = agent;
6+
}
7+
configDidLoad() {
8+
this.agent.bootLog.push('configDidLoad in plugin');
9+
}
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = class Boot {
4+
constructor(app) {
5+
this.app = app;
6+
}
7+
configDidLoad() {
8+
this.app.bootLog.push('configDidLoad in plugin');
9+
}
10+
};

0 commit comments

Comments
 (0)