Skip to content

Commit 7bc3b77

Browse files
author
Seth Kinast
committed
Update streaming-incremental example to work with newer q
1 parent 3fc12ef commit 7bc3b77

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

examples/streaming-incremental/app.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ dust.onLoad = function(tmpl, cb) {
1313
var app = express();
1414

1515
var position;
16+
var context = {
17+
wait: function(chunk, context, bodies, params) {
18+
var delayMilliseconds = parseInt(params.delay, 10) * 1000;
19+
// Returning a Promise-- Dust will wait for the promise to resolve
20+
var promise = q(position++).delay(delayMilliseconds);
21+
promise.then(function(position) {
22+
console.log('Rendering', params.name, 'which started in position', position);
23+
});
24+
return promise;
25+
}
26+
};
1627

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

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

2841
app.get('/streaming', function(req, res) {
29-
position = 1;
30-
dust.stream('hello', {
31-
'wait': function(chunk, context, bodies, params) {
32-
var delayMilliseconds = parseInt(params.delay, 10) * 1000;
33-
// Returning a Promise-- Dust will wait for the promise to resolve
34-
return q(position++).delay(delayMilliseconds)
35-
.then(function(position) {
36-
console.log('Rendering', params.name, 'which started in position', position);
37-
});
38-
}
39-
}).pipe(res)
42+
dust.stream('hello', context).pipe(res)
4043
.on('end', function() {
4144
console.log('Done!');
4245
});
4346
});
4447

4548
app.get('/rendering', function(req, res) {
46-
position = 1;
47-
dust.render('hello', {
48-
'wait': function(chunk, context, bodies, params) {
49-
var delayMilliseconds = parseInt(params.delay, 10) * 1000;
50-
// Returning a Promise-- Dust will wait for the promise to resolve
51-
return q(position++).delay(delayMilliseconds)
52-
.then(function(position) {
53-
console.log('Rendering', params.name, 'which started in position', position);
54-
});
55-
}
56-
}, function(err, out) {
57-
console.log('Done!');
49+
dust.render('hello', context, function(err, out) {
5850
res.send(out);
51+
console.log('Done!');
5952
});
6053
});
6154

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{#wait name="one" delay=1}
2-
<p>Hello world! I'm delayed one second.
2+
<p>Hello world! I'm delayed one second.</p>
33
{/wait}
44

55
{#wait name="two" delay=2}
@@ -10,16 +10,19 @@
1010
<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>
1111
{/wait}
1212

13-
<p>
14-
{#wait name="fourA" delay=1}1{/wait}
15-
{#wait name="fourB" delay=1}2{/wait}
16-
{#wait name="fourC" delay=1}3{/wait}
17-
</p>
13+
<ul>
14+
{#wait name="fourA" delay=1}<li>1</li>{/wait}
15+
{#wait name="fourB" delay=1}<li>2</li>{/wait}
16+
{#wait name="fourC" delay=1}<li>3</li>{/wait}
17+
</ul>
1818

1919
{#wait name="five" delay=10}
2020
<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>
2121
{/wait}
2222

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

0 commit comments

Comments
 (0)