vue.js 源代码学习笔记 ----- Dep
/* @flow */ import type Watcher from './watcher'
import { remove } from '../util/index' let uid = 0 /**
* A dep is an observable that can have multiple
* directives subscribing to it. 一个订阅模式 可以有多个指令去订阅他
*/
export default class Dep {
static target: ?Watcher;
id: number;
subs: Array<Watcher>; constructor () {
this.id = uid++
this.subs = []
} addSub (sub: Watcher) {
this.subs.push(sub)
} removeSub (sub: Watcher) {
remove(this.subs, sub)
} depend () {
if (Dep.target) {
Dep.target.addDep(this)
}
} notify () {
// stabilize the subscriber list first , 避免改动影响到原来的数组
const subs = this.subs.slice()
for (let i = 0, l = subs.length; i < l; i++) {
subs[i].update()
}
}
} // the current target watcher being evaluated.
// this is globally unique because there could be only one
// watcher being evaluated at any time.
// 一个全局唯一的, 因为只能有一个watcher 依赖的对象的值 被计算, 在任何时间 Dep.target = null
/*
利用这个中间变量, 缓存已有的target, 在 pushTarget 函数, 使用传入的target 代替 Dep.target; 然后使用 Dep.target
最后使用 popTarget 还原 ; 主要是为了Observe中的get方法, 判断当前是否有watcher(Dep.target), 如果有就在dep增加这个属性的依赖, Dep.target.depend( dep1 ) 比如 methods 的每一个方法可以是 一个 watcher, 这个wactcher可能会依赖多个 data里面的属性
*/
const targetStack = []
//放入 dep的 订阅者
export function pushTarget (_target: Watcher) {
if (Dep.target) targetStack.push(Dep.target)
Dep.target = _target
}
//得到一个 dep的 订阅者
export function popTarget () { Dep.target = targetStack.pop() }
vue.js 源代码学习笔记 ----- Dep的更多相关文章
- vue.js 源代码学习笔记 ----- html-parse.js
/** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...
- vue.js 源代码学习笔记 ----- instance state
/* @flow */ import Dep from '../observer/dep' import Watcher from '../observer/watcher' import { set ...
- vue.js 源代码学习笔记 ----- 工具方法 option
/* @flow */ import Vue from '../instance/index' import config from '../config' import { warn } from ...
- vue.js 源代码学习笔记 ----- 工具方法 lang
/* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
- vue.js 源代码学习笔记 ----- observe
参考 vue 2.2.6版本 /* @flow */ //引入订阅者模式 import Dep from './dep' import { arrayMethods } from './array' ...
- vue.js 源代码学习笔记 ----- helpers.js
/* @flow */ import { parseFilters } from './parser/filter-parser' export function baseWarn (msg: str ...
- vue.js 源代码学习笔记 ----- instance render
/* @flow */ import { warn, nextTick, toNumber, _toString, looseEqual, emptyObject, handleError, loos ...
- vue.js 源代码学习笔记 ----- instance event
/* @flow */ import { updateListeners } from '../vdom/helpers/index' import { toArray, tip, hyphenate ...
随机推荐
- mysql创建账号
1.创建账号1)只读账号GRANT USAGE ON *.* TO 'testgao_r'@'192.168.1.1' IDENTIFIED BY 'password' WITH MAX_QUERIE ...
- linux系统调用是通过软中断实现的吗
软中断是利用硬件中断的概念,用软件方式进行模拟,实现宏观上的异步执行效果.很多情况下,软中断和信号有些类似,同时,软中断又是和硬中断相对应的,硬中断是外部设备对CPU的中断,软中断通常是硬中断服务程序 ...
- 【读书笔记】《深入浅出nodejs》第五章 内存控制
海量请求+长时间运行 -> 内存控制 -> 一切资源高效循环利用 1. V8的垃圾回收机制与内存限制 在Node中通过JavaScript使用内存时,只能使用部分内存(64位系统下约1.4 ...
- 如何设置hyper-v下的ubuntu虚拟机分辨率
1.登陆ubuntu虚拟机 2.vi /etc/default/grub,改变如下内容: 改变前: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" ...
- win10家庭版的defender注册表关闭和开启
关闭方法: 打开“命令提示符(管理员)”,然后输入: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defe ...
- ubuntu安装python MySQLdb模块
本文讲述了python安装mysql-python的方法.分享给大家供大家参考,具体如下: ubuntu 系统下进行的操作 首先安装了pip工具 ? 1 sudo apt-get install py ...
- [小问题笔记(八)] 常用SQL(读字段名,改字段名,打印影响行数,添加默认值,查找存储过程等)
读取所有字段,自然排序 declare @fields varchar(max) Select @fields=ISNULL(@fields,'')++name+',' from syscolumns ...
- python学习笔记glob模块
python有许多的类库,现将学习记录下来,以供以后回顾复习: 1.glob模块 用于文件名操作,匹配指定目录下的文件,返回的是目录加文件名,常用的有两个函数: glob(pattern),返回匹配的 ...
- 06_zookeeper_原生API使用2
1. 设置znode节点数据(同步) import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java ...
- 如何制作自己的R包?
摘自 方匡南 等编著<R数据分析-方法与案例详解>.电子工业出版社 R包简介 R包提供了一个加载所需代码.数据和文件的集合.R软件自身就包含大约30种不同功能的包,这些基本包提供了R软件的 ...