-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathrender.js
More file actions
158 lines (142 loc) · 4.55 KB
/
render.js
File metadata and controls
158 lines (142 loc) · 4.55 KB
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
// 节流方法,性能优化
import { getComKey, cloneDeep } from '../util/index'
import { diffData } from './diff-data'
// 全局的命名约定,为了节省编译的包大小一律采取形象的缩写,说明如下。
// $c === $child
// $k === $comKey
// 新型的被拍平的数据结构
// {
// $root: {
// '1-1'{
// // ... data
// },
// '1.2-1': {
// // ... data1
// },
// '1.2-2': {
// // ... data2
// }
// }
// }
const KEY_SEP = '_'
function getVmData (vm) {
// 确保当前 vm 所有数据被同步
const dataKeys = [].concat(
Object.keys(vm._data || {}),
Object.keys(vm._props || {}),
Object.keys(vm._mpProps || {}),
Object.keys(vm._computedWatchers || {})
)
return dataKeys.reduce((res, key) => {
res[key] = cloneDeep(vm[key])
return res
}, {})
}
function getParentComKey (vm, res = []) {
const { $parent } = vm || {}
if (!$parent) return res
res.unshift(getComKey($parent))
if ($parent.$parent) {
return getParentComKey($parent, res)
}
return res
}
function formatVmData (vm) {
const $p = getParentComKey(vm).join(KEY_SEP)
const $k = $p + ($p ? KEY_SEP : '') + getComKey(vm)
// getVmData 这儿获取当前组件内的所有数据,包含 props、computed 的数据
// 改动 vue.runtime 所获的的核心能力
const data = Object.assign(getVmData(vm), { $k, $kk: `${$k}${KEY_SEP}`, $p })
const key = '$root.' + $k
const res = { [key]: data }
return res
}
function collectVmData (vm, res = {}) {
const { $children: vms } = vm
if (vms && vms.length) {
vms.forEach(v => collectVmData(v, res))
}
return Object.assign(res, formatVmData(vm))
}
/**
* 频率控制 返回函数连续调用时,func 执行频率限定为 次 / wait
* 自动合并 data
*
* @param {function} func 传入函数
* @param {number} wait 表示时间窗口的间隔
* @param {object} options 如果想忽略开始边界上的调用,传入{leading: false}。
* 如果想忽略结尾边界上的调用,传入{trailing: false}
* @return {function} 返回客户调用函数
*/
function throttle (func, wait, options) {
let context, args, result
let timeout = null
// 上次执行时间点
let previous = 0
if (!options) options = {}
// 延迟执行函数
function later () {
// 若设定了开始边界不执行选项,上次执行时间始终为0
previous = options.leading === false ? 0 : Date.now()
timeout = null
result = func.apply(context, args)
if (!timeout) context = args = null
}
return function (handle, data) {
const now = Date.now()
// 首次执行时,如果设定了开始边界不执行选项,将上次执行时间设定为当前时间。
if (!previous && options.leading === false) previous = now
// 延迟执行时间间隔
const remaining = wait - (now - previous)
context = this
args = args ? [handle, Object.assign(args[1], data)] : [handle, data]
// 延迟时间间隔remaining小于等于0,表示上次执行至此所间隔时间已经超过一个时间窗口
// remaining大于时间窗口wait,表示客户端系统时间被调整过
if (remaining <= 0 || remaining > wait) {
clearTimeout(timeout)
timeout = null
previous = now
result = func.apply(context, args)
if (!timeout) context = args = null
// 如果延迟执行不存在,且没有设定结尾边界不执行选项
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining)
}
return result
}
}
// 优化频繁的 setData: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/performance/tips.html
const throttleSetData = throttle((handle, data) => {
if (!Object.keys(data).length) {
return
}
handle(data)
}, 50)
function getPage (vm) {
const rootVueVM = vm.$root
const { mpType = '', page } = rootVueVM.$mp || {}
// 优化后台态页面进行 setData: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/performance/tips.html
if (mpType === 'app' || !page || typeof page.setData !== 'function') {
return
}
return page
}
// 优化js变量动态变化时候引起全量更新
// 优化每次 setData 都传递大量新数据
export function updateDataToMP () {
const page = getPage(this)
if (!page) {
return
}
const data = {}
diffData(this, data)
throttleSetData(page.setData.bind(page), data)
}
export function initDataToMP () {
const page = getPage(this)
if (!page) {
return
}
const data = collectVmData(this.$root)
page.setData(data)
}