-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.coffee
287 lines (268 loc) · 8.98 KB
/
watch.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
{arrayize, isString, isArray, isObject, isPlainObject, clone, getID, concat, assign} = require("./_helpers")
merger = require("./_merger")
watchStr = "__watch__"
instancesStr = "__instances__"
module.exports =
_name: "watch"
_prio: 1000
_v: 1
_mergers: [
merger.copy(source: "watch", target: "_watch")
merger.concat(source: "data")
]
_rebind: "$watch"
mixins: [
require "./path"
require "./parseFunction"
]
methods:
$watch:
__w: {}
getObj: (o) ->
if o.path?
if not (obj = @$watch.__w[o.path]) and (p = @__parents)?
i = p.length
while not obj and i
obj = p[--i].$watch.__w[o.path]
return obj
return null
setObj: (o) ->
o = (w = @$watch).sharedInit(o)
w.__w[o.path] = o if o.path?
return o
notify: (path) ->
unless (o = @$watch.getObj(path:path))
unless (o = @__parent?.$watch.getObj(path:path))
cwarn(true, "watch: couldn't notify #{path}")
return
o.notify(o.value)
parseCbs: (o, prop) -> o[prop] = arrayize(o[prop]).map @$parseFunction.bind(@)
parse: (obj,shouldClone) ->
unless isObject(obj)
obj = cbs: obj
else if shouldClone
obj = clone(obj)
if not obj.__parsed__
@$watch.parseCbs(obj, "cbs")
unless obj.initial == false
unless obj.initial
obj.initial = obj.cbs.slice()
else
@$watch.parseCbs(obj, "initial")
obj.__parsed__ = true
return obj
merge: (o1, o2) ->
concat o1.cbs, o2.cbs
if o2.initial
if o1.initial
concat o1.initial, o2.initial
else
o1.initial = o2.initial
if o2.parent?
o1.parent = o2.parent
@$watch.setupParent(o1) if o1.__init__
if o2.hasOwnProperty("value") and o2.value != o1.value
o1.parent[o1.name] = o2.value
getConfig: (o) ->
if not o.__configured__ and o.path? and (tmp = @_watch?[o.path])?
w = @$watch
c = w.parse(tmp, true)
w.merge(o,c)
o.__configured__ = true
init: (o) ->
w = @$watch
obj = w.getObj(o)
if obj?.__init__ # already initialized
w.merge(obj,o)
return obj
else # not initialized yet
w.getConfig(o)
if obj # but object already saved
w.merge(obj, o)
return w.sharedInit(obj)
else
return w.setObj(o)
sharedInit: (o) ->
unless o.__sInit__
o.__sInit__ = true
o.cDeps = []
o._taints = null
o.id = getID()
o.instance = @
o.checkComputed = ->
if (cd = window.__ceriDeps)? and not cd[o.id]? and (
(cai = window.__ceriActiveInstance) == null or
((ins = o.instance) == cai or (cai.__parents? and ~cai.__parents.indexOf(ins))))
o.cDeps.push(cd(o))
o.nullTaints()
return o
setupParent: (o) ->
parent = o.parent
name = o.name
if o.oldParent != parent
if (oldArr = o.watchArr)?
if ~(i = oldArr.indexOf(o))
oldArr.splice(i,1)
delete o.watchArr
o.oldParent = parent
if not (desc = Object.getOwnPropertyDescriptor parent, name)? or not desc.get
Object.defineProperty parent, name,
configurable: true
enumerable: true
get: ->
for obj in wrapper.objs
obj.checkComputed()
return wrapper.value
set: (newVal) ->
wrapper.value = newVal
for obj in wrapper.objs
obj.oldVal = obj.value
obj.value = newVal
# iterate over children
obj.instance.$watch.processNewValue(obj)
obj.notify(newVal, obj.oldVal)
unless o.watchArr
unless parent._isCeri
unless (wrapper = parent[watchStr])?
wrapper = {}
obj = {}
obj[watchStr] = value: wrapper
parent.__proto__ = Object.create parent.__proto__, obj
wrapper = wrapper[name] ?= {
objs: []
value: o.value
}
wrapper.objs.push o
else
wrapper =
objs: [o]
value: o.value
o.watchArr = wrapper.objs
return o
path: (o) ->
if o.parentPath? and o.name?
o.path = o.parentPath + "." + o.name
#cwarn !o.path?, "$watch.path requires path"
@$path.toNameAndParent(o)
@$watch.parse(o)
unless o.parent # save cbs for later
if (obj = @$watch.getObj(o))? # watch obj already saved
@$watch.merge(obj, o)
cwarn o.value?, "can't set #{o.value} on #{o.path} yet. Parent isn't setted yet"
return
else # save watch obj for later
@$watch.setObj(o)
else # can be appended
o = @$watch.init(o)
unless o.__init__ # needs setup
o.__init__ = true
o.value ?= o.parent[o.name]
o.nullTaints = -> o._taints = null
o.notify = (val, oldVal) ->
unless (taints = o._taints)?
taints = o._taints = o.cDeps.reduce(((h, c) -> c.getTaints(h,true)
), _taints: [])._taints
for cb in taints.map((taint) => taint())
cb()
for cb in o.cbs
cb.call(o.instance, val, oldVal, o)
return
@$watch.setupParent(o)
@$watch.processNewValue(o)
# triggering cbs
if o.initial
if o.value?
for cb in o.initial
cb.call(@,o.value)
else if o.dirty
o.notify(o.value)
o.initial = false
return o
processNewValue: (o) ->
child = o.value
parent = o.parent
isValidObj = (obj) -> obj? and isObject(obj) and not isArray(obj) and not obj?._isCeri
# wiring all cbs for all children
if isValidObj(child)
for own k, v of child
@$watch.path(parent: child, name:k, value:v, parentPath: o.path)
if o.oldVal and isValidObj(o.oldVal) # detect deleted props
for own k, v of o.oldVal
unless child.hasOwnProperty(k)
if (obj = @$watch.getObj(path: o.path+"."+k))?
obj.notify(null,v)
created: ->
# make data responsive
for fn in @data
obj = fn.call(@)
for k,v of obj
if (v2 = @[k])? and isPlainObject(v) and isPlainObject(v2)
v = assign v2,v
@$watch.path(parent:@, name: k, value: v, path: k)
test module.exports, (merge) ->
el = el2 = null
spy = (id) -> spy[id] ?= sinon.spy()
sharedObj = nested: "test40"
getWatchObj = (el, name) -> el.$watch.__w[name]
before (done) ->
el = makeEl merge
data: ->
someData: "test"
someData2: "test10"
someArray: ["test20"]
someObj:
someNestedProp: "test30"
sharedObj: sharedObj
watch:
someData:
initial: false
cbs: spy(1)
someData2: spy(2)
someArray: spy(3)
"someObj.someNestedProp": spy(5)
sharedObj: spy(6)
"sharedObj.nested": spy(6)
created: ->
@$watch.path path:"someData", cbs: spy(4)
el2 = makeEl merge
data: ->
sharedObj: sharedObj
watch:
sharedObj: spy(7)
"sharedObj.nested": spy(7)
el.$nextTick done
after ->
el.remove()
el2.remove()
it "should create data", ->
should.exist el.someData
el.someData.should.equal "test"
el.someData2.should.equal "test10"
el.someArray[0].should.equal "test20"
el.someObj.someNestedProp.should.equal "test30"
it "should work with initial", ->
spy(1).should.not.have.been.called
spy(2).should.have.been.calledWith "test10"
spy(3).should.have.been.calledOnce
spy(4).should.have.been.calledWith "test"
spy(5).should.have.been.calledWith "test30"
it "should notify on change", ->
el.someData = "test2"
spy(1).should.have.been.calledWith "test2", "test"
spy(4).should.have.been.calledWith "test2", "test"
it "should work with nested props", ->
el.someObj.someNestedProp = "test31"
spy(5).should.have.been.calledWith "test31","test30"
it "should work with nested shared objs", ->
spy(6).reset()
spy(7).reset()
el.sharedObj.nested = "test41"
el2.sharedObj.nested.should.equal "test41"
spy(6).should.have.been.calledWith "test41","test40"
spy(7).should.have.been.calledWith "test41","test40"
it "should work with shared objs", ->
spy(6).reset()
spy(7).reset()
el.sharedObj = nested: "test42"
spy(6).should.have.been.calledTwice
spy(7).should.not.have.been.called