vue.js源代码core scedule.js学习笔记


Posted in Javascript onJuly 03, 2017

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript一些不错的函数脚本代码
Sep 10 Javascript
火狐浏览器(firefox)下获得Event对象以及keyCode
Nov 13 Javascript
ASP.NET jQuery 实例2 (表单中使用回车在TextBox之间向下移动)
Jan 13 Javascript
『JavaScript』限制Input只能输入数字实现思路及代码
Apr 22 Javascript
iframe子页面获取父页面元素的方法
Nov 05 Javascript
教你如何使用PHP输出中文JSON字符串
May 22 Javascript
JS/jQuery判断DOM节点是否存在的简单方法
Nov 24 Javascript
JavaScript组成、引入、输出、运算符基础知识讲解
Dec 08 Javascript
Vue的Class与Style绑定的方法
Sep 01 Javascript
微信小程序五子棋游戏的悔棋实现方法【附demo源码下载】
Feb 20 Javascript
微信小程序实现渐入渐出动画效果
Jun 13 Javascript
使用Cargo工具高效创建Rust项目
Aug 14 Javascript
lhgcalendar时间插件限制只能选择三个月的实现方法
Jul 03 #Javascript
JavaScript生成图形验证码
Aug 24 #Javascript
JS滚动到指定位置导航栏固定顶部
Jul 03 #Javascript
mac上node.js环境的安装测试
Jul 03 #Javascript
关于页面刷新vuex数据消失问题解决方案
Jul 03 #Javascript
解决VUEX刷新的时候出现数据消失
Jul 03 #Javascript
vue.js学习之UI组件开发教程
Jul 03 #Javascript
You might like
最贵的咖啡是怎么产生的,它的风味怎么样?
2021/03/04 新手入门
PHP3 safe_mode 失效漏洞
2006/10/09 PHP
PHP添加Xdebug扩展的方法
2014/02/12 PHP
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
2014/06/10 PHP
YII模块实现绑定二级域名的方法
2014/07/09 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
PHP操作Postgresql封装类与应用完整实例
2018/04/24 PHP
聊聊 PHP 8 新特性 Attributes
2020/08/19 PHP
判断页面是关闭还是刷新的js代码
2007/01/28 Javascript
一段批量给页面上的控件赋值js
2010/06/19 Javascript
js中浮点型运算BUG的解决方法说明
2014/01/06 Javascript
jQuery中remove()方法用法实例
2014/12/25 Javascript
JQuery CheckBox(复选框)操作方法汇总
2015/04/15 Javascript
JavaScript中的toLocaleDateString()方法使用简介
2015/06/12 Javascript
JavaScript入门基础
2015/08/12 Javascript
jQuery Mobile框架中的表单组件基础使用教程
2016/05/17 Javascript
基于JavaScript实现熔岩灯效果导航菜单
2017/01/04 Javascript
微信JSAPI支付操作需要注意的细节
2017/01/10 Javascript
JS正则表达式验证账号、手机号、电话和邮箱是否合法
2017/03/08 Javascript
深入浅析Vue.js中 computed和methods不同机制
2018/03/22 Javascript
python实现在一个画布上画多个子图
2020/01/19 Python
在django中form的label和verbose name的区别说明
2020/05/20 Python
Python爬虫获取豆瓣电影并写入excel
2020/07/31 Python
Python 利用argparse模块实现脚本命令行参数解析
2020/12/28 Python
使用css3制作动感导航条示例
2014/01/26 HTML / CSS
初始化了一个没有run()方法的线程类,是否会出错?
2014/03/27 面试题
我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
2014/03/30 面试题
自荐信的五个重要部分
2013/10/29 职场文书
机电一体化毕业生求职信
2013/11/02 职场文书
美术毕业生求职信
2014/02/25 职场文书
中介公司区域经理岗位职责范本
2014/03/02 职场文书
初中班主任评语大全
2014/04/24 职场文书
高中英语演讲稿范文
2014/04/24 职场文书
单位委托书范本(3篇)
2014/09/18 职场文书
三好学生事迹材料
2014/12/24 职场文书
Python OpenCV 彩色与灰度图像的转换实现
2021/06/05 Python