-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure treat function app.js as configDidLoad (#181)
- Loading branch information
1 parent
c2e1ef6
commit 33c07db
Showing
17 changed files
with
148 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "bootPluginDep", | ||
"eggPlugin": { | ||
"name": "bootPluginDep", | ||
"deps": [ | ||
"bootPuginDep" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.