-
Notifications
You must be signed in to change notification settings - Fork 1
/
cell.coffee
41 lines (40 loc) · 1.01 KB
/
cell.coffee
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
export default ($F)->###!IMPORT###
do($F)->###!SCRIPT###
module.exports = ($F)->###!REQUIRE###
$F::cell = (params...)->
current = @
value = null
changed = false
destroyed = false
do recalc = ->
throw new current.Error() if destroyed
changed = false
value = current (for param in params then do param)...
res = @constructor.prototype(-> unless changed then value else do recalc)
.catch (e)->
changed = true
throw e
for param in params then param.relateds.push res
res.relateds = []
res.recalc = recalc
res.markChange = ->
return res if changed
changed = true
for related in res.relateds
do related.markChange
res
res.destroy = ->
for param in params
param.relateds = (item for item in param.relateds when item != res)
res
$F.cell = (value)->
res = @prototype (newValue)->
if arguments.length
if value != newValue
value = newValue
for related in res.relateds
do related.markChange
value
res.relateds = []
res.recalc = -> value
res