likes
comments
collection
share

Vue响应式原理小探

作者站长头像
站长
· 阅读数 46

目标

  • 模拟一个最小版本的 Vue
  • 响应式原理的常问问题
  • 实际项目中出问题的原理层面的解决
    • 给 Vue 实例新增一个成员是否是响应式的?
    • 给属性重新赋值成对象,是否是响应式的?

先前须知

  • 数据驱动
  • 响应式的核心原理
  • 发布订阅模式和观察者模式

数据驱动

  • 数据响应式、双向绑定、数据驱动
  • 数据响应式
    • 数据模型仅仅是普通的 JavaScript 对象,而当我们修改数据时,视图会进行更新,避免了繁琐的 DOM 操作,提高开发效率
  • 双向绑定
    • 数据改变,视图改变;视图改变,数据也随之改变
    • 可以使用 v-model 在表单元素上创建双向数据绑定
  • 数据驱动是 Vue 最的特性之一
    • 开发过程中仅需要关注数据本身,不需要关心数据是如何渲染到视图

数据响应式的核心原理

Vue2

  • Object.defineProperty()
  • 浏览器兼容 IE8 以上(不兼容 IE8)
    • Object.defineProperty()  方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性,并返回此对象。
    // data 选项
    let data = {
      msg: 'hello'
    }

    // 模拟 Vue 的实例
    let vm = {}

    // 数据劫持:当访问或者设置 vm 中的成员的时候,做一些干预操作
    Object.defineProperty(vm, 'msg', {
      // 可枚举(可遍历)
      enumerable: true,
      // 可配置(可以使用 delete 删除,可以通过 defineProperty 重新定义)
      configurable: true,
      // 当获取值的时候执行
      get () {
        return data.msg
      },
      // 当设置值的时候执行
      set (newValue) {
        if (newValue === data.msg) {
          return
        }
        data.msg = newValue
        // 数据更改,更新 DOM 的值
        document.querySelector('#app').textContent = data.msg
      }
    })

    // test
    vm.msg = 'Hello World'
  • 一个对象中多个属性需要转换 getter/setter 处理
    // data 选项
    let data = {
      msg: 'hello',
      count: 10
    }

    // 模拟 Vue 的实例
    let vm = {}

    proxyData(data)

    function proxyData(data) {
      // 遍历 data 对象的所有属性
      Object.keys(data).forEach(key => {
        // 把 data 中的属性,转换成 vm 的 setter/setter
        Object.defineProperty(vm, key, {
          enumerable: true,
          configurable: true,
          get () {
            console.log('get: ', key, data[key])
            return data[key]
          },
          set (newValue) {
            console.log('set: ', key, newValue)
            if (newValue === data[key]) {
              return
            }
            data[key] = newValue
            // 数据更改,更新 DOM 的值
            document.querySelector('#app').textContent = data[key]
          }
        })
      })
    }

    // test
    vm.msg = 'Hello World'

Vue3

  • Proxy
    • Proxy 对象用于创建一个对象的代理,从而实现基本操作的拦截和自定义(如属性查找、赋值、枚举、函数调用等)。
  • 直接监听对象,而非属性。
  • ES 6中新增,IE 不支持,性能由浏览器优化
    // 模拟 Vue 中的 data 选项
    let data = {
      msg: 'hello',
      count: 0
    }

    // 模拟 Vue 实例
    let vm = new Proxy(data, {
      // 执行代理行为的函数
      // 当访问 vm 的成员会执行
      get (target, key) {
        console.log('get, key: ', key, target[key])
        return target[key]
      },
      // 当设置 vm 的成员会执行
      set (target, key, newValue) {
        console.log('set, key: ', key, newValue)
        if (target[key] === newValue) {
          return
        }
        target[key] = newValue
        document.querySelector('#app').textContent = target[key]
      }
    })

    // test
    vm.msg = 'Hello World'

发布订阅模式和观察者模式

发布订阅模式

  • 发布/订阅模式
    • 订阅者
    • 发布者
    • 信号中心

