Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

module.exports = require('./lib/egg_loader');
module.exports.EggCore = require('./lib/egg');
module.exports.EggLoader = require('./lib/loader/egg_loader');
164 changes: 164 additions & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
'use strict';

const assert = require('assert');
const fs = require('fs');
const KoaApplication = require('koa');
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;

const DEPRECATE = Symbol('EggCore#deprecate');

class EggCore extends KoaApplication {

/**
* @constructor
* @param {Object} options - options
* @param {String} [options.baseDir=process.cwd()] - the directory of application
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里如果缩进的话应该不用 @param

- {String} [baseDir=process.cwd()]

不过这样也行

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看例子推荐用 .

* @param {String} [options.type=application|agent] - wheter it's running in app worker or agent worker
* @param {Object} [options.plugins] - custom plugins
* @since 1.0.0
*/
constructor(options) {
options = options || {};
options.baseDir = options.baseDir || process.cwd();
options.type = options.type || 'application';

assert(typeof options.baseDir === 'string', 'options.baseDir required, and must be a string');
assert(fs.existsSync(options.baseDir), `Directory ${options.baseDir} not exists`);
assert(fs.statSync(options.baseDir).isDirectory(), `Directory ${options.baseDir} is not a directory`);
assert(options.type === 'application' || options.type === 'agent', 'options.type should be application or agent');

super();

/**
* @member {Object} EggCore#options
* @since 1.0.0
*/
this._options = options;

/**
* logging for EggCore, avoid using console directly
* @member {Logger} EggCore#console
* @since 1.0.0
*/
this.console = new EggConsoleLogger();

/**
* @member {EggLoader} EggCore#loader
* @since 1.0.0
*/
const Loader = this[Symbol.for('egg#loader')];
assert(Loader, 'Symbol.for(\'egg#loader\') is required');
this.loader = new Loader({
baseDir: options.baseDir,
app: this,
plugins: options.plugins,
logger: this.console,
});

this._initReady();
}

/**
* alias to options.type
* @member {String}
* @since 1.0.0
*/
get type() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdoc

return this._options.type;
}

/**
* alias to options.baseDir
* @member {String}
* @since 1.0.0
*/
get baseDir() {
return this._options.baseDir;
}

/**
* @member {Function}
* @see https://npmjs.com/package/depd
* @since 1.0.0
*/
get deprecate() {
if (!this[DEPRECATE]) {
// require depd when get, `process.env.NO_DEPRECATION = '*'` should be use when run test everytime
this[DEPRECATE] = require('depd')('egg');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require 不能放顶部?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atian25 看注释说明。。。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require 的时候就读取了环境变量?今天在手机没喵源码

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require 一次就缓存了,之后里面的代码就不会执行了,可以看下 depd 的源码

}
return this[DEPRECATE];
}

/**
* name in package.json
* @member {String}
* @since 1.0.0
*/
get name() {
return this.loader.pkg.name;
}

/**
* alias to {EggCore#loader}
* @member {Object}
* @since 1.0.0
*/
get plugins() {
return this.loader.plugins;
}

/**
* alias to {EggCore#loader}
* @member {Config}
* @since 1.0.0
*/
get config() {
return this.loader.config;
}

/**
* close all listeners
* @member {Function}
* @since 1.0.0
*/
close() {
this.emit('close');
this.removeAllListeners();
}

/**
* @member {Function}
* @private
*/
_initReady() {
/**
* register an callback function that will be invoked when application is ready.
* @member {Function} EggCore#ready
* @since 1.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个使用说明?

*/

/**
* If a client starts asynchronously, you can register `readyCallback`,
* then the application will wait for the callback to ready
*
* It will log when the callback is not invoked after 10s
* @member {Function} EggCore#readyCallback
* @since 1.0.0
* @example
* ```js
* const done = app.readyCallback('mysql');
* mysql.ready(done);
* ```
*/
require('ready-callback')({ timeout: 10000 }).mixin(this);

this.on('ready_stat', data => {
this.console.info('[egg:core:ready_stat] end ready task %s, remain %j', data.id, data.remain);
}).on('ready_timeout', id => {
this.console.warn('[egg:core:ready_timeout] 10 seconds later %s was still unable to finish.', id);
});
}

}

module.exports = EggCore;
14 changes: 9 additions & 5 deletions lib/context_loader.js → lib/loader/context_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const assert = require('assert');
const is = require('is-type-of');
const Loader = require('./loader');
const FileLoader = require('./file_loader');
const classLoader = Symbol('classLoader');
const EXPORTS = Loader.EXPORTS;
const EXPORTS = FileLoader.EXPORTS;

class ClassLoader {

Expand All @@ -31,10 +31,10 @@ class ClassLoader {
}
}

class ContextLoader extends Loader {
class ContextLoader extends FileLoader {

constructor(options) {
assert(options.field, 'options.field is required');
assert(options.property, 'options.property is required');
assert(options.inject, 'options.inject is required');
const target = options.target = {};
if (options.fieldClass) {
Expand All @@ -44,7 +44,8 @@ class ContextLoader extends Loader {

const app = this.options.inject;

Object.defineProperty(app.context, options.field, {

Object.defineProperty(app.context, options.property, {
get() {
if (!this[classLoader]) {
this[classLoader] = getInstance(target, this);
Expand All @@ -67,6 +68,9 @@ function getInstance(values, ctx) {
} else {
instance = Class;
}
// Can't set property to primitive, so check again
} else if (is.primitive(values)) {
instance = values;
} else {
instance = new ClassLoader({ ctx, properties: values });
}
Expand Down
Loading