Skip to content

Commit 17c3726

Browse files
committed
simplify observer self dep
1 parent b1c21a1 commit 17c3726

File tree

4 files changed

+16
-47
lines changed

4 files changed

+16
-47
lines changed

src/observer/array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var arrayMethods = Object.create(arrayProto)
4242
}
4343
if (inserted) ob.observeArray(inserted)
4444
// notify change
45-
ob.notify()
45+
ob.dep.notify()
4646
return result
4747
})
4848
})

src/observer/index.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require('./object')
1818
function Observer (value) {
1919
this.value = value
2020
this.active = true
21-
this.deps = []
21+
this.dep = new Dep()
2222
_.define(value, '__ob__', this)
2323
if (_.isArray(value)) {
2424
var augment = config.proto && _.hasProto
@@ -138,50 +138,27 @@ p.convert = function (key, val) {
138138
var ob = this
139139
var childOb = ob.observe(val)
140140
var dep = new Dep()
141-
if (childOb) {
142-
childOb.deps.push(dep)
143-
}
144141
Object.defineProperty(ob.value, key, {
145142
enumerable: true,
146143
configurable: true,
147144
get: function () {
148145
if (ob.active) {
149146
dep.depend()
150147
}
148+
if (childOb) {
149+
childOb.dep.depend()
150+
}
151151
return val
152152
},
153153
set: function (newVal) {
154154
if (newVal === val) return
155-
// remove dep from old value
156-
var oldChildOb = val && val.__ob__
157-
if (oldChildOb) {
158-
oldChildOb.deps.$remove(dep)
159-
}
160155
val = newVal
161-
// add dep to new value
162-
var newChildOb = ob.observe(newVal)
163-
if (newChildOb) {
164-
newChildOb.deps.push(dep)
165-
}
156+
childOb = ob.observe(newVal)
166157
dep.notify()
167158
}
168159
})
169160
}
170161

171-
/**
172-
* Notify change on all self deps on an observer.
173-
* This is called when a mutable value mutates. e.g.
174-
* when an Array's mutating methods are called, or an
175-
* Object's $add/$delete are called.
176-
*/
177-
178-
p.notify = function () {
179-
var deps = this.deps
180-
for (var i = 0, l = deps.length; i < l; i++) {
181-
deps[i].notify()
182-
}
183-
}
184-
185162
/**
186163
* Add an owner vm, so that when $add/$delete mutations
187164
* happen we can notify owner vms to proxy the keys and

src/observer/object.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _.define(
2121
return
2222
}
2323
ob.convert(key, val)
24-
ob.notify()
24+
ob.dep.notify()
2525
if (ob.vms) {
2626
var i = ob.vms.length
2727
while (i--) {
@@ -69,7 +69,7 @@ _.define(
6969
if (!ob || _.isReserved(key)) {
7070
return
7171
}
72-
ob.notify()
72+
ob.dep.notify()
7373
if (ob.vms) {
7474
var i = ob.vms.length
7575
while (i--) {

test/unit/specs/observer/observer_spec.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,18 @@ describe('Observer', function () {
6565
Observer.setTarget(watcher)
6666
obj.a.b
6767
Observer.setTarget(null)
68-
expect(watcher.deps.length).toBe(2)
68+
expect(watcher.deps.length).toBe(3) // obj.a + a.b + b
6969
obj.a.b = 3
7070
expect(watcher.update.calls.count()).toBe(1)
7171
// swap object
7272
var oldA = obj.a
7373
obj.a = { b: 4 }
7474
expect(watcher.update.calls.count()).toBe(2)
75-
expect(oldA.__ob__.deps.length).toBe(0)
76-
expect(obj.a.__ob__.deps.length).toBe(1)
7775
watcher.deps = []
7876
Observer.setTarget(watcher)
7977
obj.a.b
8078
Observer.setTarget(null)
81-
expect(watcher.deps.length).toBe(2)
79+
expect(watcher.deps.length).toBe(3)
8280
// set on the swapped object
8381
obj.a.b = 5
8482
expect(watcher.update.calls.count()).toBe(3)
@@ -87,8 +85,7 @@ describe('Observer', function () {
8785
it('observing $add/$set/$delete', function () {
8886
var obj = { a: 1 }
8987
var ob = Observer.create(obj)
90-
var dep = new Dep()
91-
ob.deps.push(dep)
88+
var dep = ob.dep
9289
spyOn(dep, 'notify')
9390
obj.$add('b', 2)
9491
expect(obj.b).toBe(2)
@@ -121,8 +118,7 @@ describe('Observer', function () {
121118
it('observing array mutation', function () {
122119
var arr = []
123120
var ob = Observer.create(arr)
124-
var dep = new Dep()
125-
ob.deps.push(dep)
121+
var dep = ob.dep
126122
spyOn(dep, 'notify')
127123
var objs = [{}, {}, {}]
128124
arr.push(objs[0])
@@ -142,8 +138,7 @@ describe('Observer', function () {
142138
it('array $set', function () {
143139
var arr = [1]
144140
var ob = Observer.create(arr)
145-
var dep = new Dep()
146-
ob.deps.push(dep)
141+
var dep = ob.dep
147142
spyOn(dep, 'notify')
148143
arr.$set(0, 2)
149144
expect(arr[0]).toBe(2)
@@ -159,8 +154,7 @@ describe('Observer', function () {
159154
var obj1 = arr[0]
160155
var obj2 = arr[1]
161156
var ob = Observer.create(arr)
162-
var dep = new Dep()
163-
ob.deps.push(dep)
157+
var dep = ob.dep
164158
spyOn(dep, 'notify')
165159
// remove by index
166160
arr.$remove(0)
@@ -185,8 +179,7 @@ describe('Observer', function () {
185179
var ob = Observer.create(obj)
186180
expect(obj.$add).toBeTruthy()
187181
expect(obj.$delete).toBeTruthy()
188-
var dep = new Dep()
189-
ob.deps.push(dep)
182+
var dep = ob.dep
190183
spyOn(dep, 'notify')
191184
obj.$add('b', 2)
192185
expect(dep.notify).toHaveBeenCalled()
@@ -196,8 +189,7 @@ describe('Observer', function () {
196189
expect(arr.$set).toBeTruthy()
197190
expect(arr.$remove).toBeTruthy()
198191
expect(arr.push).not.toBe([].push)
199-
var dep2 = new Dep()
200-
ob2.deps.push(dep2)
192+
var dep2 = ob2.dep
201193
spyOn(dep2, 'notify')
202194
arr.push(1)
203195
expect(dep2.notify).toHaveBeenCalled()

0 commit comments

Comments
 (0)