Vue之Dep和Observer的用法及說明
Vue 中響應(yīng)式系統(tǒng)利用了訂閱發(fā)布模式來實(shí)現(xiàn)基本的邏輯。本文將介紹其中的兩個(gè)重要角色,他們就是Dep和Observer。其中Observer 是觀察者和 Dep是訂閱收集和發(fā)布者。另外watcher是作為訂閱者的角色。
本文將重點(diǎn)將Observer和Dep。
一:Observer
vue 通過Observer 構(gòu)造函數(shù),為響應(yīng)式變量添加訪問和賦值的get set的回調(diào)。
? var Observer = function Observer (value) { ? ? this.value = value; ? ? this.dep = new Dep(); ? ? this.vmCount = 0; ? ? def(value, '__ob__', this); ? ? if (Array.isArray(value)) { ? ? ? if (hasProto) { ? ? ? ? protoAugment(value, arrayMethods); ? ? ? } else { ? ? ? ? copyAugment(value, arrayMethods, arrayKeys); ? ? ? } ? ? ? this.observeArray(value); ? ? } else { ? ? ? this.walk(value); // 這是非數(shù)組值添加get set 回調(diào) ? ? } ? };
批量為obj上的key添加get set的回調(diào)
? Observer.prototype.walk = function walk (obj) { ? ? var keys = Object.keys(obj); ? ? for (var i = 0; i < keys.length; i++) { ? ? ? defineReactive$$1(obj, keys[i]); ? ? } ? };
// 這個(gè)是為obj上的key添加get set方法的核心邏輯 ? function defineReactive$$1 ( ? ? obj, ? ? key, ? ? val, ? ? customSetter, ? ? shallow ? ) { ? ? var dep = new Dep(); ? ? var property = Object.getOwnPropertyDescriptor(obj, key); ? ? if (property && property.configurable === false) { ? ? ? return ? ? } ? ? // cater for pre-defined getter/setters ? ? var getter = property && property.get; ? ? var setter = property && property.set; ? ? if ((!getter || setter) && arguments.length === 2) { ? ? ? val = obj[key]; ? ? } ? ? var childOb = !shallow && observe(val); ? ? Object.defineProperty(obj, key, { ? ? ? enumerable: true, ? ? ? configurable: true, ? ? ? get: function reactiveGetter () { ? ? ? ? ?// 省略N行 ? ? ? }, ? ? ? set: function reactiveSetter (newVal) { ? ? ? ?// 省略N行 ? ? ? } ? ? }); ? }
有了這一層,當(dāng)數(shù)據(jù)獲取和修改就會(huì)觸發(fā)這里的get 和 set
二:Dep 依賴關(guān)系
變量能夠響應(yīng)數(shù)據(jù)的獲取和修改對于一個(gè)透明處理dom更新的框架來說是不夠的,框架還需要處理變量和更新dom的watcher的依賴關(guān)系。Dep就是為了干這個(gè)事情的。
在之前的文章里,介紹過Watcher,vue中的組件在掛載前,都會(huì)基于組件的render函數(shù),生成一個(gè)Watcher實(shí)例,并執(zhí)行render函數(shù)來進(jìn)行渲染
渲染dom的時(shí)候我們需要讀取變量,這個(gè)時(shí)候就會(huì)觸發(fā)我們上一步Observer為每個(gè)變量里量身定做的get方法。那vue在get里做了什么事情呢?
請看下面一段代碼中的中文注釋:
function defineReactive$$1 ( ? ? obj, ? ? key, ? ? val, ? ? customSetter, ? ? shallow ? ) { ? ? var dep = new Dep(); // 創(chuàng)建一個(gè)Dep實(shí)例 ? ? var property = Object.getOwnPropertyDescriptor(obj, key); ? ? if (property && property.configurable === false) { ? ? ? return ? ? } ? ? // cater for pre-defined getter/setters ? ? var getter = property && property.get; ? ? var setter = property && property.set; ? ? if ((!getter || setter) && arguments.length === 2) { ? ? ? val = obj[key]; ? ? } ? ? var childOb = !shallow && observe(val); ? ? Object.defineProperty(obj, key, { ? ? ? enumerable: true, ? ? ? configurable: true, ? ? ? get: function reactiveGetter () { ? ? ? ? var value = getter ? getter.call(obj) : val; ? ? ? ? if (Dep.target) { ? ? ? ? ? //在此處將當(dāng)前watcher加入到dep中 ? ? ? ? ? dep.depend();? ? ? ? ? ? if (childOb) { ? ? ? ? ? ? childOb.dep.depend(); ? ? ? ? ? ? if (Array.isArray(value)) { ? ? ? ? ? ? ? dependArray(value); ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? } ? ? ? ? return value ? ? ? },
此處有一個(gè)Dep.target 存在的條件判斷,什么是Dep.target
Dep.target = null;
當(dāng)watcher實(shí)例創(chuàng)建完,默認(rèn)回去調(diào)用get方法來渲染組件界面
這個(gè)時(shí)候會(huì)通過pushTarget(this) 將當(dāng)前watcher設(shè)置到Dep.target上去。
? Watcher.prototype.get = function get () { ? ?// 設(shè)置當(dāng)前watcher到全局變量上去 ? ? pushTarget(this); ? ? var value; ? ? // 省略N行代碼 ? ? return value ? };
pushTarget的具體實(shí)現(xiàn)
?function pushTarget (target) { ? ? targetStack.push(target); ? ? Dep.target = target; ? }
這一步完成后,在調(diào)用render函數(shù)觸發(fā)dom中響應(yīng)式變量的get的時(shí)候
下面的代碼就能夠執(zhí)行了
?if (Dep.target) { ? ? ? ? ? //在此處將當(dāng)前watcher加入到dep中 ? ? ? ? ? dep.depend();?
dep.depend 干了啥呢?
接著看:
?Dep.prototype.depend = function depend () { ? ? // Dep.target 對應(yīng)的就是上面的全局變量里的watcher ? ? if (Dep.target) { ? ? ? Dep.target.addDep(this); ? ? } ? };
也就是調(diào)用了watcher.addDep(this)
? Watcher.prototype.addDep = function addDep (dep) { ? ? var id = dep.id; ? ? if (!this.newDepIds.has(id)) { ? ? ? this.newDepIds.add(id); ? ? ? // ?會(huì)在watcher的實(shí)際例子里維護(hù)一個(gè)所以訂閱的dep的數(shù)組 ? ? ? this.newDeps.push(dep);? ? ? ? if (!this.depIds.has(id)) { ? ? ? ? dep.addSub(this);? ? ? ? } ? ? } ? }; ? Dep.prototype.addSub = function addSub (sub) { ? // 繞了一大圈,最終就是把watcher 塞到dep實(shí)例的subs數(shù)組里了。 ? ? this.subs.push(sub); ? };
到此,dom的渲染訂閱者和數(shù)據(jù)的觀察發(fā)布者就關(guān)聯(lián)上了。
等用戶修改數(shù)據(jù)的時(shí)候,觸發(fā)為響應(yīng)數(shù)據(jù)設(shè)計(jì)的set 回調(diào)函數(shù)
set: function reactiveSetter (newVal) { ? ? ? ? var value = getter ? getter.call(obj) : val; ? ? ? ? /* eslint-disable no-self-compare */ ? ? ? ? if (newVal === value || (newVal !== newVal && value !== value)) { ? ? ? ? ? return ? ? ? ? } ? ? ? ? /* eslint-enable no-self-compare */ ? ? ? ? if (customSetter) { ? ? ? ? ? customSetter(); ? ? ? ? } ? ? ? ? // #7981: for accessor properties without setter ? ? ? ? if (getter && !setter) { return } ? ? ? ? if (setter) { ? ? ? ? ? setter.call(obj, newVal); ? ? ? ? } else { ? ? ? ? ? val = newVal; ? ? ? ? } ? ? ? ? childOb = !shallow && observe(newVal); ? ? ? ? // 重點(diǎn)看這里,這里是發(fā)布者發(fā)布數(shù)據(jù)變化的消息 ? ? ? ? // dep通知所有依賴的watcher ? ? ? ? dep.notify(); ? ? ? } ? ? });
dep.notify做了啥事呢?
? Dep.prototype.notify = function notify () { ? ? // stabilize the subscriber list first ? ? // subs就是上面get回調(diào)里,我們用來加入watcher依賴的數(shù)組 ? ? var subs = this.subs.slice();? ? ? if (!config.async) { ? ? ? // subs aren't sorted in scheduler if not running async ? ? ? // we need to sort them now to make sure they fire in correct ? ? ? // order ? ? ? subs.sort(function (a, b) { return a.id - b.id; }); ? ? } ? ? for (var i = 0, l = subs.length; i < l; i++) { ? ? // ?遍歷調(diào)用watcher的update方法 ? ? ? subs[i].update();? ? ? } ? };
Dep 的構(gòu)造函數(shù)和其他實(shí)例方法:
? // Dep的構(gòu)造函數(shù) ? var Dep = function Dep () { ? ? this.id = uid++; ? ? this.subs = []; ? }; // ?取消訂閱 ? Dep.prototype.removeSub = function removeSub (sub) { ? ? remove(this.subs, sub); ? };
其他相關(guān)
// 移除deps數(shù)組里的不在newDeps里的老的依賴,將之前newDeps收集的dep依賴丟給this.deps ? Watcher.prototype.cleanupDeps = function cleanupDeps () { ? ? var i = this.deps.length; ? ? while (i--) { ? ? ? var dep = this.deps[i]; ? ? ? if (!this.newDepIds.has(dep.id)) { ? ? ? ? dep.removeSub(this); ? ? ? } ? ? } ? ? var tmp = this.depIds; ? ? this.depIds = this.newDepIds; ? ? this.newDepIds = tmp; ? ? this.newDepIds.clear(); ? ? tmp = this.deps; ? ? this.deps = this.newDeps; ? ? this.newDeps = tmp; ? ? this.newDeps.length = 0; ? };
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue中__ob__:?Observer的踩坑記錄
- vue3?圖片懶加載的兩種方式、IntersectionObserver和useIntersectionObserver實(shí)例詳解
- 關(guān)于Vue?"__ob__:Observer"屬性的解決方案詳析
- Vue2?Observer實(shí)例dep和閉包中dep區(qū)別詳解
- vue中關(guān)于_ob_:observer的處理方式
- Vue數(shù)組中出現(xiàn)__ob__:Observer無法取值問題的解決方法
- Vue響應(yīng)式原理Observer、Dep、Watcher理解
- vue中{__ob__: observer}對象轉(zhuǎn)化為數(shù)組進(jìn)行遍歷方式
相關(guān)文章
vue實(shí)現(xiàn)幾秒后跳轉(zhuǎn)新頁面代碼
這篇文章主要介紹了vue實(shí)現(xiàn)幾秒后跳轉(zhuǎn)新頁面代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁功能(附demo)
這篇文章主要介紹了vue2.0嵌套路由實(shí)現(xiàn)豆瓣電影分頁功能(附demo),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決
這篇文章主要介紹了Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10