Permalink
Browse files

callback async and stream emitting preparation

  • Loading branch information...
1 parent 0f21de9 commit 68daeb6adc08783288227bb566a1357fb361ff4b @pgte committed Sep 14, 2012
Showing with 26 additions and 10 deletions.
  1. +25 −9 lib/node.js
  2. +1 −1 test/update.js
View
@@ -1,4 +1,5 @@
var ent = require('ent');
+var Stream = require('stream').Stream;
module.exports = function (tag, sel, level) {
return new Node(tag, sel, level);
@@ -17,6 +18,26 @@ function Node (tag, sel, level) {
this.sel = sel;
}
+function emit(value, out, done) {
+ var self = this;
+ var sel = self.sel;
+
+ if (value instanceof Stream) {
+ var stream = value;
+ stream.on('data', function(d) {
+ out.emit('data', d);
+ });
+ stream.on('end', function() {
+ sel.updating = false;
+ done();
+ });
+ } else {
+ out.emit('data', value);
+ sel.updating = false;
+ done();
+ }
+}
+
Node.prototype.html = function (cb) {
var p = this.p, sel = this.sel;
if (this.expired) expire();
@@ -53,7 +74,7 @@ Node.prototype.update = function (cb, attrs) {
final(function (s) {
var d = typeof cb === 'function' ? cb(html) : cb;
var data = '<' + self.name + attrText + '>' + d;
- this_.emit('data', data + s);
+ emit.call(self, data + s, this_, done);
});
sel.updating = false;
done();
@@ -68,27 +89,22 @@ Node.prototype.update = function (cb, attrs) {
p.buffered = '';
p.callback = function (html, final, done) {
- this.emit('data',
- typeof cb === 'function' ? cb(html) : cb
- );
- sel.updating = false;
- done();
+ emit.call(self, typeof cb === 'function' ? cb(html) : cb, this, done);
};
p.writes = 0;
sel.updating = true;
sel.pending.push(p);
};
Node.prototype.replace = function (cb) {
- var p = this.p, sel = this.sel;
+ var self = this, p = this.p, sel = this.sel;
if (this.expired) expire();
p.buffered = '';
p.callback = function (html, final, done) {
var this_ = this;
final(function (s) {
- var data = typeof cb === 'function' ? cb(html + s) : cb;
- this_.emit('data', data);
+ emit.call(self, typeof cb === 'function' ? cb(html + s) : cb, this_, done);
});
sel.updating = false;
done();
View
@@ -16,7 +16,7 @@ test('update', function (t) {
return html.toUpperCase();
});
});
-
+
tr.select('.c', function (node) {
node.update('---');
});

0 comments on commit 68daeb6

Please sign in to comment.