mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import {
  isFn,
  noop
} from 'uni-shared'
 
import {
  wrapperMPEvent
} from 'uni-helpers/patch'
 
import {
  VD_SYNC,
  UI_EVENT,
  PAGE_CREATE,
  PAGE_CREATED,
  MOUNTED_DATA,
  UPDATED_DATA,
  VD_SYNC_VERSION
} from '../../../constants'
 
import {
  generateId
} from '../../../helpers/util'
 
import {
  removeVdSync,
  registerVdSync
} from '../subscribe-handlers/on-vd-sync'
 
import {
  vdSyncCallbacks
} from '../subscribe-handlers/on-vd-sync-callback'
 
import {
  hookKeyboardEvent
} from './keyboard'
 
import parseComponentCreateOptions from './parse-component-create-options'
 
function wrapperEvent (event) {
  event.preventDefault = noop
  event.stopPropagation = noop
  return wrapperMPEvent(event)
}
 
const handleVdData = {
  [UI_EVENT]: function onUIEvent (vdBatchEvent, vd) {
    vdBatchEvent.forEach(([cid, nid, event]) => {
      nid = String(nid)
      const target = vd.elements.find(target => target.cid === cid && target.nid === nid)
      if (!target) {
        if (process.env.NODE_ENV !== 'production') {
          console.error(`event handler[${cid}][${nid}] not found`)
        }
        return
      }
      const type = event.type
      const mpEvent = wrapperEvent(event)
      if (type === 'focus' || type === 'blur') {
        hookKeyboardEvent(mpEvent, event => {
          target.dispatchEvent(type, event)
        })
      } else {
        target.dispatchEvent(type, mpEvent)
      }
    })
  }
}
 
function onVdSync (vdBatchData, vd) {
  vdBatchData.forEach(([type, vdData]) => {
    handleVdData[type](vdData, vd)
  })
}
 
export class VDomSync {
  constructor (pageId, pagePath, pageQuery, pageVm) {
    this.pageId = pageId
    this.pagePath = pagePath
    this.pageQuery = pageQuery
    this.pageVm = pageVm
    this.batchData = []
    this.vms = Object.create(null)
    this.initialized = false
 
    this.pageCreateData = false
 
    this.elements = [] //  目前仅存储事件 element
 
    this._init()
  }
 
  _init () {
    registerVdSync(this.pageId, (vdBatchData) => {
      onVdSync(vdBatchData, this)
    })
  }
 
  addMountedVm (vm) {
    vm._$mounted() // 触发vd数据同步
    this.addVdSyncCallback(function mounted () {
      vm.__call_hook('mounted')
    })
  }
 
  addUpdatedVm (vm) {
    vm._$updated() // 触发vd数据同步
    this.addVdSyncCallback(function mounted () {
      vm.__call_hook('updated')
    })
  }
 
  addVdSyncCallback (callback) {
    isFn(callback) && vdSyncCallbacks.push(callback)
  }
 
  getVm (id) {
    return this.vms[id]
  }
 
  addVm (vm) {
    const id = vm._$id
    const oldVm = this.vms[id]
    if (oldVm) {
      const newId = generateId(oldVm, oldVm.$parent, VD_SYNC_VERSION)
      oldVm._$id = newId
      this.vms[newId] = oldVm
      this.elements.forEach(element => {
        const cid = element.cid
        element.cid = cid === id ? newId : cid
      })
    }
    this.vms[id] = vm
  }
 
  removeVm (vm) {
    const cid = vm._$id
    if (vm === this.vms[cid]) { // 仅相同vm的才移除,否则保留
      // 目前同一位置的vm,cid均一样
      // 移除尚未同步的data
      this.batchData = this.batchData.filter(data => data[1][0] !== cid)
      delete this.vms[cid]
    }
  }
 
  addElement (elm) {
    this.elements.indexOf(elm) === -1 && this.elements.push(elm)
  }
 
  removeElement (elm) {
    const elmIndex = this.elements.indexOf(elm)
    if (elmIndex === -1) {
      if (process.env.NODE_ENV !== 'production') {
        console.error(`removeElement[${elm.cid}][${elm.nid}] not found`)
      }
      return
    }
    this.elements.splice(elmIndex, 1)
  }
 
  push (type, cid, data, options) {
    const typeData = [cid, data]
    if (options) {
      typeData.push(options)
    }
    this.batchData.push([type, typeData])
  }
 
  find (type, cid) {
    return this.batchData.find(data => data[0] === type && data[1][0] === cid)
  }
 
  sendPageCreate (data) {
    this.pageCreateData = data
    UniServiceJSBridge.publishHandler(VD_SYNC, {
      data: [
        [PAGE_CREATE, data]
      ],
      options: {
        timestamp: Date.now()
      }
    }, [this.pageId])
  }
 
  flush () {
    if (!this.initialized) {
      this.initialized = true
      this.batchData.push([PAGE_CREATED, [this.pageId, this.pagePath, this.pageQuery]])
    }
    const batchData = this.batchData.filter(data => {
      if (data[0] === UPDATED_DATA && !Object.keys(data[1][1]).length) {
        return false
      }
      return true
    })
    this.batchData.length = 0
    if (batchData.length) {
      UniServiceJSBridge.publishHandler(VD_SYNC, {
        data: batchData,
        options: {
          timestamp: Date.now()
        }
      }, [this.pageId])
    }
  }
 
  restorePageCreate () {
    this.batchData.push([PAGE_CREATE, this.pageCreateData])
  }
 
  restoreMountedData () {
    const addMountedData = (vm) => {
      if (vm._$id) {
        this.push(MOUNTED_DATA, vm._$id, vm._$data, parseComponentCreateOptions(vm))
      }
      // TODO vue 中 $children 顺序不可靠,可能存在恢复误差
      vm.$children.forEach(childVm => addMountedData(childVm))
    }
    addMountedData(this.pageVm)
  }
 
  restorePageCreated () {
    this.batchData.push([PAGE_CREATED, [this.pageId, this.pagePath, this.pageQuery]])
  }
 
  restore () {
    this.initialized = true
    this.batchData.length = 0
 
    this.restorePageCreate()
    this.restoreMountedData()
    this.restorePageCreated()
 
    this.flush()
  }
 
  destroy () {
    this.batchData.length = 0
    this.vms = Object.create(null)
    this.initialized = false
    this.elements.length = 0
    removeVdSync(this.pageId)
  }
}