Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added timing to callback for init

  • Loading branch information...
commit 46a517a7d2c189c52aa788ce291a96054bc0951b 1 parent 3c12426
Scott Burch authored
Showing with 27 additions and 12 deletions.
  1. +25 −11 couch-ar.js
  2. +2 −1  test/spec/TestUserSpec.js
36 couch-ar.js
View
@@ -1,3 +1,5 @@
+var domainLoadedCount = 0;
+
var cradle = require('cradle');
var fs = require('fs');
var Base = require('./Base');
@@ -39,13 +41,27 @@ exports.init = function(config, callback) {
});
function initDomainConstructors() {
- fs.readdirSync(config.root).forEach(function(filename) {
+ var timeout = setTimeout(function() {
+ callback(db);
+ },60000);
+
+ var filenames = fs.readdirSync(config.root);
+ filenames.forEach(function(filename) {
/\.js$/.test(filename) && require(config.root + '/' + filename)
});
- callback(db);
+
+ (function checkLoaded() {
+ if(domainLoadedCount === filenames.length) {
+ clearTimeout(timeout);
+ callback(db);
+ } else {
+ setTimeout(checkLoaded, 100);
+ }
+ }());
}
}
+
/**
* Create a domain constructor. Use this in each domain file
*/
@@ -65,14 +81,14 @@ exports.create = function(name, config, constr) {
}
// Run all of the creators
- addCreateMethod(function() {
- addViews(function() {
- addFinders(function() {
- });
- });
+ addCreateMethod();
+ addViews(function() {
+ addFinders();
+ domainLoadedCount++;
});
+
factory.addView = function() {
addView.apply(factory, arguments);
}
@@ -81,7 +97,7 @@ exports.create = function(name, config, constr) {
exports[config.dbName] = exports[config.dbName] || {};
return exports[name] = exports[config.dbName][name] = factory;
- function addFinders(callback) {
+ function addFinders() {
for (prop in config.properties) {
addFinders(prop);
}
@@ -90,7 +106,6 @@ exports.create = function(name, config, constr) {
}
addFinders('id');
addList();
- callback();
function addFinders(finderName) {
@@ -206,7 +221,7 @@ exports.create = function(name, config, constr) {
/**
* Add the create() static method to the factory
*/
- function addCreateMethod(callback) {
+ function addCreateMethod() {
factory.create = function(props) {
var obj = factory();
for (var n in props) {
@@ -217,7 +232,6 @@ exports.create = function(name, config, constr) {
obj.rev = obj.rev || props._rev || props.rev;
return obj;
}
- callback && callback();
}
}
3  test/spec/TestUserSpec.js
View
@@ -29,7 +29,8 @@ describe('couch-ar', function () {
},
function () {
// delay so that everything can be setup
- setTimeout(asyncSpecDone, 500);
+// setTimeout(asyncSpecDone, 500);
+ asyncSpecDone();
}
);
asyncSpecWait();
Please sign in to comment.
Something went wrong with that request. Please try again.