Skip to content

Commit

Permalink
refactor(v3): generate unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Feb 13, 2020
1 parent 66771b4 commit 8b89204
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
10 changes: 5 additions & 5 deletions packages/uni-app-plus/dist/index.v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions packages/uni-app-plus/dist/service.runtime.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/uni-app-plus/dist/view.umd.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/platforms/app-plus/service/framework/plugins/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
guid,
hasOwn,
isObject,
camelize
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 31 additions & 11 deletions src/platforms/app-plus/view/framework/plugins/vdom-sync.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
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 = {}) {
this.updateBatchVData.push([cid, data])
}

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
Expand All @@ -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 () {
Expand Down

0 comments on commit 8b89204

Please sign in to comment.