Comparing version 0.0.0 to 0.0.1
25
index.js
@@ -15,3 +15,2 @@ "use strict"; | ||
function Hostess() { | ||
this._done = {}; | ||
this._queue = {}; | ||
@@ -27,2 +26,4 @@ events.EventEmitter.call(this); | ||
return this.on('done', deps); | ||
} else if (name === 'set') { | ||
return this[deps] = callback; | ||
} | ||
@@ -42,2 +43,4 @@ | ||
deps: deps, | ||
called: false, | ||
done: false, | ||
callback: callback | ||
@@ -50,3 +53,3 @@ } | ||
for (var i = 0; i < deps.length; ++i) { | ||
if (this._done[deps[i]]) ++met; | ||
if (this._queue[deps[i]] && this._queue[deps[i]].done) ++met; | ||
} | ||
@@ -57,6 +60,19 @@ return met === deps.length; | ||
Hostess.prototype.exec = function(name) { | ||
var self = this; | ||
var item = this._queue[name]; | ||
if (!item.callback) return; | ||
if (item.called) return; | ||
if (!this.depsMet(item.deps)) return; | ||
if (this.debug) { | ||
setTimeout(function() { | ||
if (self._queue[name].done) return; | ||
console.log(name + ' timed out'); | ||
}, this.timeout || 500); | ||
console.log('calling ' + name); | ||
} | ||
item.called = true; | ||
if (item.callback.length > 0) { | ||
@@ -79,5 +95,4 @@ // async | ||
return function(err) { | ||
self._queue[name].done = true; | ||
if (err) return self.emit('error', err); | ||
self._queue[name] = {}; | ||
self._done[name] = true; | ||
self.checkAllForReady(); | ||
@@ -90,3 +105,3 @@ } | ||
for (var name in this._queue) { | ||
if (!this._done[name]) ++count; | ||
if (!this._queue[name].done) ++count; | ||
this.exec(name); | ||
@@ -93,0 +108,0 @@ } |
{ | ||
"name": "hostess", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "async by depencency", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -73,8 +73,5 @@ Hostess is a nice girl that will run your async code based on | ||
host('done', function() { | ||
// nothing left in the queue | ||
}); | ||
host('no deps', function() { | ||
// done will be fired after this | ||
setTimeout(next, 100); | ||
}); | ||
@@ -86,5 +83,34 @@ | ||
host('milk', function() { | ||
host('milk', function(next) { | ||
// won't fire done | ||
setTimeout(next, 100); | ||
}); | ||
host('done', function() { | ||
// will be called when there is nothing in the queue | ||
}); | ||
``` | ||
## Debugging | ||
```javascript | ||
var hostess = require('hostess') | ||
, host = hostess() | ||
host('set', 'debug', true); | ||
host('set', 'timeout', 1000); // defaults to 500ms | ||
host('dep', function(next) { | ||
setTimeout(next, 500); | ||
}); | ||
host('buggy', [ 'dep' ], function(next) { | ||
// forgot to call next(); | ||
}); | ||
/* Outputs: | ||
calling dep | ||
calling buggy | ||
buggy timed out | ||
*/ | ||
``` |
@@ -149,3 +149,3 @@ var hostess = require('./index') | ||
host('rook', ['bishop', 'knight'], function(next) { process.nextTick(next); }); | ||
host('bishop', ['pawn'], function(next) { process.nextTick(next); }); | ||
host('bishop', ['pawn'], function() {}); | ||
host('knight', ['pawn'], function(next) { process.nextTick(next); }); | ||
@@ -152,0 +152,0 @@ host('pawn', function(next) { process.nextTick(next); }); |
10414
283
115