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 244 245 246
| function h(tag, props, children) { if ([...arguments].length === 1 && typeof arguments[0] === 'string') { return { tag: 'text', val: arguments[0] } } return { tag, props, children } } function mount(vnode, container) { let root if (typeof container === 'string') { root = document.querySelector(container) } else { root = container } if (vnode.tag === 'text') { let el = (vnode.el = document.createTextNode(vnode.val)) root.appendChild(el) return } let el = (vnode.el = document.createElement(vnode.tag)) for (const prop_key in vnode.props) { if (prop_key.startsWith('On')) { el.addEventListener( prop_key.slice(2).toLowerCase(), vnode.props[prop_key] ) } else if (prop_key === 'style') { for (const style_key in vnode.props[prop_key]) { el.style[style_key] = vnode.props[prop_key][style_key].toString() } } else { el.setAttribute(prop_key, vnode.props[prop_key]) } }
if (vnode.children && Array.isArray(vnode.children)) { vnode.children.forEach(it => mount(it, el)) } root?.appendChild(el) } function unmount(parent, oNode) { parent?.removeChild(oNode) } function patch(o, n) { if (o.tag === 'text' || n.tag === 'text') { if (o.val !== n.val) { let tmp_parent_el = o.el.parentElement unmount(tmp_parent_el, o.el) mount(n, tmp_parent_el) return } } if (typeof o === 'object' && typeof n === 'object') { if (o.tag !== n.tag) { let tmp_parent_el = o.el?.parentElement unmount(tmp_parent_el, o.el) mount(n, tmp_parent_el) return } else { let nProps = n.props let oProps = o.props for (const n_key in nProps) { if (n_key === 'style') { for (const style_key in nProps.style) { o.el.style[style_key] = nProps.style[style_key].toString() } } if (n_key.startsWith('On')) { o.el?.addEventListener(n_key.slice(2).toLowerCase(), nProps[n_key]) } else { if (n_key !== 'style') { o.el?.setAttribute(n_key, nProps[n_key]) } } } if (nProps !== undefined) { if (!('style' in nProps) && o.el !== undefined) { o.el.style = '' } else { for (const o_style_key in oProps.style) { if (!(o_style_key in nProps.style)) { o.el.style[o_style_key] = '' } } } } for (const o_key in oProps) { if (o_key.startsWith('On')) { o.el?.removeEventListener(o_key.slice(2).toLowerCase(), oProps[o_key]) } else if (!(o_key in nProps)) { o.el.removeAttribute(o_key) } } if (o.children !== undefined && n.children !== undefined) { if (o.children.length === 0) { n.children.forEach(it => mount(it, o.el)) } else if (n.children.length === 0) { o.children.forEach(it => unmount(o.el, it.el)) } } if (o.children === undefined || n.children === undefined) { if (o.children === undefined && n.children !== undefined) { n.children.forEach(it => mount(it, o.el)) } if (n.children === undefined && o.children !== undefined) { o.children.forEach(it => unmount(it, o.el)) } } else if (o.children.length !== 0 && n.children.length !== 0) { let minLen = Math.min(o.children.length, n.children.length) for (let index = 0; index < minLen; index++) { patch(o.children[index], n.children[index]) } for (let index = minLen; index < o.children.length; index++) { unmount(o.el, o.children[index].el) } for (let index = minLen; index < n.children.length; index++) { mount(n.children[index], o.el) } } } } } class Watcher { static _effect = null static WatchEffect(effect) { Watcher._effect = effect effect() Watcher._effect = null } } class Dep { constructor() { this.watchers = new Set() } depend() { this.watchers.add(Watcher._effect) } notify() { this.watchers.forEach(watcher => { watcher instanceof Function && watcher() }) } }
const depMap = new WeakMap() function getDep(obj, prop_key) { let wrap = depMap.get(obj) if (!wrap) { wrap = new Map() depMap.set(obj, wrap) } let dep = wrap.get(prop_key) if (!dep) { dep = new Dep() wrap.set(prop_key, dep) } return dep } function useReactive(data) { return new Proxy(data, { get(target, prop_key) { getDep(target, prop_key).depend() return data[prop_key] }, set(target, prop_key, newVal) { data[prop_key] = newVal getDep(target, prop_key).notify() return data[prop_key] } }) }
function createAppByMiniVue(options) { let data = useReactive(options.data) let isMounted = false let oldNode = null return { data, mount(container) { Watcher.WatchEffect(function() { if (!isMounted) { let vnode = options.render.call(data) oldNode = vnode mount(vnode, container) isMounted = true } else { let newVnode = options.render.call(data) patch(oldNode, newVnode) oldNode = newVnode } }) } } }
|