-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
63 lines (56 loc) · 1.49 KB
/
map.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module.exports = Map
module.exports = Map
var util = require('util')
var Deque = require('double-ended-queue')
var Rheo = require('./rheo')
var rheo = require('./')
var Document = require('./document')
util.inherits(Map, Rheo)
function Map (rheo_template, callback) {
Rheo.call(this, {objectMode: true})
var self = this
this.document = new Document()
this.html = ''
this.callback = callback
this.done_loading = false
this.rheo_template = rheo_template
.render()
.on('data', function (html) {
self.html += html
})
.once('end', function (html) {
if (html) self.html += html
self.done_loading = true
})
}
Map.prototype._make_rheo = function () {
return rheo(this._html)
}
Map.prototype._transform = function (data, enc, cb) {
var self = this
if (!this.done_loading) {
this.rheo_template.once('end', function () {
self.done_loading = true
self._transform(data, enc, cb)
})
} else {
var write_stream = rheo(this.html)
var read_stream = this.callback(write_stream, data)
var accum = new Deque()
read_stream.on('data', function (queue) {
accum.enqueue.apply(accum, queue.toArray())
})
read_stream.once('end', function (queue) {
if (queue) accum.enqueue.applye(accum, queue.toArray())
if (!accum.isEmpty()) {
var document = accum.peekFront().parent
document.move_children(self.document)
self.push(accum)
}
cb()
})
}
}
Map.prototype._flush = function (cb) {
cb()
}