偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

「源碼剖析」NextTick到底有什么作用

開發(fā) 前端
在vue中每次監(jiān)聽到數(shù)據(jù)變化的時候,都會去調(diào)用notify通知依賴更新,觸發(fā)watcher中的update方法。

[[379888]]

 在vue中每次監(jiān)聽到數(shù)據(jù)變化的時候,都會去調(diào)用notify通知依賴更新,觸發(fā)watcher中的update方法。

  1. update () { 
  2.    /* istanbul ignore else */ 
  3.    if (this.lazy) { 
  4.      
  5.    } else if (this.sync) { 
  6.    
  7.    } else { 
  8.      this.get() 
  9.      //queueWatcher(this) 
  10.    } 
  11.  } 

如果通過watcher中的get方法去重新渲染組件,那么在渲染的過程中假如多次更新數(shù)據(jù)會導(dǎo)致同一個watcher被觸發(fā)多次,這樣會導(dǎo)致重復(fù)的數(shù)據(jù)計算和DOM的操作。如下圖所示,修改3次message之后DOM被操作了3次。


為了解決上述問題,不去直接調(diào)用get方法而是將每次調(diào)用update方法后需要批處理的wather暫存到一個隊列當(dāng)中,如果同一個 watcher 被多次觸發(fā),通過wacther 的id屬性對其去重,只會被推入到隊列中一次。然后,等待所有的同步代碼執(zhí)行完畢之后在下一個的事件循環(huán)中,Vue 刷新隊列并執(zhí)行實際 (已去重的) 工作。

  1. let has: { [key: number]: ?true } = {} 
  2. let waiting = false 
  3. export function queueWatcher (watcher: Watcher) { 
  4.   const id = watcher.id  //對watcher去重 
  5.   if (has[id] == null) { 
  6.     has[id] = true 
  7.     queue.push(watcher); 
  8.     if (!waiting) {  //節(jié)流 
  9.       waiting = true 
  10.       nextTick(flushSchedulerQueue) 
  11.     } 

調(diào)用watcher的run方法異步更新DOM

  1. let has: { [key: number]: ?true } = {} 
  2. function flushSchedulerQueue () { 
  3.   let watcher, id 
  4.   queue.sort((a, b) => a.id - b.id) 
  5.  
  6.   for (index = 0; index < queue.length; index++) { 
  7.     watcher = queue[index
  8.     if (watcher.before) { 
  9.       watcher.before() 
  10.     } 
  11.     id = watcher.id 
  12.     has[id] = null  //清空id 
  13.     watcher.run()   //更新值 
  14.   } 
  15.    
  16.   resetSchedulerState()   //清空watcher隊列 
  17.  
  18. function resetSchedulerState () { 
  19.   index = queue.length  = 0 
  20.   has = {} 
  21.   waiting =  false 

在vue內(nèi)部調(diào)用nextTick(flushSchedulerQueue),vm.$nextTick方法調(diào)用的也是nextTick()方法

  1. Vue.prototype.$nextTick = function (cb) { 
  2.    nextTick(cb,this); 
  3.  }; 

那么多次調(diào)用nextTick方法是怎么處理的呢?

  1. const callbacks = [] 
  2. let pending = false  
  3. export function nextTick (cb?: Function, ctx?: Object) { 
  4.   callbacks.push(() => { 
  5.     if (cb) { 
  6.       try { 
  7.         cb.call(ctx) 
  8.       } catch (e) { 
  9.         handleError(e, ctx, 'nextTick'
  10.       } 
  11.     } 
  12.   }) 
  13.   if (!pending) { 
  14.     pending = true 
  15.     timerFunc() 
  16.   } 

nextTick將所有的回調(diào)函數(shù)暫存到了一個隊列中,然后通過異步調(diào)用更新去依次執(zhí)行隊列中的回調(diào)函數(shù)。

  1. function flushCallbacks () { 
  2.   pending = false 
  3.   const copies = callbacks.slice(0) 
  4.   callbacks.length = 0 
  5.   for (let i = 0; i < copies.length; i++) { 
  6.     copies[i]() 
  7.   } 

 

nextTick函數(shù)中異步更新對兼容性做了處理,使用原生的 Promise.then、MutationObserver 和 setImmediate,如果執(zhí)行環(huán)境不支持,則會采用 setTimeout(fn, 0) 代替。

Promise

  1. if (typeof Promise !== 'undefined' && isNative(Promise)) { 
  2.   const p = Promise.resolve() 
  3.   timerFunc = () => { 
  4.     p.then(flushCallbacks) 
  5.   } 

 

MutationObserver

MutationObserver 它會在指定的DOM發(fā)生變化時被調(diào)用。創(chuàng)建了一個文本DOM,通過監(jiān)聽字符值的變化,當(dāng)文本字符發(fā)生變化的時候調(diào)用回調(diào)函數(shù)。

  1. if (!isIE && typeof MutationObserver !== 'undefined' && ( 
  2.   isNative(MutationObserver) || 
  3.   MutationObserver.toString() === '[object MutationObserverConstructor]' 
  4. )) { 
  5.   let counter = 1 
  6.   const observer = new MutationObserver(flushCallbacks) 
  7.   const textNode = document.createTextNode(String(counter)) 
  8.   observer.observe(textNode, { 
  9.     characterData: true 
  10.   }) 
  11.   timerFunc = () => { 
  12.     counter = (counter + 1) % 2 
  13.     textNode.data = String(counter) 
  14.   } 

 

setImmediate

setImmediate該方法用作把一些需要持續(xù)運行的操作放在一個其他函數(shù)里,在瀏覽器完成后面的其他語句后,就立即執(zhí)行此替換函數(shù)。

  1. if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) { 
  2.   timerFunc = () => { 
  3.     setImmediate(flushCallbacks) 
  4.   } 
  5. }else
  6.   timerFunc = () => { 
  7.     setTimeout(flushCallbacks, 0) 
  8.   } 

 

總結(jié)

vue渲染DOM的時候觸發(fā)set方法中的去依賴更新,在更新的過程中watcher不是每次都去執(zhí)行去觸發(fā)DOM的更新,而是通過對wather的去重之后,通過nextTick異步調(diào)用觸發(fā)DOM更新。

nextTick()就是一個異步函數(shù),在異步函數(shù)中通過隊列批處理nextTick傳入的回調(diào)函數(shù)cb,但是隊列彼此不是同時進行的,通過節(jié)流的方式依次執(zhí)行。

 

責(zé)任編輯:姜華 來源: 前端簡報
相關(guān)推薦

2024-10-15 09:48:56

2018-06-26 14:29:44

LinuxUnix不同

2024-02-26 07:36:09

lockJava語言

2022-09-14 09:45:15

指標(biāo)標(biāo)簽

2012-07-25 15:45:28

ERPSCM

2019-10-14 10:09:33

Wi-Fi 6Wi-Fi無線網(wǎng)絡(luò)

2021-09-06 10:45:18

XDRMDR

2022-09-01 21:02:31

手機衛(wèi)星5G

2016-09-22 16:47:55

iOSAndroidWindows Pho

2019-06-25 09:49:01

5G承載網(wǎng)網(wǎng)絡(luò)

2018-08-13 07:32:42

物聯(lián)網(wǎng)聯(lián)網(wǎng)物IOT

2020-02-27 08:52:51

NFVSDN網(wǎng)絡(luò)

2020-10-20 09:57:04

量子計算人工智能技術(shù)

2012-05-31 09:24:55

云計算云存儲

2022-09-27 09:43:08

物聯(lián)網(wǎng)設(shè)備物聯(lián)網(wǎng)

2021-03-09 05:49:32

通信女性從業(yè)者通信行業(yè)

2020-08-05 07:00:00

SSD硬盤存儲

2025-09-26 04:55:00

2022-10-27 19:32:20

切片golang數(shù)組

2022-07-29 08:25:02

volatileC語言原子
點贊
收藏

51CTO技術(shù)棧公眾號