Skip to content

Commit

Permalink
review codes
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasyni committed Dec 24, 2014
1 parent a86e468 commit ed7e5e6
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 48 deletions.
23 changes: 13 additions & 10 deletions gruntfile.js
Expand Up @@ -5,6 +5,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');

var src = ['test/bearcat.js', 'test/beans/support/*.js', 'test/context/applicationContext.js', 'test/util/*.js',
'test/resource/*.js', 'test/aop/aop.js', 'test/aop/aop_annotation.js', 'test/aop/advisor.js', 'test/aop/aspect.js',
Expand All @@ -13,7 +14,6 @@ module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),
mochaTest: {
dot: {
Expand Down Expand Up @@ -41,26 +41,29 @@ module.exports = function(grunt) {
browserify: {
tests: {
src: src,
dest: './dist/browserified_tests.js'
dest: './test/browser/browserified_tests.js'
},
// This browserify build be used by users of the module. It contains a
// UMD (universal module definition) and can be used via an AMD module
// loader like RequireJS or by simply placing a script tag in the page,
// which registers mymodule as a global var. You can see examples for both
// usages in browser/example/index.html (script tag) and
// browser/example/index-require.html (RequireJS).
standalone: {
src: ['index.js'],
dest: './dist/bearcat.standalone.js',
dest: './dist/bearcat.js',
options: {
browserifyOptions: {
standalone: 'bearcat'
}
}
}
}
},
uglify: {
dist: {
files: {
'./dist/bearcat.min.js': ['<%= browserify.standalone.dest %>'],
}
}
},
});

// Default task.
grunt.registerTask('default', ['clean', 'mochaTest']);
grunt.registerTask('browser_test', ['browserify:tests']);
grunt.registerTask('package', ['browserify:standalone', 'uglify']);
};
1 change: 1 addition & 0 deletions index.js
@@ -1,2 +1,3 @@
require('./lib/util/requireUtil');