假设,存在一个"信号中心",某个任务执行完成,就向信号中心"发布"(publish)一个信号,其他任务可以向信号中心"订阅"(subscribe)这个信号,从而知道什么时候自己可以开始执行。这就叫做"发布/订阅模式"(publish-subscribe pattern)

示例

			// 事件触发器
			class EventEmitter {
				constructor() {
					this.subs = Object.create(null);
				}

				// 注册事件
				$on(eventType, handler) {
					this.subs[eventType] = this.subs[eventType] || [];
					this.subs[eventType].push(handler);
				}

				// 触发事件
				$emit(eventType) {
					if (this.subs[eventType]) {
						this.subs[eventType].forEach((handler) => {
							handler();
						});
					}
				}
			}

			let vm = new EventEmitter();
			console.log(vm);

			vm.$on("click", () => {
				console.log("click1");
			});

			vm.$on("click", () => {
				console.log("click2");
			});

			vm.$emit("click");

观察者模式

  • 观察者(订阅者) -- Watcher
    • update():当事件发生时,具体要做的事情
  • 目标(发布者) -- Dep
    • subs 数组:存储所有的观察者
    • addSub():添加观察者
    • notify():当事件发生,调用所有观察者的 update() 方法
  • 没有事件中心
			// 发布者-目标
			class Dep {
				constructor() {
					// 记录所有的订阅者
					this.subs = [];
				}
				// 添加订阅者
				addSub(sub) {
					if (sub && sub.update) {
						this.subs.push(sub);
					}
				}

				// 发布通知
				notify() {
					this.subs.forEach((sub) => {
						sub.update();
					});
				}
			}

			// 订阅者-观察者
			class Watcher {
				update() {
					console.log("update");
				}
			}

			// 测试
			let dep = new Dep();
			let watcher = new Watcher();

			dep.addSub(watcher);

			dep.notify();

结论

  • 观察者模式是由具体目标调度,比如当事件触发,Dep 就会去调用观察者的方法,所以观察者模式的订阅者与发布者之间是存在依赖的。
  • 发布/订阅模式由统一调度中心调用,因此发布者和订阅者不需要知道对方的存在

响应式原理

初步认识

  • 基本结构

Vue响应式原理小探

每部分作用

  • Vue
    • 把 data 中的成员注入到 Vue 实例,并且把 data 中的成员转成 getter/setter
  • Observer
    • 能够对数据对象的所有属性进行监听,如有变动可拿到最新值并通知 Dep
  • Compiler
    • 解析每个元素中的指令/插值表达式,并替换成相应的数据
  • Dep
    • 添加观察者(watcher),当数据变化通知所有观察者
  • Watcher
    • 数据变化更新视图

Vue

  • 功能
    • 负责接收初始化的参数(选项)
    • 负责把 data 中的属性注入到 Vue 实例,转换成 getter/setter
    • 负责调用 observer 监听 data 中所有属性的变化
    • 负责调用 compiler 解析指令/插值表达式
  • 结构

Vue响应式原理小探

上代码:

class Vue {
	constructor(options) {
		// 1. 通过属性保存选项的数据
		console.log(options);
		this.$options = options || {};
		this.$data = options.data || {};
		this.$el =
			typeof options.el === "string"
				? document.querySelector(options.el)
				: options.el;
		// 2. 把data中的成员转换成getter和setter,注入到vue实例中
		this._proxyData(this.$data);
		// 3. 调用observer对象,监听数据的变化

		// 4. 调用compiler对象,解析指令和差值表达式
	}

