Skip to content

Commit

Permalink
delegate中出现2+次时,删除操作报错 #49
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Aug 19, 2018
1 parent a45fd0f commit 9960ea2
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 6 deletions.
4 changes: 4 additions & 0 deletions build/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var res;
function delegate(e, json, top) {
var elem = e.target;
var vd = _hash2.default.get(elem.getAttribute('migi-uid'));
// 可能已经被删除回收,无需继续执行 see https://github.com/migijs/migi/issues/49
if (!vd.__dom) {
return [false];
}
// 点击根元素忽略;不存在也忽略,比如非vd添加的dom没有migi-uid
if (vd == top || !vd) {
return [false];
Expand Down
8 changes: 8 additions & 0 deletions build/domDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ function diffChild(parent, elem, ovd, nvd, record) {
break;
}
}
// 遍历孩子vd回收
_util2.default.getAllChildrenElement(ovd).forEach(function (item) {
_cachePool2.default.add(item.__destroy());
});
// 缓存对象池
_cachePool2.default.add(ovd.__destroy());
record.state = _type2.default.DOM_TO_TEXT;
Expand Down Expand Up @@ -655,6 +659,10 @@ function diffChild(parent, elem, ovd, nvd, record) {
nvd.style = parent.style;
nvd.emit(_Event2.default.DOM);
_hash2.default.set(nvd);
// 遍历孩子vd回收
_util2.default.getAllChildrenElement(ocp ? ovd.__virtualDom : ovd).forEach(function (item) {
_cachePool2.default.add(item.__destroy());
});
// 缓存对象池
_cachePool2.default.add(ovd.__destroy());
break;
Expand Down
18 changes: 17 additions & 1 deletion build/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ function arrFirst(arr) {
return arr;
}

function getAllChildrenElement(vd) {
var res = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

vd.__children.forEach(function (item) {
if (item instanceof migi.VirtualDom) {
getAllChildrenElement(item, res);
res.push(item);
} else if (item instanceof migi.Component) {
getAllChildrenElement(item.__virtualDom, res);
res.push(item.__virtualDom);
}
});
return res;
}

var util = {
clone: function clone(obj) {
return _clone(obj);
Expand Down Expand Up @@ -178,7 +193,8 @@ var util = {
arrFirst: arrFirst,
isDom: function isDom(obj) {
return obj instanceof _Element2.default;
}
},
getAllChildrenElement: getAllChildrenElement
};

exports.default = util;
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "migi",
"version": "6.0.0",
"version": "6.0.1",
"description": "A JavaScript MVVM on JSX",
"maintainers": [
{
Expand Down
4 changes: 4 additions & 0 deletions src/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ var res;
function delegate(e, json, top) {
var elem = e.target;
var vd = hash.get(elem.getAttribute('migi-uid'));
// 可能已经被删除回收,无需继续执行 see https://github.com/migijs/migi/issues/49
if(!vd.__dom) {
return [false];
}
// 点击根元素忽略;不存在也忽略,比如非vd添加的dom没有migi-uid
if(vd == top || !vd) {
return [false];
Expand Down
8 changes: 8 additions & 0 deletions src/domDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ function diffChild(parent, elem, ovd, nvd, record) {
break;
}
}
// 遍历孩子vd回收
util.getAllChildrenElement(ovd).forEach((item) => {
cachePool.add(item.__destroy());
});
// 缓存对象池
cachePool.add(ovd.__destroy());
record.state = type.DOM_TO_TEXT;
Expand Down Expand Up @@ -626,6 +630,10 @@ function diffChild(parent, elem, ovd, nvd, record) {
nvd.style = parent.style;
nvd.emit(Event.DOM);
hash.set(nvd);
// 遍历孩子vd回收
util.getAllChildrenElement(ocp ? ovd.__virtualDom : ovd).forEach((item) => {
cachePool.add(item.__destroy());
});
// 缓存对象池
cachePool.add(ovd.__destroy());
break;
Expand Down
15 changes: 15 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ function arrFirst(arr) {
return arr;
}

function getAllChildrenElement(vd, res = []) {
vd.__children.forEach((item) => {
if(item instanceof migi.VirtualDom) {
getAllChildrenElement(item, res);
res.push(item);
}
else if(item instanceof migi.Component) {
getAllChildrenElement(item.__virtualDom, res);
res.push(item.__virtualDom);
}
});
return res;
}

var util = {
clone(obj) {
return clone(obj);
Expand All @@ -172,6 +186,7 @@ var util = {
isDom: function(obj) {
return obj instanceof Element;
},
getAllChildrenElement,
};

export default util;
16 changes: 16 additions & 0 deletions tests/deleteanddelegate/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>test</title>
</head>
<body>
<div id="test"></div>
<div id="test2">no</div>
<script src="../../demo/es5-shim.js"></script>
<script src="../../demo/es5-sham.js"></script>
<script src="../../demo/es6-shim.js"></script>
<script src="../../dist/index.js"></script>
<script src="script.js"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions tests/deleteanddelegate/script.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Component extends migi.Component {
constructor(...data) {
super(...data);
let self = this;
self.list = [1];
}
@bind list
del() {
this.list.pop();
}
render() {
return <ul onClick={ { b: this.del, 'span': this.click } }>
{
(this.list || []).map(() => {
return <li>
<span>name</span>
<b>del</b>
</li>;
})
}
</ul>;
}
}

migi.render(
<Component/>,
'#test'
);

window.onerror = function() {
document.querySelector('#test2').innerHTML = 'yes';
};
14 changes: 14 additions & 0 deletions tests/deleteanddelegate/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var path = require('path');
var fs = require('fs');

module.exports = {
'delegate': function(browser) {
browser
.url('file://' + path.join(__dirname, 'index.html'))
.waitForElementVisible('body', 1000)
.assert.elementPresent('#test b')
.click('#test b')
.assert.containsText('#test2', 'no')
.end();
}
};
4 changes: 4 additions & 0 deletions web/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ var res;
function delegate(e, json, top) {
var elem = e.target;
var vd = _hash2.default.get(elem.getAttribute('migi-uid'));
// 可能已经被删除回收,无需继续执行 see https://github.com/migijs/migi/issues/49
if (!vd.__dom) {
return [false];
}
// 点击根元素忽略;不存在也忽略,比如非vd添加的dom没有migi-uid
if (vd == top || !vd) {
return [false];
Expand Down
8 changes: 8 additions & 0 deletions web/domDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ function diffChild(parent, elem, ovd, nvd, record) {
break;
}
}
// 遍历孩子vd回收
_util2.default.getAllChildrenElement(ovd).forEach(function (item) {
_cachePool2.default.add(item.__destroy());
});
// 缓存对象池
_cachePool2.default.add(ovd.__destroy());
record.state = _type2.default.DOM_TO_TEXT;
Expand Down Expand Up @@ -655,6 +659,10 @@ function diffChild(parent, elem, ovd, nvd, record) {
nvd.style = parent.style;
nvd.emit(_Event2.default.DOM);
_hash2.default.set(nvd);
// 遍历孩子vd回收
_util2.default.getAllChildrenElement(ocp ? ovd.__virtualDom : ovd).forEach(function (item) {
_cachePool2.default.add(item.__destroy());
});
// 缓存对象池
_cachePool2.default.add(ovd.__destroy());
break;
Expand Down
18 changes: 17 additions & 1 deletion web/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ function arrFirst(arr) {
return arr;
}

function getAllChildrenElement(vd) {
var res = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];

vd.__children.forEach(function (item) {
if (item instanceof migi.VirtualDom) {
getAllChildrenElement(item, res);
res.push(item);
} else if (item instanceof migi.Component) {
getAllChildrenElement(item.__virtualDom, res);
res.push(item.__virtualDom);
}
});
return res;
}

var util = {
clone: function clone(obj) {
return _clone(obj);
Expand Down Expand Up @@ -178,7 +193,8 @@ var util = {
arrFirst: arrFirst,
isDom: function isDom(obj) {
return obj instanceof _Element2.default;
}
},
getAllChildrenElement: getAllChildrenElement
};

exports.default = util;});

0 comments on commit 9960ea2

Please sign in to comment.