vue源码分析系列之响应式数据(四)

4次阅读

共计 2900 个字符,预计需要花费 8 分钟才能阅读完成。

前言
上一节着重讲述了 initComputed 中的代码,以及数据是如何从 computed 中到视图层的,以及 data 修改后如何作用于 computed。这一节主要记录 initWatcher 中的内容。
正文
demo 修改
之前的 new Vue(options) 的 options 中,我们可以观察到 computed,data,但是对于 watch 是没法演示的,所以我们在代码中加入一段可以观察到 watch 初始化以及效果的代码。
{
watch: {
a(newV,oldV) {
console.log(`${oldV} -> ${newV}`);
}
}
}
依旧是观察 a 这个变量,当点击 + 1 按钮时候,即可让 a 变化。
入口
if (opts.watch && opts.watch !== nativeWatch) {
initWatch(vm, opts.watch);
}
initWatch
function initWatch (vm: Component, watch: Object) {
for (const key in watch) {
// 拿到 watch 中相关的处理逻辑
const handler = watch[key]
// 如果是个数组,就挨个创建 watcher
if (Array.isArray(handler)) {
for (let i = 0; i < handler.length; i++) {
createWatcher(vm, key, handler[i])
}
} else {
// 否则直接初始化,我们此次例子中会直接走这里
// createWatcher(vm, a, fn)
createWatcher(vm, key, handler)
}
}
}
createWatcher
function createWatcher (
vm: Component,
keyOrFn: string | Function,
handler: any,
options?: Object
) {
// 这里只是对不同创建形式的标准化。
if (isPlainObject(handler)) {
options = handler
handler = handler.handler
}
if (typeof handler === ‘string’) {
handler = vm[handler]
}
// 最终这里是真正监听值变化的地方。
// $watch(a, handle, undefined);
return vm.$watch(keyOrFn, handler, options)
}
vm.$watch
Vue.prototype.$watch = function (
expOrFn: string | Function,
cb: any,
options?: Object
): Function {
const vm: Component = this
if (isPlainObject(cb)) {
return createWatcher(vm, expOrFn, cb, options)
}
options = options || {}
options.user = true
// 主要是这里创建一个观察者
// new Watcher(vm, ‘a’, handle, {user: true})
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
cb.call(vm, watcher.value)
}
return function unwatchFn () {
watcher.teardown()
}
}
new Watcher
watcher,即观察者,是我们多次提到的一个东西。这里主要强调的是 watcher.get() 中的内容。
class Watcher {
constructor (
vm: Component,
expOrFn: string | Function,
cb: Function,
options?: Object
) {
// 一堆初始化信息
if (options) {
this.deep = !!options.deep
this.user = !!options.user
this.lazy = !!options.lazy
this.sync = !!options.sync
} else {
this.deep = this.user = this.lazy = this.sync = false
}
this.value = this.lazy
? undefined
: this.get()
}

get () {
// 将当前 watcher 设为 Dep.target 方便后面访问 a 的 getter 时候,
// 设定为 a 的依赖
pushTarget(this)
let value
const vm = this.vm
try {
// 求 a 的值。并把当前 watcher 加入 a 的依赖中。
value = this.getter.call(vm, vm)
} catch (e) {
if (this.user) {
handleError(e, vm, `getter for watcher “${this.expression}”`)
} else {
throw e
}
} finally {
// “touch” every property so they are all tracked as
// dependencies for deep watching
if (this.deep) {
traverse(value)
}
popTarget()
this.cleanupDeps()
}
return value
}
}
run watcher
到上一步, 监听 a 的 watcher 已经初始化完毕了,当 a 因为点击鼠标变化时候,会触发这个 watcher 的变化。执行 watcher 的 run 方法,我们继续来看下 run 方法里的内容。
class Watcher {
run () {
if (this.active) {
// 求 a 的最新值。
const value = this.get()
if (
value !== this.value ||
// Deep watchers and watchers on Object/Arrays should fire even
// when the value is the same, because the value may
// have mutated.
isObject(value) ||
this.deep
) {
// 当 a 变的时候,将旧值赋给 oldValue。
const oldValue = this.value
// this.value 赋予最新的值。
this.value = value
// 用户定义的 watch 调用时加入 try,catch。
if (this.user) {
try {
// 执行当时传入的回调,并将新值与旧值一并传入。
this.cb.call(this.vm, value, oldValue)
} catch (e) {
handleError(e, this.vm, `callback for watcher “${this.expression}”`)
}
} else {
this.cb.call(this.vm, value, oldValue)
}
}
}
}
}
总结
至此,watch,监听属性的这一部分已经完结,本质上就是对于每个监听的属性,创建一个 watcher。当 watcher 改变时候,会触发开发者定义的回调。通过前两篇文章的学习,这篇应该算是很理解的内容。

正文完
 0

vue源码分析系列之响应式数据(四)

4次阅读

共计 3002 个字符,预计需要花费 8 分钟才能阅读完成。