	_proxyData(data) {
		// 遍历data中的所有属性
		Object.keys(data).forEach((key) => {
			// 把data的属性注入到vue实例中
			Object.defineProperty(this, key, {
				// 当且仅当该属性的 configurable 键值为 true 时,该属性的描述符才能够被改变,同时该属性也能从对应的对象上被删除。 默认为 false。
				configurable: true,
				// 当且仅当该属性的 enumerable 键值为 true 时,该属性才会出现在对象的枚举属性中。 默认为 false。
				enumerable: true,
				// 属性的 getter 函数,如果没有 getter,则为 undefined。当访问该属性时,会调用此函数。执行时不传入任何参数,但是会传入 this 对象(由于继承关系,这里的this并不一定是定义该属性的对象)。该函数的返回值会被用作属性的值。 默认为 undefined。
				get() {
					return data[key];
				},

				// 属性的 setter 函数,如果没有 setter,则为 undefined。当属性值被修改时,会调用此函数。该方法接受一个参数(也就是被赋予的新值),会传入赋值时的 this 对象。 默认为 undefined。
				set(newValue) {
					if (newValue === data[key]) {
						return;
					}
					return (data[key] = newValue);
				},
			});
		});
	}
}

Vue响应式原理小探

Observer

  • 功能

    • 负责把 data 选项中的属性转换成响应式数据
    • data 中的某个属性也是对象,把该属性转换成响应式数据
    • 数据变化发送通知
  • 结构

Vue响应式原理小探 代码:

/**
 *  负责数据劫持
 *  把 $data 中的成员转换成 getter/setter
 */
class Observer {
	constructor(data) {
		this.walk(data);
	}

	// 1. 判断数据是否是对象,如果不是对象返回
	// 2. 如果是对象,遍历对象的所有属性,设置为 getter/setter
	walk(data) {
		// 1. 判断data是否是对象
		if (!data || typeof data !== "object") return;

		// 2. 遍历data对象的所有属性
		Object.keys(data).forEach((key) => {
			this.defineReactive(data, key, data[key]);
		});
	}

	// 定义响应式成员
	defineReactive(obj, key, val) {
		const that = this;
		// 如果val是对象,把val内部的属性转换成响应式数据
		this.walk(val);
		Object.defineProperty(obj, key, {
			enumerable: true,
			configurable: true,
			get() {
				return val;
			},
			set(newValue) {
				if (newValue === val) return;
				val = newValue;
                // 如果 newValue 是对象,设置 newValue 的成员为响应式
				that.walk(newValue);
			},
		});
	}
}

Vue响应式原理小探

Compiler

  • 功能

    • 负责编译模板,解析指令/插值表达式
    • 负责页面的首次渲染
    • 当数据变化后重新渲染视图
  • 结构

Vue响应式原理小探

代码:

/**
 * 负责解析指令/插值表达式
 */
class Compiler {
	constructor(vm) {
		this.el = vm.$el;
		this.vm = vm;
		this.compile(this.el);
	}

	// 编译模板
	// 处理文本节点和元素节点
	compile(el) {
		let childNodes = el.childNodes;
		Array.from(childNodes).forEach((node) => {
			// 处理文本节点
			if (this.isTextNode(node)) {
				this.compileText(node);
			} else if (this.isElementNode(node)) {
				// 处理元素节点
				this.compileElement(node);
			}

			// 判断node的节点是否有子节点,如果有子节点要递归调用compile。
			if (node.childNodes && node.childNodes.length) {
				this.compile(node);
			}
		});
	}

	// 编译元素节点,处理指令
	compileElement(node) {
		console.log(node.attributes);
		// 遍历所有的属性节点
		Array.from(node.attributes).forEach((attr) => {
			// 获取元素属性的名称
			let attrName = attr.name;
			// 判断当前的属性名称是否是指令
			if (this.isDirective(attrName)) {
				// attrName 的形式 v-text  v-model
				// 截取属性的名称,获取 text model
				attrName = attrName.substring(2);
				// 获取属性的名称,属性的名称就是我们数据对象的属性 v-text="name",获取的是name;
				const key = attr.value;
				// 处理不同的指令
				this.update(node, key, attrName);
			}
		});
	}

	// 负责更新 DOM
	// 创建 Watche
	update(node, key, dir) {
		// node 节点,key 数据的属性名称,dir 指令的后半部分
		const updaterFn = this[dir + "Updater"];
		updaterFn && updaterFn(node, this.vm[key]);
	}

