Skip to content

Commit

Permalink
allow this.callback to be used more flexibly
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Jun 24, 2010
1 parent 3d83502 commit 4e1da2f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
28 changes: 12 additions & 16 deletions lib/vows/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ this.Context = function (vow, ctx, env) {
this.emitter = null;
this.env = env || {};
this.env.context = this;
this.env.__defineGetter__('callback', function () {
that._callback = true;

return function (e, res) {
var args = Array.prototype.slice.call(arguments, 1);
var emit = function () {
if (e) { that.emitter.emit('error', e) }
else { that.emitter.emit.apply(that.emitter, ['success'].concat(args)) }
};
// If `this.callback` is called synchronously,
// the emitter will not have been set yet,
// so we defer the emition, that way it'll behave
// asynchronously.
if (that.emitter) { emit() }
else { process.nextTick(emit) }
this.env.callback = function (e, res) {
var args = Array.prototype.slice.call(arguments, 1);
var emit = function () {
if (e) { that.emitter.emit('error', e) }
else { that.emitter.emit.apply(that.emitter, ['success'].concat(args)) }
};
});
// If `this.callback` is called synchronously,
// the emitter will not have been set yet,
// so we defer the emition, that way it'll behave
// asynchronously.
if (that.emitter) { emit() }
else { process.nextTick(emit) }
};
this.name = vow.description;
this.title = [
ctx.title || '',
Expand Down
4 changes: 2 additions & 2 deletions lib/vows/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ this.Suite.prototype = new(function () {
if (typeof(topic) === 'function') {
// Run the topic, passing the previous context topics
topic = topic.apply(ctx.env, ctx.topics);

if (typeof(topic) === 'undefined') { ctx._callback = true }
}

// If this context has a topic, store it in `lastTopic`,
Expand All @@ -144,8 +146,6 @@ this.Suite.prototype = new(function () {
process.nextTick(function (val) {
return function () { ctx.emitter.emit("success", val) };
}(topic));
} else if (typeof(topic) !== "undefined" && !old) {
throw new(Error)("topic must not return anything when using `this.callback`.");
}
topic = ctx.emitter;
}
Expand Down
20 changes: 9 additions & 11 deletions test/vows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,10 @@ vows.describe("Vows").addBatch({
"A topic with callback-style async": {
"when successful": {
topic: function () {
function async(callback) {
process.nextTick(function () {
callback(null, "OK");
});
}
async(this.callback);
var that = this;
process.nextTick(function () {
that.callback(null, "OK");
});
},
"should work like an event-emitter": function (res) {
assert.equal(res, "OK");
Expand Down Expand Up @@ -225,18 +223,18 @@ vows.describe("Vows").addBatch({
"'A', with `this.foo = true`": {
topic: function () {
this.foo = true;
return this.foo;
return this;
},
"should have `this.foo` set to true": function (res) {
assert.equal(res, true);
assert.equal(res.foo, true);
}
},
"'B', with nothing set": {
topic: function () {
return this.foo;
return this;
},
"shouldn't have access to `this.foo`": function (res) {
assert.isUndefined(res);
"shouldn't have access to `this.foo`": function (e, res) {
assert.isUndefined(res.foo);
}
}
}
Expand Down

0 comments on commit 4e1da2f

Please sign in to comment.