Skip to content

Commit 77e11f5

Browse files
authored
fix: fix ready callback id (#214)
1 parent 8a77e29 commit 77e11f5

18 files changed

Lines changed: 73 additions & 28 deletions

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ sudo: false
22
os:
33
- linux
44
- osx
5-
- windows
65
language: node_js
76
node_js:
87
- '8'
@@ -13,4 +12,4 @@ install:
1312
script:
1413
- npm run ci
1514
after_success:
16-
- npminstall codecov && codecov --disable=gcov -f .nyc_output/*.json
15+
- npminstall codecov && codecov --disable=gcov

lib/egg.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const CLOSE_PROMISE = Symbol('EggCore#closePromise');
2121
class EggCore extends KoaApplication {
2222

2323
/**
24-
* @constructor
24+
* @class
2525
* @param {Object} options - options
2626
* @param {String} [options.baseDir=process.cwd()] - the directory of application
2727
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
@@ -221,7 +221,7 @@ class EggCore extends KoaApplication {
221221
* register an callback function that will be invoked when application is ready.
222222
* @see https://github.com/node-modules/ready
223223
* @since 1.0.0
224-
* @param {boolean|Error|Function} flagOrFunction -
224+
* @param {boolean|Error|Function} [flagOrFunction] -
225225
* @return {Promise|null} return promise when argument is undefined
226226
* @example
227227
* const app = new Application(...);

lib/lifecycle.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ class Lifecycle extends EventEmitter {
105105
}
106106

107107
registerBeforeStart(scope) {
108-
this[REGISTER_READY_CALLBACK](scope, this.loadReady, 'Before Start');
108+
this[REGISTER_READY_CALLBACK]({
109+
scope,
110+
ready: this.loadReady,
111+
timingKeyPrefix: 'Before Start',
112+
});
109113
}
110114

111115
registerBeforeClose(fn) {
@@ -151,7 +155,12 @@ class Lifecycle extends EventEmitter {
151155
for (const boot of this[BOOTS]) {
152156
const didLoad = boot.didLoad && boot.didLoad.bind(boot);
153157
if (didLoad) {
154-
this[REGISTER_READY_CALLBACK](didLoad, this.loadReady, 'Did Load');
158+
this[REGISTER_READY_CALLBACK]({
159+
scope: didLoad,
160+
ready: this.loadReady,
161+
timingKeyPrefix: 'Did Load',
162+
scopeFullName: boot.fullPath + ':didLoad',
163+
});
155164
}
156165
}
157166
}
@@ -162,7 +171,12 @@ class Lifecycle extends EventEmitter {
162171
for (const boot of this[BOOTS]) {
163172
const willReady = boot.willReady && boot.willReady.bind(boot);
164173
if (willReady) {
165-
this[REGISTER_READY_CALLBACK](willReady, this.bootReady, 'Will Ready');
174+
this[REGISTER_READY_CALLBACK]({
175+
scope: willReady,
176+
ready: this.bootReady,
177+
timingKeyPrefix: 'Will Ready',
178+
scopeFullName: boot.fullPath + ':willReady',
179+
});
166180
}
167181
}
168182
}
@@ -230,13 +244,13 @@ class Lifecycle extends EventEmitter {
230244
}
231245
}
232246

233-
[REGISTER_READY_CALLBACK](scope, ready, timingKeyPrefix) {
247+
[REGISTER_READY_CALLBACK]({ scope, ready, timingKeyPrefix, scopeFullName }) {
234248
if (!is.function(scope)) {
235249
throw new Error('boot only support function');
236250
}
237251

238-
// get filename from stack
239-
const name = utils.getCalleeFromStack(true, 4);
252+
// get filename from stack if scopeFullName is undefined
253+
const name = scopeFullName || utils.getCalleeFromStack(true, 4);
240254
const timingkey = `${timingKeyPrefix} in ` + utils.getResolvedFilename(name, this.app.baseDir);
241255

242256
this.timing.start(timingkey);

lib/loader/context_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ClassLoader {
4141
class ContextLoader extends FileLoader {
4242

4343
/**
44-
* @constructor
44+
* @class
4545
* @param {Object} options - options same as {@link FileLoader}
4646
* @param {String} options.fieldClass - determine the field name of inject object.
4747
*/

lib/loader/egg_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const REQUIRE_COUNT = Symbol('EggLoader#requireCount');
1818
class EggLoader {
1919

2020
/**
21-
* @constructor
21+
* @class
2222
* @param {Object} options - options
2323
* @param {String} options.baseDir - the directory of application
2424
* @param {EggCore} options.app - Application instance

lib/loader/file_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const defaults = {
3232
class FileLoader {
3333

3434
/**
35-
* @constructor
35+
* @class
3636
* @param {Object} options - options
3737
* @param {String|Array} options.directory - directories to be loaded
3838
* @param {Object} options.target - attach the target object from loaded files

lib/loader/mixin/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
*
1616
* Will merge config.default.js 和 config.${env}.js
1717
*
18-
* @method EggLoader#loadConfig
18+
* @function EggLoader#loadConfig
1919
* @since 1.0.0
2020
*/
2121
loadConfig() {

lib/loader/mixin/custom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module.exports = {
5959
}
6060
const bootHook = this.requireFile(bootFilePath);
6161
if (is.class(bootHook)) {
62+
bootHook.prototype.fullPath = bootFilePath;
6263
// if is boot class, add to lifecycle
6364
this.lifecycle.addBootHook(bootHook);
6465
} else if (is.function(bootHook)) {

lib/loader/mixin/extend.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515

1616
/**
1717
* mixin Agent.prototype
18-
* @method EggLoader#loadAgentExtend
18+
* @function EggLoader#loadAgentExtend
1919
* @since 1.0.0
2020
*/
2121
loadAgentExtend() {
@@ -24,7 +24,7 @@ module.exports = {
2424

2525
/**
2626
* mixin Application.prototype
27-
* @method EggLoader#loadApplicationExtend
27+
* @function EggLoader#loadApplicationExtend
2828
* @since 1.0.0
2929
*/
3030
loadApplicationExtend() {
@@ -33,7 +33,7 @@ module.exports = {
3333

3434
/**
3535
* mixin Request.prototype
36-
* @method EggLoader#loadRequestExtend
36+
* @function EggLoader#loadRequestExtend
3737
* @since 1.0.0
3838
*/
3939
loadRequestExtend() {
@@ -42,7 +42,7 @@ module.exports = {
4242

4343
/**
4444
* mixin Response.prototype
45-
* @method EggLoader#loadResponseExtend
45+
* @function EggLoader#loadResponseExtend
4646
* @since 1.0.0
4747
*/
4848
loadResponseExtend() {
@@ -51,7 +51,7 @@ module.exports = {
5151

5252
/**
5353
* mixin Context.prototype
54-
* @method EggLoader#loadContextExtend
54+
* @function EggLoader#loadContextExtend
5555
* @since 1.0.0
5656
*/
5757
loadContextExtend() {
@@ -60,7 +60,7 @@ module.exports = {
6060

6161
/**
6262
* mixin app.Helper.prototype
63-
* @method EggLoader#loadHelperExtend
63+
* @function EggLoader#loadHelperExtend
6464
* @since 1.0.0
6565
*/
6666
loadHelperExtend() {
@@ -83,7 +83,7 @@ module.exports = {
8383

8484
/**
8585
* Loader app/extend/xx.js to `prototype`,
86-
* @method loadExtend
86+
* @function loadExtend
8787
* @param {String} name - filename which may be `app/extend/{name}.js`
8888
* @param {Object} proto - prototype that mixed
8989
* @since 1.0.0

lib/loader/mixin/middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
*
1717
* app.config.xx is the options of the middleware xx that has same name as config
1818
*
19-
* @method EggLoader#loadMiddleware
19+
* @function EggLoader#loadMiddleware
2020
* @param {Object} opt - LoaderOptions
2121
* @example
2222
* ```js

0 commit comments

Comments
 (0)