自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue2剝絲抽繭-響應(yīng)式系統(tǒng)之異步隊(duì)列

開(kāi)發(fā) 前端
試想一下如果這里的 console.log 是渲染頁(yè)面,那改變一次值就刷新一下頁(yè)面,會(huì)造成嚴(yán)重的性能問(wèn)題,頁(yè)面也會(huì)不停的改變。

Vue2 源碼從零詳解系列文章, 還沒(méi)有看過(guò)的同學(xué)可能需要看一下之前的,vue.windliang.wang。

場(chǎng)景

import { observe } from "./reactive";
import Watcher from "./watcher";

const data = {
a: 1,
b: 2,
c: 3,
};
observe(data);
const updateComponent = () => {
console.log(data.a + data.b);
};

new Watcher(updateComponent);

const updateComponent2 = () => {
console.log(data.c);
};
new Watcher(updateComponent2);

data.a = 2;
data.a = 3;
data.b = 4;

data.c = 5;

new Watcher(updateComponent) 進(jìn)行依賴(lài)收集會(huì)輸出一次 3 ,new Watcher(updateComponent2) 進(jìn)行依賴(lài)收集也會(huì)輸出一次 3 。

之后我們依次改變 a、 a 、b、c 的值,每改變一次就會(huì)觸發(fā) Watcher 的執(zhí)行,會(huì)連續(xù)進(jìn)行四次的 console.log。

試想一下如果這里的 console.log 是渲染頁(yè)面,那改變一次值就刷新一下頁(yè)面,會(huì)造成嚴(yán)重的性能問(wèn)題,頁(yè)面也會(huì)不停的改變。

解決方案

我們可以通過(guò)一個(gè)隊(duì)列,收集所有的 Watcher 。

那什么時(shí)候執(zhí)行 Watcher 隊(duì)列呢?

為了等所有的 Watcher 都收集完畢,可以將 Watcher 的執(zhí)行放到 setTimeout 中。這樣當(dāng)主線程全部執(zhí)行后,才會(huì)去執(zhí)行 Watcher 隊(duì)列。

代碼實(shí)現(xiàn)

我們可以給每一個(gè) Watcher 加上一個(gè) id,如果隊(duì)列中已經(jīng)有 id 了就不加入隊(duì)列。

let uid = 0;

export default class Watcher {
constructor(Fn, options) {
this.getter = Fn;
this.depIds = new Set(); // 擁有 has 函數(shù)可以判斷是否存在某個(gè) id
this.deps = [];
this.newDeps = []; // 記錄新一次的依賴(lài)
this.newDepIds = new Set();
/******新增 *************************/
this.id = ++uid; // uid for batching
// options
if (options) {
this.sync = !!options.sync;
}
/************************************/
this.get();
}
...
}

我們同時(shí)提供了一個(gè) options 對(duì)象,保存了其中的 sync 字段,表示是像之前一樣立即出觸發(fā) Watcher 還是放到隊(duì)列中。

然后 Watcher 的 update 方法中我們?nèi)フ{(diào)用加入隊(duì)列的函數(shù)。

export default class Watcher {
...
update() {
if (this.sync) {
this.run(); // 直接運(yùn)行
} else {
queueWatcher(this); // 加入隊(duì)列
}
}
...
}

看一下 queueWatcher 的實(shí)現(xiàn)。

const queue = []; // 保存 Watcher 隊(duì)列
let has = {}; // 去重 Watcher
let waiting = false; // 是否加入到了 setTimeout 隊(duì)列

export function queueWatcher(watcher) {
const id = watcher.id;
if (has[id] == null) {
has[id] = true;
queue.push(watcher); // 加入隊(duì)列
// queue the flush
if (!waiting) { // 執(zhí)行 Watcher 函數(shù)放到 setTimeout 隊(duì)列中,只加入一次即可
waiting = true;
setTimeout(flushSchedulerQueue, 0);
}
}
}

再看一下上邊執(zhí)行 Watcher 隊(duì)列的 flushSchedulerQueue 函數(shù)的實(shí)現(xiàn)。

let flushing = false; // 是否正在執(zhí)行隊(duì)列
let index = 0;
/**
* Flush both queues and run the watchers.
*/
function flushSchedulerQueue() {
flushing = true;
let watcher, id;
for (index = 0; index < queue.length; index++) {
watcher = queue[index];
id = watcher.id;
has[id] = null;
watcher.run();
}

resetSchedulerState(); // 執(zhí)行結(jié)束后進(jìn)行重置
}

/**
* Reset the scheduler's state.
*/
function resetSchedulerState() {
index = queue.length = 0;
has = {};
waiting = flushing = false;
}

總體上就是上邊的樣子了。

執(zhí)行結(jié)果

import { observe } from "./reactive";
import Watcher from "./watcher";

const data = {
a: 1,
b: 2,
c: 3,
};
observe(data);
const updateComponent = () => {
console.log(data.a + data.b);
};

new Watcher(updateComponent);

const updateComponent2 = () => {
console.log(data.c);
};
new Watcher(updateComponent2);

data.a = 2;
data.a = 3;
data.b = 4;

data.c = 5;

雖然后邊我們改變了四次 data 中的值,但事實(shí)上只有兩個(gè) Watcher ,因此只會(huì)輸出兩次。

總結(jié)

通過(guò)異步的一個(gè)隊(duì)列,當(dāng)所有 Watcher 收集完畢后統(tǒng)一執(zhí)行,進(jìn)行了性能方面的優(yōu)化。

責(zé)任編輯:武曉燕 來(lái)源: windliang
相關(guān)推薦

2022-03-29 09:59:58

響應(yīng)式系統(tǒng)Vue2

2022-04-06 07:28:47

數(shù)組響應(yīng)式系統(tǒng)

2022-04-02 09:56:41

Vue2響應(yīng)式系統(tǒng)

2022-04-14 08:46:46

響應(yīng)式系統(tǒng)js

2022-04-03 19:27:35

Vue2響應(yīng)式系統(tǒng)

2022-03-31 10:15:10

分支切換響應(yīng)式系統(tǒng)

2022-04-10 11:04:40

響應(yīng)式系統(tǒng)setdelete

2022-08-31 08:09:35

Vue2AST模版

2011-08-09 09:41:52

iPhoneASINetworkQ異步隊(duì)列

2024-09-02 16:10:19

vue2前端

2024-03-07 12:54:06

數(shù)據(jù)分析師企業(yè)

2023-03-02 11:51:00

數(shù)據(jù)分析師企業(yè)

2021-05-19 14:25:19

前端開(kāi)發(fā)技術(shù)

2019-04-25 14:20:56

數(shù)據(jù)分析套路工具

2022-06-26 00:00:02

Vue3響應(yīng)式系統(tǒng)

2024-03-15 11:47:19

Vue2前端權(quán)限控制

2021-03-09 22:29:46

Vue 響應(yīng)式API

2023-11-19 18:53:27

Vue2MVVM

2016-10-19 20:47:55

vuevue-cli移動(dòng)端

2020-09-25 07:40:39

技術(shù)開(kāi)發(fā)選型
點(diǎn)贊
收藏

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