	// v-text 指令的更新方法
	textUpdater(node, value) {
		node.textContent = value;
	}

	// v-model 指令的更新方法
	modelUpdater(node, value) {
		node.value = value;
	}

	// 编译文本节点,处理插值表达式
	compileText(node) {
		// 匹配插值表达式,提取内容
		let reg = /\{\{(.+?)\}\}/;
		let value = node.textContent;
		if (reg.test(value)) {
			let key = RegExp.$1.trim();
			node.textContent = value.replace(reg, this.vm[key]);
		}
	}

	// 判断元素属性是否是指令
	isDirective(attrName) {
		return attrName.startsWith("v-");
	}

	// 判断节点是否是文本节点
	isTextNode(node) {
		return node.nodeType === 3;
	}

	// 判断节点是否是元素节点
	isElementNode(node) {
		return node.nodeType === 1;
	}
}

Dep(Dependency)

Vue响应式原理小探

  • 功能
    • 收集依赖,添加观察者(watcher)
    • 通知所有观察者
  • 结构

Vue响应式原理小探 代码

class Dep {
	constructor() {
		// 存储所有的观察者
		this.subs = [];
	}

	/**添加观察者 */
	addSub(sub) {
		if (sub && sub.update) {
			this.subs.push(sub);
		}
	}

	/**通知所有观察者 */
	notify() {
		this.subs.forEach((sub) => {
			sub.update();
		});
	}
}

// 在 Observer 中收集依赖,发送通知

// defineReactive 中
// 创建 dep 对象收集依赖
const dep = new Dep()

// getter 中
// get 的过程中收集依赖
Dep.target && dep.addSub(Dep.target)

// setter 中
// 当数据变化之后,发送通知
dep.notify()

Watcher

Vue响应式原理小探

  • 功能
    • 当数据变化触发依赖, dep 通知所有的 Watcher 实例更新视图
    • 自身实例化的时候往 dep 对象中添加自己
  • 结构

Vue响应式原理小探 代码实现:

class Watcher {
	constructor(vm, key, cb) {
		this.vm = vm;
		// data 中的属性名称
		this.key = key;
		// 当数据变化的时候,调用 cb 更新视图
		this.cb = cb;
		// 在 Dep 的静态属性上记录当前 watcher 对象,当访问数据的时候把 watcher 添加到 dep 的 subs 中
		Dep.target = this;
		// 触发一次 getter,让 dep 为当前 key 记录 watcher
		this.oldValue = vm[key];
		// 清空 target
		Dep.target = null;
	}

	// 触发更新
	update() {
		const newValue = this.vm[this.key];
		if (this.oldValue === newValue) {
			return;
		}
		this.cb(newValue);
	}
}

// 在 Compiler中为每一个指令/插值表达式创建 watcher 对象,监视数据的变化
// textUpdater等中要使用 this
updaterFn && updaterFn.call(this, node, this.vm[key], key);

	// v-text 指令的更新方法
	textUpdater(node, value, key) {
		node.textContent = value;
		// 每一个指令中创建一个 watcher,观察数据的变化
		new Watcher(this.vm, key, (newValue) => {
			node.textContent = newValue;
		});
	}

双向绑定

	// v-model 指令的更新方法
	modelUpdater(node, value, key) {
		node.value = value;
		// 每一个指令中创建一个 watcher,观察数据的变化
		new Watcher(this.vm, key, (newValue) => {
			node.value = newValue;
		});

		// 双向绑定
		node.addEventListener("input", () => {
			this.vm[key] = node.value;
		});
	}

总结

