vue.js源代码core scedule.js学习笔记
程序员文章站
2023-09-08 21:32:37
vue.js 源代码学习笔记 core scedule.js,供大家参考,具体内容如下
/* @flow */
import type watcher...
vue.js 源代码学习笔记 core scedule.js,供大家参考,具体内容如下
/* @flow */ import type watcher from './watcher' import config from '../config' import { callhook } from '../instance/lifecycle' import { warn, nexttick, devtools } from '../util/index' const queue: array<watcher> = [] let has: { [key: number]: ?true } = {} let circular: { [key: number]: number } = {} let waiting = false let flushing = false let index = 0 /** * reset the scheduler's state. */ function resetschedulerstate () { queue.length = 0 has = {} if (process.env.node_env !== 'production') { circular = {} } waiting = flushing = false } /** * flush both queues and run the watchers. */ function flushschedulerqueue () { flushing = true let watcher, id, vm // sort queue before flush. // this ensures that: // 1. components are updated from parent to child. (because parent is always // created before the child) // 2. a component's user watchers are run before its render watcher (because // user watchers are created before the render watcher) // 3. if a component is destroyed during a parent component's watcher run, // its watchers can be skipped. queue.sort((a, b) => a.id - b.id) // do not cache length because more watchers might be pushed // as we run existing watchers for (index = 0; index < queue.length; index++) { watcher = queue[index] id = watcher.id has[id] = null watcher.run() // in dev build, check and stop circular updates. if (process.env.node_env !== 'production' && has[id] != null) { circular[id] = (circular[id] || 0) + 1 if (circular[id] > config._maxupdatecount) { warn( 'you may have an infinite update loop ' + ( watcher.user ? `in watcher with expression "${watcher.expression}"` : `in a component render function.` ), watcher.vm ) break } } } // reset scheduler before updated hook called const oldqueue = queue.slice() resetschedulerstate() // call updated hooks index = oldqueue.length while (index--) { watcher = oldqueue[index] vm = watcher.vm if (vm._watcher === watcher && vm._ismounted) { callhook(vm, 'updated') } } // devtool hook /* istanbul ignore if */ if (devtools && config.devtools) { devtools.emit('flush') } } /** * push a watcher into the watcher queue. * jobs with duplicate ids will be skipped unless it's * pushed when the queue is being flushed. */ export function queuewatcher (watcher: watcher) { const id = watcher.id if (has[id] == null) { has[id] = true if (!flushing) { queue.push(watcher) } else { // if already flushing, splice the watcher based on its id // if already past its id, it will be run next immediately. let i = queue.length - 1 while (i >= 0 && queue[i].id > watcher.id) { i-- } queue.splice(math.max(i, index) + 1, 0, watcher) } // queue the flush if (!waiting) { waiting = true nexttick(flushschedulerqueue) } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 酸菜的腌制方法?带你了解神奇的腌菜
下一篇: 详解vue数据渲染出现闪烁问题