-
Notifications
You must be signed in to change notification settings - Fork 744
/
view.js
34 lines (24 loc) · 712 Bytes
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
const Readable = require('stream').Readable;
const co = require('co');
module.exports = class View extends Readable {
constructor(context) {
super();
// render the view on a different loop
co.call(this, this.render).catch(context.onerror);
}
_read() {}
*render() {
// push the <head> immediately
this.push('<!DOCTYPE html><html><head><title>Hello World</title></head>');
// render the <body> on the next tick
const body = yield done => {
setImmediate(() => done(null, '<p>Hello World</p>'));
};
this.push('<body>' + body + '</body>');
// close the document
this.push('</html>');
// end the stream
this.push(null);
};
};