Skip to content

Commit

Permalink
fixed bug about end event
Browse files Browse the repository at this point in the history
fixed bug about end event
  • Loading branch information
miniflycn committed Oct 1, 2013
1 parent 330397b commit 2d6d85a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/vaild.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ module.exports = (function () {
var statusCode = res.statusCode;
if (statusCode === 200) {
that.emitter.emit('check', null, true);
res.on('data', function (data) {
that.emitter.emit('data', null, data);
});
res.on('end', function () {
that.emitter.emit('end');
that.url = undefined;
that.emitter = null;
});
} else if (300 < statusCode && statusCode < 304) {
that.fetch && req.abort();
var emitter = that.emitter
Expand All @@ -60,21 +68,19 @@ module.exports = (function () {
that.fetch && vaild.on('data', function (err, data) {
emitter.emit('data', err, data);
});
vaild.on('error', function (err) {
that.emitter.emit('error', err);
});
vaild.on('end', function () {
that.emitter.emit('end');
});
} else {
that.emitter.emit('check', null, false);
}
res.on('error', function (err) {
req.abort();
that.emitter.emit('data', err);
});
res.on('data', function (data) {
that.emitter.emit('data', null, data);
});
res.on('end', function () {
that.emitter.emit('end');
that.url = undefined;
that.emitter = null;
});
});
req.on('error', function (err) {
that.fetch && req.abort();
Expand Down
16 changes: 16 additions & 0 deletions test/vaild.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,20 @@ describe('vaild', function () {
assert.deepEqual(m.emitter, null);
assert.deepEqual(m.fetch, undefined);
});

it('should emit end event when finish validity detection', function (done) {
var step = 0
, v = vaild('http://localhost:7777/move').on('check', function (err, vaild) {
step.should.equal(0);
step = 1;
vaild.should.be.true;
}).on('data', function (err, data) {
step.should.equal(1);
step = 2;
data.toString().should.equal('hello world!');
}).on('end', function () {
step.should.equal(2);
done();
});
});
});

0 comments on commit 2d6d85a

Please sign in to comment.