提交 8b892049 编写于 作者: fxy060608's avatar fxy060608

refactor(v3): generate unique id

上级 66771b4f
......@@ -297,10 +297,6 @@ var serviceContext = (function () {
return ('' + str).replace(/[^\x00-\xff]/g, '**').length
}
function guid () {
return Math.floor(4294967296 * (1 + Math.random())).toString(16).slice(1)
}
function debounce (fn, delay) {
let timeout;
return function () {
......@@ -12886,7 +12882,11 @@ var serviceContext = (function () {
this._$vdomSync = new VDomSync(this.$options.pageId, this.$options.pagePath, this);
}
if (this._$vd) {
this._$id = guid();
if (!this.$parent) {
this._$id = '-1';
} else {
this._$id = this.$parent._$id + ',' + this.$vnode.data.attrs._i;
}
this._$vd.addVm(this);
this._$vdMountedData = Object.create(null);
this._$setData(MOUNTED_DATA, this._$vdMountedData);
......
......@@ -5640,8 +5640,7 @@ function insertBefore() {
}
function removeChild(node, child) {
if (child && child._$vd) {
// TODO 目前存储的 element 非树形,remove 的不干净(会遗留子节点)
if (child && child._$vd) {
child._$vd.removeElement(child);
}
}
......@@ -6801,14 +6800,13 @@ function updateDOMListeners (oldVnode, vnode) {
var on = vnode.data.on || {};
var oldOn = oldVnode.data.on || {};
target$1 = vnode.elm;
// fixed by xxxxxx 存储 vd
target$1._$vd = vnode.context._$vd;
var context = vnode.context;
target$1._$vd = context._$vd || (context.$root && context.$root._$vd);
// 存储事件标记
target$1.setAttribute('nid', String(vnode.data.attrs['_i']));
target$1.setAttribute('cid', context._$id || vnode.data.cid);
target$1.setAttribute('cid', context._$id);
normalizeEvents(on);
updateListeners(on, oldOn, add$1, remove$2, createOnceHandler$1, vnode.context);
......
import {
guid,
hasOwn,
isObject,
camelize
......@@ -88,7 +87,11 @@ export function initData (Vue) {
this._$vdomSync = new VDomSync(this.$options.pageId, this.$options.pagePath, this)
}
if (this._$vd) {
this._$id = guid()
if (!this.$parent) {
this._$id = '-1'
} else {
this._$id = this.$parent._$id + ',' + this.$vnode.data.attrs._i
}
this._$vd.addVm(this)
this._$vdMountedData = Object.create(null)
this._$setData(MOUNTED_DATA, this._$vdMountedData)
......
import {
guid
} from 'uni-shared'
import {
VD_SYNC,
UI_EVENT
} from '../../../constants'
function findParentCid (vm) {
let parent = vm.$parent
while (parent) {
if (parent._$id) {
return parent._$id
}
parent = parent.$parent
}
}
export class VDomSync {
constructor (pageId) {
this.pageId = pageId
this.addBatchVData = []
this.addBatchVData = Object.create(null)
this.updateBatchVData = []
this.vms = Object.create(null)
}
addVData (cid, data = {}, options = {}) {
this.addBatchVData.push([cid, data, options])
this.addBatchVData[cid] = [data, options]
}
updateVData (cid, data = {}) {
......@@ -24,13 +30,22 @@ export class VDomSync {
}
initVm (vm) {
const [cid, data, options] = this.addBatchVData.shift()
if (!cid) {
vm._$id = guid()
if (!vm.$parent) {
vm._$id = '-1'
} else {
vm._$id = findParentCid(vm) + ',' + vm.$vnode.data.attrs._i
}
let vData = this.addBatchVData[vm._$id]
if (!vData) {
console.error('cid unmatched', vm)
vData = {
data: {},
options: {}
}
} else {
vm._$id = cid
delete this.addBatchVData[vm._$id]
}
const [data, options] = vData
Object.assign(vm.$options, options)
vm.$r = data || Object.create(null)
this.vms[vm._$id] = vm
......@@ -50,7 +65,12 @@ export class VDomSync {
}
clearAddBatchVData () {
this.addBatchVData.length = 0
if (process.env.NODE_ENV !== 'production') {
if (Object.keys(this.addBatchVData).length) {
console.error('this.addBatchVData...=' + JSON.stringify(this.addBatchVData))
}
}
this.addBatchVData = Object.create(null)
}
flush () {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册