vdom-sync.js 2.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5
import {
  VD_SYNC,
  UI_EVENT
} from '../../../constants'

fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13 14 15
function findParentCid (vm) {
  let parent = vm.$parent
  while (parent) {
    if (parent._$id) {
      return parent._$id
    }
    parent = parent.$parent
  }
}

fxy060608's avatar
init v3  
fxy060608 已提交
16 17 18
export class VDomSync {
  constructor (pageId) {
    this.pageId = pageId
fxy060608's avatar
fxy060608 已提交
19
    this.addBatchVData = Object.create(null)
fxy060608's avatar
fxy060608 已提交
20
    this.updateBatchVData = []
fxy060608's avatar
init v3  
fxy060608 已提交
21 22 23
    this.vms = Object.create(null)
  }

fxy060608's avatar
fxy060608 已提交
24
  addVData (cid, data = {}, options = {}) {
fxy060608's avatar
fxy060608 已提交
25
    this.addBatchVData[cid] = [data, options]
fxy060608's avatar
init v3  
fxy060608 已提交
26 27
  }

fxy060608's avatar
fxy060608 已提交
28 29
  updateVData (cid, data = {}) {
    this.updateBatchVData.push([cid, data])
fxy060608's avatar
init v3  
fxy060608 已提交
30 31 32
  }

  initVm (vm) {
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39
    if (!vm.$parent) {
      vm._$id = '-1'
    } else {
      vm._$id = findParentCid(vm) + ',' + vm.$vnode.data.attrs._i
    }
    let vData = this.addBatchVData[vm._$id]
    if (!vData) {
fxy060608's avatar
fxy060608 已提交
40
      console.error('cid unmatched', vm)
fxy060608's avatar
fxy060608 已提交
41 42 43 44
      vData = {
        data: {},
        options: {}
      }
fxy060608's avatar
init v3  
fxy060608 已提交
45
    } else {
fxy060608's avatar
fxy060608 已提交
46
      delete this.addBatchVData[vm._$id]
fxy060608's avatar
init v3  
fxy060608 已提交
47
    }
fxy060608's avatar
fxy060608 已提交
48
    const [data, options] = vData
fxy060608's avatar
fxy060608 已提交
49
    Object.assign(vm.$options, options)
fxy060608's avatar
init v3  
fxy060608 已提交
50 51 52 53
    vm.$r = data || Object.create(null)
    this.vms[vm._$id] = vm
  }

fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63 64
  sendUIEvent (cid, nid, event) {
    UniViewJSBridge.publishHandler(VD_SYNC, {
      data: [
        [UI_EVENT, [
          [cid, nid, event]
        ]]
      ],
      options: {
        timestamp: Date.now()
      }
    })
fxy060608's avatar
fxy060608 已提交
65 66
  }

fxy060608's avatar
fxy060608 已提交
67
  clearAddBatchVData () {
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73
    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)
fxy060608's avatar
fxy060608 已提交
74 75
  }

fxy060608's avatar
init v3  
fxy060608 已提交
76
  flush () {
fxy060608's avatar
fxy060608 已提交
77 78
    this.updateBatchVData.forEach(([cid, data]) => {
      const vm = this.vms[cid]
fxy060608's avatar
init v3  
fxy060608 已提交
79
      if (!vm) {
fxy060608's avatar
fxy060608 已提交
80
        return console.error(`Not found ${cid}`)
fxy060608's avatar
init v3  
fxy060608 已提交
81
      }
fxy060608's avatar
fxy060608 已提交
82 83
      Object.keys(data).forEach(cid => {
        Object.assign((vm.$r[cid] || (vm.$r[cid] = Object.create(null))), data[cid])
fxy060608's avatar
fxy060608 已提交
84 85
      })

fxy060608's avatar
init v3  
fxy060608 已提交
86 87
      vm.$forceUpdate()
    })
fxy060608's avatar
fxy060608 已提交
88
    this.updateBatchVData.length = 0
fxy060608's avatar
init v3  
fxy060608 已提交
89 90
  }
}