|
1 |
| -var Readable = require('stream').Readable; |
2 |
| -var inherits = require('util').inherits; |
3 |
| -var co = require('co'); |
| 1 | +'use strict'; |
4 | 2 |
|
5 |
| -module.exports = View |
| 3 | +const Readable = require('stream').Readable; |
| 4 | +const co = require('co'); |
6 | 5 |
|
7 |
| -inherits(View, Readable); |
8 | 6 |
|
9 |
| -function View(context) { |
10 |
| - Readable.call(this, {}); |
| 7 | +module.exports = class View extends Readable { |
11 | 8 |
|
12 |
| - // render the view on a different loop |
13 |
| - co.call(this, this.render).catch(context.onerror); |
14 |
| -} |
| 9 | + constructor(context) { |
| 10 | + super(); |
| 11 | + |
| 12 | + // render the view on a different loop |
| 13 | + co.call(this, this.render).catch(context.onerror); |
| 14 | + } |
| 15 | + |
| 16 | + _read() {} |
| 17 | + |
| 18 | + *render() { |
| 19 | + // push the <head> immediately |
| 20 | + this.push('<!DOCTYPE html><html><head><title>Hello World</title></head>'); |
15 | 21 |
|
16 |
| -View.prototype._read = function () {}; |
17 |
| - |
18 |
| -View.prototype.render = function* () { |
19 |
| - // push the <head> immediately |
20 |
| - this.push('<!DOCTYPE html><html><head><title>Hello World</title></head>'); |
21 |
| - |
22 |
| - // render the <body> on the next tick |
23 |
| - var body = yield function (done) { |
24 |
| - setImmediate(function () { |
25 |
| - done(null, '<p>Hello World</p>'); |
26 |
| - }); |
27 |
| - }; |
28 |
| - this.push('<body>' + body + '</body>'); |
29 |
| - |
30 |
| - // close the document |
31 |
| - this.push('</html>'); |
32 |
| - // end the stream |
33 |
| - this.push(null); |
34 |
| -}; |
| 22 | + // render the <body> on the next tick |
| 23 | + const body = yield done => { |
| 24 | + setImmediate(() => done(null, '<p>Hello World</p>')); |
| 25 | + }; |
| 26 | + this.push('<body>' + body + '</body>'); |
| 27 | + |
| 28 | + // close the document |
| 29 | + this.push('</html>'); |
| 30 | + |
| 31 | + // end the stream |
| 32 | + this.push(null); |
| 33 | + }; |
| 34 | +} |
0 commit comments