Skip to content

Commit f5677f8

Browse files
author
Seth Kinast
committedMar 30, 2015
Clarify a few examples and add a new explicitly-incremental streaming example
1 parent 5d44883 commit f5677f8

File tree

9 files changed

+124
-3
lines changed

9 files changed

+124
-3
lines changed
 

‎examples/basic-browser/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
To run:
1+
To install:
22

3-
bower install && ../../bin/dustc views/*.dust --output=lib/compiled.js
3+
npm install && bower install
4+
5+
To (re)compile templates:
6+
7+
../../bin/dustc views/*.dust --output=lib/compiled.js
48

59
Then load `index.html` in your browser.
610

‎examples/streaming-hoffman/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ The example proxies dustjs.com through your local server (after an artificial de
88

99
Hoffman handles template loading and will reload and recompile templates each time they are loaded until you turn view caching on. For production, you'd want to turn caching on.
1010

11+
Note that Hoffman doesn't always use the most up-to-date version of Dust, so you might need to manually upgrade it by editing its `package.json` file.
12+
1113
More info on Hoffman: https://www.npmjs.com/package/hoffman

‎examples/streaming-hoffman/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "Use Hoffman to enhance Dust streaming with Express",
55
"main": "app.js",
66
"dependencies": {
7-
"dustjs-linkedin": "^2.6.1",
87
"express": "^4.12.3",
98
"hoffman": "^0.1.4",
109
"request": "^2.53.0"
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
To run:
2+
3+
npm install && npm start
4+
5+
This example explicitly shows how Dust streams and flushes chunks to the browser.
6+
7+
If you use Dust's rendering interface, the callback is invoked once the entire template is done rendering. Therefore, your template will render as slowly as your slowest asynchronous call. Notice how the `/rendering` endpoint does not load for 10 seconds, even though you can see in the console that Dust is working on rendering the async blocks.
8+
9+
If you use Dust's streaming interface, Dust flushes pieces of your template to the browser as soon as they are done. However, Dust can't stream out-of-order, because browsers don't know how to interpret responses that are not in-order. This means that even if a chunk is done early, it has to wait for all previous chunks to complete.

‎examples/streaming-incremental/app.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var fs = require('fs'),
2+
path = require('path'),
3+
express = require('express'),
4+
dust = require('dustjs-linkedin'),
5+
q = require('q');
6+
7+
// Define a custom `onLoad` function to tell Dust how to load templates
8+
dust.onLoad = function(tmpl, cb) {
9+
fs.readFile(path.join('./views', path.resolve('/', tmpl + '.dust')),
10+
{ encoding: 'utf8' }, cb);
11+
};
12+
13+
var app = express();
14+
15+
var position;
16+
17+
app.get('/', function (req, res) {
18+
dust.render('index', {}, function(err, out) {
19+
res.send(out);
20+
});
21+
});
22+
23+
app.use(function(req, res, next) {
24+
console.log('\n\nBeginning the render of', req.path);
25+
next();
26+
});
27+
28+
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)
40+
.on('end', function() {
41+
console.log('Done!');
42+
});
43+
});
44+
45+
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!');
58+
res.send(out);
59+
});
60+
});
61+
62+
app.listen(3000, function () {
63+
console.log('Using dust version', dust.version);
64+
console.log('Visit http://localhost:3000');
65+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "dustjs-example-streaming-incremental",
3+
"version": "1.0.0",
4+
"description": "Explicitly show how Dust streams a page incrementally",
5+
"main": "app.js",
6+
"dependencies": {
7+
"dustjs-linkedin": "^2.6.1",
8+
"express": "^4.12.3",
9+
"q": "^1.2.0"
10+
},
11+
"scripts": {
12+
"start": "node app.js",
13+
"test": "echo \"Error: no test specified\" && exit 1"
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{#wait name="one" delay=1}
2+
<p>Hello world! I'm delayed one second.
3+
{/wait}
4+
5+
{#wait name="two" delay=2}
6+
<p>When Dust streams a template with many async blocks, it flushes them to the browser in-order, as fast as it can.</p>
7+
{/wait}
8+
9+
{#wait name="three" delay=5}
10+
<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>
11+
{/wait}
12+
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>
18+
19+
{#wait name="five" delay=10}
20+
<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>
21+
{/wait}
22+
23+
{#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>
25+
{/wait}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="/streaming">Streaming</a> vs <a href="/rendering">Rendering</a> the same template

‎examples/streaming/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var fs = require('fs'),
66

77
dust.config.whitespace = true;
88

9+
// Define a custom `onLoad` function to tell Dust how to load templates
910
dust.onLoad = function(tmpl, cb) {
1011
fs.readFile(path.join('./views', path.resolve('/', tmpl + '.dust')),
1112
{ encoding: 'utf8' }, cb);

0 commit comments

Comments
 (0)
Please sign in to comment.