module.exports = require('./lib/bearcat');
5 changes: 2 additions & 3 deletions lib/beans/beanFactory.js
Expand Up @@ -86,7 +86,6 @@ BeanFactory.prototype.doGetBean = function(beanName) {
var beanDefinition = this.getBeanDefinition(beanName);

if (!beanDefinition) {
// throw new Error('')
logger.error('BeanFactory beanDefinition null for %j', beanName);
return null;
}
Expand Down Expand Up @@ -558,7 +557,7 @@ BeanFactory.prototype.applyBeanPostProcessorsBeforeInitialization = function(bea
for (var i = 0; i < beanPostProcessors.length; i++) {
var beanProcessor = beanPostProcessors[i];
result = beanProcessor.before(result, beanName);
if (result == null) {
if (!result) {
return result;
}
}
Expand All @@ -580,7 +579,7 @@ BeanFactory.prototype.applyBeanPostProcessorsAfterInitialization = function(bean
for (var i = 0; i < beanPostProcessors.length; i++) {
var beanProcessor = beanPostProcessors[i];
result = beanProcessor.after(result, beanName);
if (result == null) {
if (!result) {
return result;
}
}
Expand Down
65 changes: 64 additions & 1 deletion lib/beans/support/beanModule.js
Expand Up @@ -8,7 +8,7 @@
*
* Bearcat BeanModule
* modified from seajs module.js
* Copyright(c) 2014 fantasyni <fantasyni@163.com>
* Copyright(c) 2014 fantasyni <fantasyni@163.com>, http://seajs.org
* MIT Licensed
*/

Expand Down Expand Up @@ -36,6 +36,14 @@ var STATUS = {
ERROR: 5
}

/**
* BeanModule constructor function.
*
* @param {String} uri
* @param {Array} dependencies
* @param {Object} loader reference
* @api public
*/
var BeanModule = function(uri, deps, loader) {
this.uri = uri;
this.dependencies = deps || [];
Expand All @@ -48,6 +56,11 @@ var BeanModule = function(uri, deps, loader) {
this.status = STATUS.INIT;
}

/**
* BeanModule resolve dependencies uri.
*
* @api private
*/
BeanModule.prototype.resolve = function() {
var mod = this
var ids = mod.dependencies
Expand All @@ -62,6 +75,11 @@ BeanModule.prototype.resolve = function() {
return uris
}

/**
* BeanModule pass entry node into dependencies.
*
* @api private
*/
BeanModule.prototype.pass = function() {
var mod = this

Expand Down Expand Up @@ -93,6 +111,11 @@ BeanModule.prototype.pass = function() {
}
}

/**
* BeanModule load script files.
*
* @api private
*/
BeanModule.prototype.load = function() {
var mod = this;

Expand Down Expand Up @@ -140,6 +163,11 @@ BeanModule.prototype.load = function() {
}
}

/**
* BeanModule onload script file event callback.
*
* @api private
*/
BeanModule.prototype.onload = function() {
var mod = this
mod.status = STATUS.LOADED
Expand All @@ -156,12 +184,23 @@ BeanModule.prototype.onload = function() {
delete mod.entries
}

/**
* BeanModule error callback.
*
* @api private
*/
BeanModule.prototype.error = function() {
var mod = this
mod.onload()
mod.status = STATUS.ERROR
}

/**
* BeanModule fetch script files using async <script> or from webworker.
*
* @param {Object} request cache
* @api private
*/
BeanModule.prototype.fetch = function(requestCache) {
var mod = this
var uri = mod.uri
Expand Down Expand Up @@ -236,18 +275,42 @@ BeanModule.prototype.fetch = function(requestCache) {
}
}

/**
* BeanModule add entry.
*
* @param {Object} entry node
* @api public
*/
BeanModule.prototype.addEntry = function(entry) {
this.entries.push(entry);
}

/**
* BeanModule set remain number to be loaded.
*
* @param {Number} remain number
* @api public
*/
BeanModule.prototype.setRemain = function(remain) {
this.remain = remain;
}

/**
* BeanModule set loader.
*
* @param {Object} loader reference
* @api public
*/
BeanModule.prototype.setLoader = function(loader) {
this.loader = loader;
}

/**
* BeanModule get loader.
*
* @return {Object} loader reference
* @api public
*/
BeanModule.prototype.getLoader = function() {
return this.loader;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/bearcat.js
Expand Up @@ -14,6 +14,7 @@
var logger = require('pomelo-logger').getLogger('bearcat', 'app');
var ApplicationContext = require('./context/applicationContext');
var BeanFactory = require('./beans/beanFactory');
var Package = require('../package.json');
var Utils = require('./util/utils');

var Root;
Expand All @@ -38,7 +39,8 @@ var Bearcat = {
configLocations: null,
applicationContext: null,
state: STATE_NEW,
startTime: null
startTime: null,
version: Package.version
};

module.exports = Bearcat;
Expand Down Expand Up @@ -375,7 +377,4 @@ Bearcat.getRoute = function(beanName, fnName) {
var bean = Bearcat.getBean(beanName);

return bean[fnName].bind(bean);
// return function(req, res, next) {
// return bean[fnName](req, res, next);
// }
}
48 changes: 45 additions & 3 deletions lib/context/applicationContext.js
Expand Up @@ -25,9 +25,9 @@ var MetaUtil = require('../util/metaUtil');
var Utils = require('../util/utils');
var Path = RequireUtil.requirePath();
var Util = RequireUtil.requireUtil();
var DEFAULT_BASE = process.cwd();
var DEFAULT_LOAD_PATH = DEFAULT_BASE + "/config";
var DEFAULT_HOT_RELOAD_PATH = DEFAULT_BASE + "/hot";
var DEFAULT_BASE = "";
var DEFAULT_LOAD_PATH = "";
var DEFAULT_HOT_RELOAD_PATH = "";

var Root;
(function() {
Expand Down Expand Up @@ -63,12 +63,31 @@ module.exports = ApplicationContext;

Util.inherits(ApplicationContext, EventEmitter);

/**
* ApplicationContext init.
*
* @api public
*/
ApplicationContext.prototype.init = function() {
if (this.hasBeanFactory()) {
this.destroyBeans();
this.closeBeanFactory();
}

DEFAULT_BASE = process.cwd();

if (this.configLocations.length) {
var contextPath = this.configLocations[0];
DEFAULT_BASE = Path.dirname(contextPath);
}

DEFAULT_LOAD_PATH = DEFAULT_BASE + "/config";
DEFAULT_HOT_RELOAD_PATH = DEFAULT_BASE + "/hot";

this.cpath = DEFAULT_LOAD_PATH;
this.hpath = DEFAULT_HOT_RELOAD_PATH;
this.base = DEFAULT_BASE;

this.createBeanFactory();
}

Expand Down Expand Up @@ -314,6 +333,11 @@ ApplicationContext.prototype.refreshBeanFactory = function() {
}
}

/**
* ApplicationContext try async loading script files when in the frontend.
*
* @api private
*/
ApplicationContext.prototype.tryAsyncLoading = function(cb) {
if (!Utils.checkBrowser()) {
return cb();
Expand All @@ -327,6 +351,11 @@ ApplicationContext.prototype.tryAsyncLoading = function(cb) {
return this.doAsyncLoading(cb);
}

/**
* ApplicationContext internal do async loading script files when in the frontend.
*
* @api private
*/
ApplicationContext.prototype.doAsyncLoading = function(cb) {
var loadBeans = this.loadBeans;

Expand Down Expand Up @@ -656,10 +685,23 @@ ApplicationContext.prototype.module = function(func, context) {
return this.registerBeanMeta(meta);
}

/**
* ApplicationContext add startup loaded bean ids.
*
* @param {Array} startup loaded bean ids
* @api public
*/
ApplicationContext.prototype.use = function(ids) {
this.loadBeans = this.loadBeans.concat(ids);
}

/**
* ApplicationContext async load bean with bean ids.
*
* @param {Array} loaded bean ids
* @param {Function} callback function
* @api public
*/
ApplicationContext.prototype.async = function(ids, cb) {
var asyncScriptLoader = new AsyncScriptLoader();
return asyncScriptLoader.load(loadBeans, cb);
Expand Down

0 comments on commit ed7e5e6

Please sign in to comment.