Skip to content

Commit 4e1da2f

Browse files
author
cloudhead
committed
allow this.callback to be used more flexibly
1 parent 3d83502 commit 4e1da2f

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

lib/vows/context.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ this.Context = function (vow, ctx, env) {
77
this.emitter = null;
88
this.env = env || {};
99
this.env.context = this;
10-
this.env.__defineGetter__('callback', function () {
11-
that._callback = true;
12-
13-
return function (e, res) {
14-
var args = Array.prototype.slice.call(arguments, 1);
15-
var emit = function () {
16-
if (e) { that.emitter.emit('error', e) }
17-
else { that.emitter.emit.apply(that.emitter, ['success'].concat(args)) }
18-
};
19-
// If `this.callback` is called synchronously,
20-
// the emitter will not have been set yet,
21-
// so we defer the emition, that way it'll behave
22-
// asynchronously.
23-
if (that.emitter) { emit() }
24-
else { process.nextTick(emit) }
10+
this.env.callback = function (e, res) {
11+
var args = Array.prototype.slice.call(arguments, 1);
12+
var emit = function () {
13+
if (e) { that.emitter.emit('error', e) }
14+
else { that.emitter.emit.apply(that.emitter, ['success'].concat(args)) }
2515
};
26-
});
16+
// If `this.callback` is called synchronously,
17+
// the emitter will not have been set yet,
18+
// so we defer the emition, that way it'll behave
19+
// asynchronously.
20+
if (that.emitter) { emit() }
21+
else { process.nextTick(emit) }
22+
};
2723
this.name = vow.description;
2824
this.title = [
2925
ctx.title || '',

lib/vows/suite.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ this.Suite.prototype = new(function () {
123123
if (typeof(topic) === 'function') {
124124
// Run the topic, passing the previous context topics
125125
topic = topic.apply(ctx.env, ctx.topics);
126+
127+
if (typeof(topic) === 'undefined') { ctx._callback = true }
126128
}
127129

128130
// If this context has a topic, store it in `lastTopic`,
@@ -144,8 +146,6 @@ this.Suite.prototype = new(function () {
144146
process.nextTick(function (val) {
145147
return function () { ctx.emitter.emit("success", val) };
146148
}(topic));
147-
} else if (typeof(topic) !== "undefined" && !old) {
148-
throw new(Error)("topic must not return anything when using `this.callback`.");
149149
}
150150
topic = ctx.emitter;
151151
}

test/vows-test.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,10 @@ vows.describe("Vows").addBatch({
180180
"A topic with callback-style async": {
181181
"when successful": {
182182
topic: function () {
183-
function async(callback) {
184-
process.nextTick(function () {
185-
callback(null, "OK");
186-
});
187-
}
188-
async(this.callback);
183+
var that = this;
184+
process.nextTick(function () {
185+
that.callback(null, "OK");
186+
});
189187
},
190188
"should work like an event-emitter": function (res) {
191189
assert.equal(res, "OK");
@@ -225,18 +223,18 @@ vows.describe("Vows").addBatch({
225223
"'A', with `this.foo = true`": {
226224
topic: function () {
227225
this.foo = true;
228-
return this.foo;
226+
return this;
229227
},
230228
"should have `this.foo` set to true": function (res) {
231-
assert.equal(res, true);
229+
assert.equal(res.foo, true);
232230
}
233231
},
234232
"'B', with nothing set": {
235233
topic: function () {
236-
return this.foo;
234+
return this;
237235
},
238-
"shouldn't have access to `this.foo`": function (res) {
239-
assert.isUndefined(res);
236+
"shouldn't have access to `this.foo`": function (e, res) {
237+
assert.isUndefined(res.foo);
240238
}
241239
}
242240
}

0 commit comments

Comments
 (0)