Skip to content

Commit

Permalink
Update streaming-incremental example to work with newer q
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Kinast committed Jun 2, 2016
1 parent 3fc12ef commit 7bc3b77
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
39 changes: 16 additions & 23 deletions examples/streaming-incremental/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ dust.onLoad = function(tmpl, cb) {
var app = express();

var position;
var context = {
wait: function(chunk, context, bodies, params) {
var delayMilliseconds = parseInt(params.delay, 10) * 1000;
// Returning a Promise-- Dust will wait for the promise to resolve
var promise = q(position++).delay(delayMilliseconds);
promise.then(function(position) {
console.log('Rendering', params.name, 'which started in position', position);
});
return promise;
}
};

app.get('/', function (req, res) {
dust.render('index', {}, function(err, out) {
Expand All @@ -22,40 +33,22 @@ app.get('/', function (req, res) {

app.use(function(req, res, next) {
console.log('\n\nBeginning the render of', req.path);
console.log('Read hello.dust to see the block names and the order in which they appear.');
position = 1;
next();
});

app.get('/streaming', function(req, res) {
position = 1;
dust.stream('hello', {
'wait': function(chunk, context, bodies, params) {
var delayMilliseconds = parseInt(params.delay, 10) * 1000;
// Returning a Promise-- Dust will wait for the promise to resolve
return q(position++).delay(delayMilliseconds)
.then(function(position) {
console.log('Rendering', params.name, 'which started in position', position);
});
}
}).pipe(res)
dust.stream('hello', context).pipe(res)
.on('end', function() {
console.log('Done!');
});
});

app.get('/rendering', function(req, res) {
position = 1;
dust.render('hello', {
'wait': function(chunk, context, bodies, params) {
var delayMilliseconds = parseInt(params.delay, 10) * 1000;
// Returning a Promise-- Dust will wait for the promise to resolve
return q(position++).delay(delayMilliseconds)
.then(function(position) {
console.log('Rendering', params.name, 'which started in position', position);
});
}
}, function(err, out) {
console.log('Done!');
dust.render('hello', context, function(err, out) {
res.send(out);
console.log('Done!');
});
});

Expand Down
17 changes: 10 additions & 7 deletions examples/streaming-incremental/views/hello.dust
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{#wait name="one" delay=1}
<p>Hello world! I'm delayed one second.
<p>Hello world! I'm delayed one second.</p>
{/wait}

{#wait name="two" delay=2}
Expand All @@ -10,16 +10,19 @@
<p>This block has a longer delay (5 seconds), so the blocks that finish below it will all be flushed as soon as it's ready.</p>
{/wait}

<p>
{#wait name="fourA" delay=1}1{/wait}
{#wait name="fourB" delay=1}2{/wait}
{#wait name="fourC" delay=1}3{/wait}
</p>
<ul>
{#wait name="fourA" delay=1}<li>1</li>{/wait}
{#wait name="fourB" delay=1}<li>2</li>{/wait}
{#wait name="fourC" delay=1}<li>3</li>{/wait}
</ul>

{#wait name="five" delay=10}
<p>This one takes a long time to render, but it's at the bottom of the page so you get the rest of the page first.</p>
{/wait}

{#wait name="six" delay=0}
<p>This block had no delay, but it couldn't be sent to the browser until everything above it completed.</p>
<blockquote>
<p>This block had no delay, so it was rendered first (check the console).
<p>However, it couldn't be sent to the browser until everything above it completed.</p>
</blockquote>
{/wait}

0 comments on commit 7bc3b77

Please sign in to comment.