Vue响应式原理小探

  • Vue
    • 记录传入的选项,设置 data/data/data/el
    • 把 data 的成员注入到 Vue 实例
    • 负责调用 Observer 实现数据响应式处理(数据劫持)
    • 负责调用 Compiler 编译指令/插值表达式等
  • Observer
    • 数据劫持
      • 负责把 data 中的成员转换成 getter/setter
      • 负责把多层属性转换成 getter/setter
      • 如果给属性赋值为新对象,把新对象的成员设置为 getter/setter
    • 添加 Dep 和 Watcher 的依赖关系
    • 数据变化发送通知
  • Compiler
    • 负责编译模板,解析指令/插值表达式
    • 负责页面的首次渲染过程
    • 当数据变化后重新渲染
  • Dep
    • 收集依赖,添加订阅者(watcher)
    • 通知所有订阅者
  • Watcher
    • 自身实例化的时候往dep对象中添加自己
    • 当数据变化dep通知所有的 Watcher 实例更新视图

完整代码

class Vue {
	constructor(options) {
		// 1. 通过属性保存选项的数据
		this.$options = options || {};
		this.$data = options.data || {};
		this.$el =
			typeof options.el === "string"
				? document.querySelector(options.el)
				: options.el;
		// 2. 把data中的成员转换成getter和setter,注入到vue实例中
		this._proxyData(this.$data);
		// 3. 调用observer对象,监听数据的变化
		new Observer(this.$data);
		// 4. 调用compiler对象,解析指令和差值表达式
		new Compiler(this);
	}

	_proxyData(data) {
		// 遍历data中的所有属性
		Object.keys(data).forEach((key) => {
			// 把data的属性注入到vue实例中
			Object.defineProperty(this, key, {
				// 当且仅当该属性的 configurable 键值为 true 时,该属性的描述符才能够被改变,同时该属性也能从对应的对象上被删除。 默认为 false。
				configurable: true,
				// 当且仅当该属性的 enumerable 键值为 true 时,该属性才会出现在对象的枚举属性中。 默认为 false。
				enumerable: true,
				// 属性的 getter 函数,如果没有 getter,则为 undefined。当访问该属性时,会调用此函数。执行时不传入任何参数,但是会传入 this 对象(由于继承关系,这里的this并不一定是定义该属性的对象)。该函数的返回值会被用作属性的值。 默认为 undefined。
				get() {
					return data[key];
				},

				// 属性的 setter 函数,如果没有 setter,则为 undefined。当属性值被修改时,会调用此函数。该方法接受一个参数(也就是被赋予的新值),会传入赋值时的 this 对象。 默认为 undefined。
				set(newValue) {
					if (newValue === data[key]) {
						return;
					}
					return (data[key] = newValue);
				},
			});
		});
	}
}

/**
 *  负责数据劫持
 *  把 $data 中的成员转换成 getter/setter
 */
class Observer {
	constructor(data) {
		this.walk(data);
	}

	// 1. 判断数据是否是对象,如果不是对象返回
	// 2. 如果是对象,遍历对象的所有属性,设置为 getter/setter
	walk(data) {
		// 1. 判断data是否是对象
		if (!data || typeof data !== "object") return;

		// 2. 遍历data对象的所有属性
		Object.keys(data).forEach((key) => {
			this.defineReactive(data, key, data[key]);
		});
	}

	// 定义响应式成员
	defineReactive(obj, key, val) {
		const that = this;
		// 负责收集依赖,并发送通知
		let dep = new Dep();
		// 如果val是对象,把val内部的属性转换成响应式数据
		this.walk(val);
		Object.defineProperty(obj, key, {
			enumerable: true,
			configurable: true,
			get() {
				// 收集依赖
				Dep.target && dep.addSub(Dep.target);
				return val;
			},
			set(newValue) {
				if (newValue === val) return;
				val = newValue;
				// 如果 newValue 是对象,设置 newValue 的成员为响应式
				that.walk(newValue);
				// 发送通知
				dep.notify();
			},
		});
	}
}

/**
 * 负责解析指令/插值表达式
 */
class Compiler {
	constructor(vm) {
		this.el = vm.$el;
		this.vm = vm;
		this.compile(this.el);
	}

