Skip to content

Commit

Permalink
fix async init has been already instantiated by get depended and do n…
Browse files Browse the repository at this point in the history
…ot call next in preInstantiateSingletons
  • Loading branch information
fantasyni committed Apr 24, 2014
1 parent 8ac3b2f commit 2dcac5e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
15 changes: 15 additions & 0 deletions examples/simple_async_init/bus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Bus = function() {}

Bus.prototype.init = function(cb) {
console.log('init Bus...');
setTimeout(function() {
console.log('Bus asyncInit setTimeout');
cb();
}, 500);
}

Bus.prototype.run = function() {
console.log('run bus...');
}

module.exports = Bus;
14 changes: 12 additions & 2 deletions examples/simple_async_init/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
"id": "car",
"func": "car",
"init": "init",
"order": 2
"order": 1,
"props": [{
"name": "wheel",
"ref": "wheel"
}]
}, {
"id": "wheel",
"func": "wheel",
"async": true,
"init": "init",
"order": 1
"order": 2
}, {
"id": "bus",
"func": "bus",
"async": true,
"init": "init",
"order": 3
}]
}
13 changes: 9 additions & 4 deletions lib/beans/beanFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,16 @@ BeanFactory.prototype.preInstantiateSingletons = function(cb) {
var beanDefinition = beanDefinitionOrderList[index++];
var beanName = beanDefinition.getBeanName();
if (beanDefinition.isAsyncInit()) {
var initCb = function() {
next();
if (!self.singletonBeanFactory.containsSingleton(beanName)) {
var initCb = function() {
next();
}
self.setInitCb(beanName, initCb);
self.getBean(beanName);
} else {
self.getBean(beanName);
next()
}
self.setInitCb(beanName, initCb);
self.getBean(beanName);
} else {
var initCb = function() {}
self.setInitCb(beanName, initCb);
Expand Down
2 changes: 1 addition & 1 deletion lib/beans/singletonBeanFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ SingletonBeanFactory.prototype.addSingleton = function(beanName, beanObject) {
* @api public
*/
SingletonBeanFactory.prototype.containsSingleton = function(beanName) {
return this.singletonObjects[beanName] !== null;
return !!this.singletonObjects[beanName];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bearcat",
"version": "0.1.1",
"version": "0.1.2",
"description": "a POJOs based application framework for node.js",
"main": "index.js",
"scripts": {
Expand Down
19 changes: 6 additions & 13 deletions test/context/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@ var ApplicationContext = require('../../lib/context/applicationContext');

// var car = applicationContext.getBean('car');
// var r = car.run();
var simplepath = require.resolve('../../examples/placeholder/context.json');
var simplepath = require.resolve('../../examples/simple_async_init/context.json');
var paths = [simplepath];
var path = require('path');

var configPath = path.dirname(simplepath) + '/config';

var applicationContext = new ApplicationContext(paths);
applicationContext.setConfigPath(configPath);
applicationContext.refresh();

var car = applicationContext.getBean('car');
var r = car.run();
applicationContext.on('finishRefresh', function() {
var car = applicationContext.getBean('car');
var r = car.run();

applicationContext.setEnv('prod');
applicationContext.refresh();
});

car = applicationContext.getBean('car');
r = car.run();
applicationContext.refresh();

0 comments on commit 2dcac5e

Please sign in to comment.