/
clock.js
43 lines (35 loc) · 872 Bytes
/
clock.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
var NoR = require('../lib/NoR.js')
var clock = NoR(function(signal, enable){}, function(time){
var timeout, enable = this.enable, signal = this.signal
tick = function() {
signal(! signal() )
timeout = setTimeout(tick, time())
}
enable.subscribe(function(enable){
if(enable) {
tick()
} else if(timeout){
clearTimeout(timeout)
}
})
})
var counter = NoR(function(clock, count){
count( count() + 1 )
}, function(){
// mark the count cell so it doent
// trigger counter execution again
this.count.unsubscribe(this)
})
var c = clock(true, false, 600)
var n = counter(c.signal, 0)
c.signal.subscribe(function(high){
process.stdout.write(high && '/' || '\\')
process.stdout.write(high && '‾' || '_')
})
n.count.subscribe(function(time){
if(time > 9) {
console.log("")
c.enable(false)
}
})
c.enable(true)