前言
上一节着重讲述了 initComputed 中的代码,以及数据是如何从 computed 中到视图层的,以及 data 修改后如何作用于 computed。这一节主要记录 initWatcher 中的内容。
正文
demo 修改
之前的 new Vue(options) 的 options 中,我们可以观察到 computed,data,但是对于 watch 是没法演示的,所以我们在代码中加入一段可以观察到 watch 初始化以及效果的代码。
{
watch: {
a(newV,oldV) {
console.log(`${oldV} -> ${newV}`);
}
}
}
依旧是观察 a 这个变量,当点击 + 1 按钮时候,即可让 a 变化。
入口
if (opts.watch && opts.watch !== nativeWatch) {
initWatch(vm, opts.watch);
}
initWatch
function initWatch (vm: Component, watch: Object) {
for (const key in watch) {
// 拿到 watch 中相关的处理逻辑
const handler = watch[key]
// 如果是个数组,就挨个创建 watcher
if (Array.isArray(handler)) {
for (let i = 0; i < handler.length; i++) {
createWatcher(vm, key, handler[i])
}
} else {
// 否则直接初始化,我们此次例子中会直接走这里
// createWatcher(vm, a, fn)
createWatcher(vm, key, handler)
}
}
}
createWatcher
function createWatcher (
vm: Component,
keyOrFn: string | Function,
handler: any,
options?: Object
) {
// 这里只是对不同创建形式的标准化。
if (isPlainObject(handler)) {
options = handler
handler = handler.handler
}
if (typeof handler === ‘string’) {
handler = vm[handler]
}
// 最终这里是真正监听值变化的地方。
// $watch(a, handle, undefined);
return vm.$watch(keyOrFn, handler, options)
}
vm.$watch
Vue.prototype.$watch = function (
expOrFn: string | Function,
cb: any,
options?: Object
): Function {
const vm: Component = this
if (isPlainObject(cb)) {
return createWatcher(vm, expOrFn, cb, options)
}
options = options || {}
options.user = true
// 主要是这里创建一个观察者
// new Watcher(vm, ‘a’, handle, {user: true})
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
cb.call(vm, watcher.value)
}
return function unwatchFn () {
watcher.teardown()
}
}
new Watcher
watcher,即观察者,是我们多次提到的一个东西。这里主要强调的是 watcher.get() 中的内容。
class Watcher {
constructor (
vm: Component,
expOrFn: string | Function,
cb: Function,
options?: Object
) {
// 一堆初始化信息
if (options) {
this.deep = !!options.deep
this.user = !!options.user
this.lazy = !!options.lazy
this.sync = !!options.sync
} else {
this.deep = this.user = this.lazy = this.sync = false
}
this.value = this.lazy
? undefined
: this.get()
}

get () {
// 将当前 watcher 设为 Dep.target 方便后面访问 a 的 getter 时候,
// 设定为 a 的依赖
pushTarget(this)
let value
const vm = this.vm
try {
// 求 a 的值。并把当前 watcher 加入 a 的依赖中。
value = this.getter.call(vm, vm)
} catch (e) {
if (this.user) {
handleError(e, vm, `getter for watcher “${this.expression}”`)
} else {
throw e
}
} finally {
// “touch” every property so they are all tracked as
// dependencies for deep watching
if (this.deep) {
traverse(value)
}
popTarget()
this.cleanupDeps()
}
return value
}
}
run watcher
到上一步, 监听 a 的 watcher 已经初始化完毕了,当 a 因为点击鼠标变化时候,会触发这个 watcher 的变化。执行 watcher 的 run 方法,我们继续来看下 run 方法里的内容。
class Watcher {
run () {
if (this.active) {
// 求 a 的最新值。
const value = this.get()
if (
value !== this.value ||
// Deep watchers and watchers on Object/Arrays should fire even
// when the value is the same, because the value may
// have mutated.
isObject(value) ||
this.deep
) {
// 当 a 变的时候,将旧值赋给 oldValue。
const oldValue = this.value
// this.value 赋予最新的值。
this.value = value
// 用户定义的 watch 调用时加入 try,catch。
if (this.user) {
try {
// 执行当时传入的回调,并将新值与旧值一并传入。
this.cb.call(this.vm, value, oldValue)
} catch (e) {
handleError(e, this.vm, `callback for watcher “${this.expression}”`)
}
} else {
this.cb.call(this.vm, value, oldValue)
}
}
}
}
}
总结
至此,watch,监听属性的这一部分已经完结,本质上就是对于每个监听的属性,创建一个 watcher。当 watcher 改变时候,会触发开发者定义的回调。通过前两篇文章的学习,这篇应该算是很理解的内容。
文章链接

vue 源码分析系列
vue 源码分析系列之 debug 环境搭建
vue 源码分析系列之入口文件分析
vue 源码分析系列之响应式数据(一)
vue 源码分析系列之响应式数据(二)
vue 源码分析系列之响应式数据(三)

正文完
 0