	// 编译模板
	// 处理文本节点和元素节点
	compile(el) {
		let childNodes = el.childNodes;
		Array.from(childNodes).forEach((node) => {
			// 处理文本节点
			if (this.isTextNode(node)) {
				this.compileText(node);
			} else if (this.isElementNode(node)) {
				// 处理元素节点
				this.compileElement(node);
			}

			// 判断node的节点是否有子节点,如果有子节点要递归调用compile。
			if (node.childNodes && node.childNodes.length) {
				this.compile(node);
			}
		});
	}

	// 编译元素节点,处理指令
	compileElement(node) {
		// 遍历所有的属性节点
		Array.from(node.attributes).forEach((attr) => {
			// 获取元素属性的名称
			let attrName = attr.name;
			// 判断当前的属性名称是否是指令
			if (this.isDirective(attrName)) {
				// attrName 的形式 v-text  v-model
				// 截取属性的名称,获取 text model
				attrName = attrName.substring(2);
				// 获取属性的名称,属性的名称就是我们数据对象的属性 v-text="name",获取的是name;
				const key = attr.value;
				// 处理不同的指令
				this.update(node, key, attrName);
			}
		});
	}

	// 负责更新 DOM
	// 创建 Watche
	update(node, key, dir) {
		// node 节点,key 数据的属性名称,dir 指令的后半部分
		const updaterFn = this[dir + "Updater"];
		// textUpdater等中要使用 this
		updaterFn && updaterFn.call(this, node, this.vm[key], key);
	}

	// v-text 指令的更新方法
	textUpdater(node, value, key) {
		node.textContent = value;
		// 每一个指令中创建一个 watcher,观察数据的变化
		new Watcher(this.vm, key, (newValue) => {
			node.textContent = newValue;
		});
	}

	// v-model 指令的更新方法
	modelUpdater(node, value, key) {
		node.value = value;
		// 每一个指令中创建一个 watcher,观察数据的变化
		new Watcher(this.vm, key, (newValue) => {
			node.value = newValue;
		});

		// 双向绑定
		node.addEventListener("input", () => {
			this.vm[key] = node.value;
		});
	}

	// 编译文本节点,处理插值表达式
	compileText(node) {
		// 匹配插值表达式,提取内容
		let reg = /\{\{(.+?)\}\}/;
		let value = node.textContent;
		if (reg.test(value)) {
			let key = RegExp.$1.trim();
			node.textContent = value.replace(reg, this.vm[key]);

			// 创建watcher对象,当数据改变更新视图
			new Watcher(this.vm, key, (newValue) => {
				node.textContent = newValue;
			});
		}
	}

	// 判断元素属性是否是指令
	isDirective(attrName) {
		return attrName.startsWith("v-");
	}

	// 判断节点是否是文本节点
	isTextNode(node) {
		return node.nodeType === 3;
	}

	// 判断节点是否是元素节点
	isElementNode(node) {
		return node.nodeType === 1;
	}
}

class Dep {
	constructor() {
		// 存储所有的观察者
		this.subs = [];
	}

	/**添加观察者 */
	addSub(sub) {
		if (sub && sub.update) {
			this.subs.push(sub);
		}
	}

	/**通知所有观察者 */
	notify() {
		this.subs.forEach((sub) => {
			sub.update();
		});
	}
}

class Watcher {
	constructor(vm, key, cb) {
		this.vm = vm;
		// data 中的属性名称
		this.key = key;
		// 当数据变化的时候,调用 cb 更新视图
		this.cb = cb;
		// 在 Dep 的静态属性上记录当前 watcher 对象,当访问数据的时候把 watcher 添加到 dep 的 subs 中
		Dep.target = this;
		// 触发一次 getter,让 dep 为当前 key 记录 watcher
		this.oldValue = vm[key];
		// 清空 target
		Dep.target = null;
	}

	// 触发更新
	update() {
		const newValue = this.vm[this.key];
		if (this.oldValue === newValue) {
			return;
		}
		this.cb(newValue);
	}
}

转载自:https://juejin.cn/post/7175824342503129125
评论
请登录