Skip to content

Commit

Permalink
fix: ensure treat function app.js as configDidLoad (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Sep 21, 2018
1 parent c2e1ef6 commit 33c07db
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ Load app/extend/helper.js

#### loadCustomApp

Load app.js
Load app.js, if app.js export boot class, then trigger configDidLoad

#### loadCustomAgent

Load agent.js
Load agent.js, if agent.js export boot class, then trigger configDidLoad

#### loadService

Expand Down
13 changes: 13 additions & 0 deletions lib/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ class Lifecycle extends EventEmitter {
this[BOOT_HOOKS].push(hook);
}

addFunctionAsBootHook(hook) {
// app.js is export as a funciton
// call this function in configDidLoad
this[BOOT_HOOKS].push(class Hook {
constructor(app) {
this.app = app;
}
configDidLoad() {
hook(this.app);
}
});
}

/**
* init boots and trigger config did config
*/
Expand Down
5 changes: 0 additions & 5 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ class EggLoader {
: this.getServerScope();
}

get bootFileName() {
return this.app.type === 'application' ? 'app' : 'agent';
}

/**
* Get {@link AppInfo#env}
* @return {String} env
Expand Down Expand Up @@ -468,7 +464,6 @@ const loaders = [
require('./mixin/middleware'),
require('./mixin/controller'),
require('./mixin/router'),
require('./mixin/boot'),
];

for (const loader of loaders) {
Expand Down
28 changes: 0 additions & 28 deletions lib/loader/mixin/boot.js

This file was deleted.

60 changes: 46 additions & 14 deletions lib/loader/mixin/custom.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,73 @@
'use strict';

const is = require('is-type-of');
const path = require('path');

const LOAD_BOOT_HOOK = Symbol('Loader#loadBootHook');

module.exports = {

/**
* load app.js
*
* @example
* - old:
*
* ```js
* module.exports = function(app) {
* // can do everything
* do();
*
* // if you will invoke asynchronous, you can use readyCallback
* const done = app.readyCallback();
* doAsync(done);
* doSomething();
* }
* ```
*
* - new:
*
* ```js
* module.exports = class Boot {
* constructor(app) {
* this.app = app;
* }
* configDidLoad() {
* doSomething();
* }
* }
* @since 1.0.0
*/
loadCustomApp() {
this.timing.start('Load app.js');
this[LOAD_BOOT_HOOK]('app');
this.lifecycle.triggerConfigDidLoad();
this.getLoadUnits()
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'app'))));
this.timing.end('Load app.js');
},

/**
* Load agent.js, same as {@link EggLoader#loadCustomApp}
*/
loadCustomAgent() {
this.timing.start('Load agent.js');
this[LOAD_BOOT_HOOK]('agent');
this.lifecycle.triggerConfigDidLoad();
this.getLoadUnits()
.forEach(unit => this.loadFile(this.resolveModule(path.join(unit.path, 'agent'))));
this.timing.end('Load agent.js');
},

// FIXME: no logger used after egg removed
loadBootHook() {
// do nothing
},

[LOAD_BOOT_HOOK](fileName) {
this.timing.start(`Load ${fileName}.js`);
for (const unit of this.getLoadUnits()) {
const bootFilePath = this.resolveModule(path.join(unit.path, fileName));
if (!bootFilePath) {
continue;
}
const bootHook = this.requireFile(bootFilePath);
if (is.class(bootHook)) {
// if is boot class, add to lifecycle
this.lifecycle.addBootHook(bootHook);
} else {
// if is boot function, wrap to class
this.lifecycle.addFunctionAsBootHook(bootHook);
}
}
// init boots
this.lifecycle.init();
this.timing.end(`Load ${fileName}.js`);
},
};
61 changes: 36 additions & 25 deletions test/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,15 +569,17 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
]);
await app.ready();
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -587,8 +589,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -600,8 +603,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -613,8 +617,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -632,20 +637,21 @@ describe('test/egg.test.js', () => {
app.loader.loadPlugin();
app.loader.loadConfig();
app.loader.loadAgentExtend();
app.loader.loadBootHook();
app.loader.loadCustomAgent();
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'agent.js in plugin',
'configDidLoad in app',
]);
await app.ready();
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'agent.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -655,8 +661,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'agent.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -668,8 +675,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'agent.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -681,8 +689,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'agent.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand Down Expand Up @@ -818,8 +827,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand All @@ -832,8 +842,9 @@ describe('test/egg.test.js', () => {
assert.deepStrictEqual(
app.bootLog,
[
'configDidLoad',
'app.js',
'configDidLoad in plugin',
'app.js in plugin',
'configDidLoad in app',
'didLoad',
'beforeStart',
'willReady',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/boot/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = class {
}

configDidLoad() {
this.app.bootLog.push('configDidLoad');
this.app.bootLog.push('configDidLoad in app');
}

async didLoad() {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/boot/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = class {
}

configDidLoad() {
this.app.bootLog.push('configDidLoad');
this.app.bootLog.push('configDidLoad in app');
}

async didLoad() {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/boot/app/plugin/boot-plugin-dep/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = class Boot {
constructor(agent) {
this.agent = agent;
}
configDidLoad() {
this.agent.bootLog.push('configDidLoad in plugin');
}
};
10 changes: 10 additions & 0 deletions test/fixtures/boot/app/plugin/boot-plugin-dep/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = class Boot {
constructor(app) {
this.app = app;
}
configDidLoad() {
this.app.bootLog.push('configDidLoad in plugin');
}
};
9 changes: 9 additions & 0 deletions test/fixtures/boot/app/plugin/boot-plugin-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "bootPluginDep",
"eggPlugin": {
"name": "bootPluginDep",
"deps": [
"bootPuginDep"
]
}
}
12 changes: 6 additions & 6 deletions test/fixtures/boot/app/plugin/boot-plugin/agent.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
const sleep = require('mz-modules/sleep');

module.exports = app => {
app.bootLog.push('app.js');
app.beforeStart(async () => {
module.exports = agent => {
agent.bootLog.push('agent.js in plugin');
agent.beforeStart(async () => {
await sleep(5);
app.bootLog.push('beforeStart');
agent.bootLog.push('beforeStart');
});

app.ready(()=> {
app.bootLog.push('ready');
agent.ready(()=> {
agent.bootLog.push('ready');
});
};
2 changes: 1 addition & 1 deletion test/fixtures/boot/app/plugin/boot-plugin/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const sleep = require('mz-modules/sleep');

module.exports = app => {
app.bootLog.push('app.js');
app.bootLog.push('app.js in plugin');
app.beforeStart(async () => {
await sleep(5);
app.bootLog.push('beforeStart');
Expand Down
Loading

0 comments on commit 33c07db

Please sign in to comment.