• 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) } } }

    这个方法主要用来保存watcher形成一个事件队列, 并且调用nextTick 执行watcher的run方法

  • 相关阅读:
    基于kafka-net实现的可以长链接的消息生产者
    Windows服务安装、卸载、启动和关闭的管理器
    基于Confluent.Kafka实现的KafkaConsumer消费者类和KafkaProducer消息生产者类型
    [转]C#中HttpClient使用注意:预热与长连接
    基于Confluent.Kafka实现的Kafka客户端操作类使用详解
    [转载]RabbitMQ消息可靠性分析
    ASP.NET Core3.1 MVC 添加验证规则
    asp.net core 3.1 webapi接口参数有时间类型取不到值得问题
    asp.net core 3.1 引用的元包dll版本兼容性问题解决方案
    Python安装和环境配置
  • 原文地址:https://www.cnblogs.com/dhsz/p/7111656.html
Copyright © 2020-2023  润新知