/
index.js
65 lines (52 loc) · 1.19 KB
/
index.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
63
64
65
var through = require('through')
module.exports = tweakable
function tweakable(create, update, remove, equal) {
create = create || _create
update = update || _update
remove = remove || _remove
equal = equal || _equal
return function createStream(parent) {
var stream = through(write, end)
var el = create.call(stream, parent)
var value = null
stream.el = el
stream.value = value
stream.on('data', function(d) {
var eq = equal(d, value)
stream.value = value = d
if (!eq) stream.emit('changed', d)
})
return stream
function write(d) {
update.call(stream, d)
stream.queue(d)
}
function end() {
remove.call(stream)
stream.queue(null)
}
}
}
function _equal(a, b) {
return a === b
}
function _create(parent) {
var input = document.createElement('input')
var stream = this
parent.appendChild(input)
;['change'
, 'keyup'
].map(function(name) {
input.addEventListener(name, function(e) {
stream.queue(input.value)
})
})
return input
}
function _update(data) {
this.el.value = data
}
function _remove() {
var el = this.el
if (el.parentNode) el.parentNode.removeChild(el)
}