Skip to content

Commit

Permalink
revert 3333598: fix memory leak when replacing arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Oct 27, 2015
1 parent c050ae5 commit 18c8629
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 46 deletions.
10 changes: 2 additions & 8 deletions src/observer/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var arrayMethods = Object.create(arrayProto)
}
var result = original.apply(this, args)
var ob = this.__ob__
var inserted, removed
var inserted
switch (method) {
case 'push':
inserted = args
Expand All @@ -38,17 +38,11 @@ var arrayMethods = Object.create(arrayProto)
break
case 'splice':
inserted = args.slice(2)
removed = result
break
case 'pop':
case 'shift':
removed = [result]
break
}
if (inserted) ob.observeArray(inserted)
if (removed) ob.unobserveArray(removed)
// notify change
ob.notify()
ob.dep.notify()
return result
})
})
Expand Down
43 changes: 7 additions & 36 deletions src/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,42 +91,7 @@ Observer.prototype.walk = function (obj) {
Observer.prototype.observeArray = function (items) {
var i = items.length
while (i--) {
var ob = Observer.create(items[i])
if (ob) {
(ob.parents || (ob.parents = [])).push(this)
}
}
}

/**
* Remove self from the parent list of removed objects.
*
* @param {Array} items
*/

Observer.prototype.unobserveArray = function (items) {
var i = items.length
while (i--) {
var ob = items[i] && items[i].__ob__
if (ob) {
ob.parents.$remove(this)
}
}
}

/**
* Notify self dependency, and also parent Array dependency
* if any.
*/

Observer.prototype.notify = function () {
this.dep.notify()
var parents = this.parents
if (parents) {
var i = parents.length
while (i--) {
parents[i].notify()
}
Observer.create(items[i])
}
}

Expand Down Expand Up @@ -217,6 +182,12 @@ function defineReactive (obj, key, val) {
if (childOb) {
childOb.dep.depend()
}
if (_.isArray(val)) {
for (var e, i = 0, l = val.length; i < l; i++) {
e = val[i]
e && e.__ob__ && e.__ob__.dep.depend()
}
}
}
return val
},
Expand Down
4 changes: 2 additions & 2 deletions src/util/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.set = function set (obj, key, val) {
return
}
ob.convert(key, val)
ob.notify()
ob.dep.notify()
if (ob.vms) {
var i = ob.vms.length
while (i--) {
Expand All @@ -51,7 +51,7 @@ exports.delete = function (obj, key) {
if (!ob) {
return
}
ob.notify()
ob.dep.notify()
if (ob.vms) {
var i = ob.vms.length
while (i--) {
Expand Down

0 comments on commit 18c8629

Please sign in to comment.