Permalink
Browse files

now fully supporting streaming

  • Loading branch information...
1 parent 0d0f774 commit 1f73664950deda1d3672c0813502230f5cecaaba @pgte committed Sep 14, 2012
Showing with 73 additions and 8 deletions.
  1. +8 −5 index.js
  2. +1 −1 lib/select.js
  3. +1 −1 test/update.html
  4. +62 −0 test/update_streaming.js
  5. +1 −1 test/update_target.html
View
13 index.js
@@ -77,15 +77,14 @@ module.exports = function (opts) {
parser.write(buf.toString());
};
- stream.end = function (buf) {
+ stream.end = function (buf, next) {
if (buf !== undefined) stream.write(buf);
if (pos < parser.position) {
var s = buffered.slice(0, parser.position - pos);
- stream.raw(s, function() {
- stream.emit('end');
- });
- } else { stream.emit('end'); }
+ stream.raw(s);
+ }
+ parser.close();
};
@@ -96,6 +95,10 @@ module.exports = function (opts) {
});
};
+ parser.onend = function() {
+ stream.emit('end');
+ };
+
//
// Pausing and resuming
//
View
2 lib/select.js
@@ -139,7 +139,7 @@ module.exports = function (parser, opts) {
});
}, function() {
if (!updating) stream.emit('data', s);
- done();
+ if (done) { done(); }
});
};
View
2 test/update.html
@@ -27,4 +27,4 @@
</div>
<div class="g">EVERYTHING IS TERRIBLE</div>
</body>
-</html>
+</html>
View
62 test/update_streaming.js
@@ -0,0 +1,62 @@
+var test = require('tap').test;
+var trumpet = require('../');
+var fs = require('fs');
+var BufferedStream = require('bufferedstream');
+
+test('update', function (t) {
+ t.plan(2);
+ var html = fs.readFileSync(__dirname + '/update_target.html', 'utf8');
+
+ var tr = trumpet();
+ fs.createReadStream(__dirname + '/update.html').pipe(tr);
+
+ var spans = [ 'tacos', 'y', 'burritos' ];
+
+ tr.select('.b span', function (node) {
+ node.update(function (html) {
+ var stream = new BufferedStream();
+ stream.end(html.toUpperCase());
+ return stream;
+ });
+ });
+
+ tr.select('.c', function (node) {
+ node.update(function() {
+ var stream = new BufferedStream();
+ stream.end('---');
+ return stream;
+ });
+ });
+
+ tr.select('.d', function (node) {
+ node.remove();
+ });
+
+ tr.select('.e', function (node) {
+ node.remove();
+ });
+
+ tr.select('.f', function (node) {
+ node.replace(function() {
+ var stream = new BufferedStream();
+ stream.end('<b>NOTHING TO SEE HERE</b>');
+ return stream;
+ });
+ });
+
+ tr.select('.g', function (node) {
+ node.replace(function (html) {
+ t.equal(html, '<div class="g">EVERYTHING IS TERRIBLE</div>');
+ var stream = new BufferedStream();
+ stream.end('<blink>TERRIBLE</blink>');
+ return stream;
+ });
+ });
+
+ var data = '';
+ tr.on('data', function (buf) { data += buf });
+
+ tr.on('end', function () {
+ t.equal(data, html);
+ });
+});
View
2 test/update_target.html
@@ -18,4 +18,4 @@
<b>NOTHING TO SEE HERE</b>
<blink>TERRIBLE</blink>
</body>
-</html>
+</html>

0 comments on commit 1f73664

Please sign in to comment.