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

refactor(v3): generate unique id

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