-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.js
68 lines (62 loc) · 1.28 KB
/
cell.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
66
67
68
define(["./name"], function (name) {
"use strict";
var _value = new name.Name()
var _events = new name.Name()
function call(a, f) {
return f.apply(null, a.map(function (x) {
return x.get()
}))
}
function Const(x) {
this[_value] = x
this[_events] = []
}
Const.prototype.get = function () {
return this[_value]
}
function Value(x) {
Const.call(this, x)
}
Value.prototype = new Const()
Value.prototype.set = function (v) {
this[_value] = v
this[_events].forEach(function (f) {
f()
})
}
function On(a, f) {
Value.call(this)
var self = this
function onset() {
self.set(call(a, f))
}
// TODO binds duplicates if called multiple times
this.bind = function () {
onset()
a.forEach(function (x) {
x[_events].push(onset)
})
}
this.unbind = function () {
a.forEach(function (x) {
var i = x[_events].indexOf(onset)
if (i !== -1) {
x[_events].splice(i, 1)
}
})
}
this.bind()
}
On.prototype = new Value()
return Object.freeze({
constant: function (x) {
return new Const(x)
},
value: function (x) {
return new Value(x)
},
on: function (a, f) {
return new On(a, f)
}
})
})