成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Vue2剝絲抽繭-響應式系統之嵌套

開發 前端
對應于 Watcher 的收集,我們同樣可以使用一個棧來保存,執行函數前將 Watcher 壓入棧,執行函數完畢后將 Watcher 彈出棧即可。其中,Dep.target 始終指向棧頂 Watcher ,代表當前正在執行的函數。

場景

在 Vue 開發中肯定存在組件嵌套組件的情況,類似于下邊的樣子。

<!-- parent-component -->
<div>
<my-component :text="inner"></my-component>
{{ text }}
<div>

<!-- my-component-->
<div>{{ text }}</div>

回到我們之前的響應式系統,模擬一下上邊的情況:

import { observe } from "./reactive";
import Watcher from "./watcher";
const data = {
text: "hello, world",
inner: "內部",
};
observe(data);

const updateMyComponent = () => {
console.log("子組件收到:", data.inner);
};

const updateParentComponent = () => {
new Watcher(updateMyComponent);
console.log("父組件收到:", data.text);
};

new Watcher(updateParentComponent);

data.text = "hello, liang";

可以先 1 分鐘考慮一下上邊輸出什么?

首先回憶一下 new Watcher 會做什么操作。

第一步是保存當前函數,然后執行當前函數前將全局的 Dep.target 賦值為當前 Watcher 對象。

接下來執行 getter 函數的時候,如果讀取了相應的屬性就會觸發 get ,從而將當前 Watcher 收集到該屬性的 Dep 中。

執行過程

import { observe } from "./reactive";
import Watcher from "./watcher";
const data = {
text: "hello, world",
inner: "內部",
};
observe(data);

const updateMyComponent = () => {
console.log("子組件收到:", data.inner);
};

const updateParentComponent = () => {
new Watcher(updateMyComponent);
console.log("父組件收到:", data.text);
};

new Watcher(updateParentComponent);

data.text = "hello, liang";

我們再一步一步理清一下:

  • new Watcher(updateParentComponent);

將 Dep.target 賦值為保存了 updateParentComponent 函數的 Watcher 。

接下來執行 updateParentComponent 函數。

  • new Watcher(updateMyComponent);

將 Dep.target 賦值為保存了 updateMyComponent 函數的 Watcher 。

接下來執行 updateMyComponent 函數。

const updateMyComponent = () => {
console.log("子組件收到:", data.inner);
};

// 讀取了 inner 變量。
// data.inner 的 Dep 收集當前 Watcher(保存了 `updateMyComponent` 函數)
const updateParentComponent = () => {
new Watcher(updateMyComponent);
console.log("父組件收到:", data.text);
};
// 讀取了 text 變量。
// data.text 的 Dep 收集當前 Watcher (保存了 `updateMyComponent` 函數)
  • data.text = "hello, liang";

觸發 text 的 set 函數,執行它依賴的 Watcher ,而此時是 updateMyComponent 函數。

所以上邊代碼最終輸出的結果是:

子組件收到: 內部  // new Watcher(updateMyComponent); 時候輸出
父組件收到:hello, world // new Watcher(updateParentComponent); 時候輸出
子組件收到: 內部 // data.text = "hello, liang"; 輸出

然而子組件并不依賴 data.text,依賴 data.text 的父組件反而沒有執行。

修復

上邊的問題出在我們保存當前正在執行 Watcher 時候使用的是單個變量 Dep.target = null; // 靜態變量,全局唯一。

回憶一下學習 C 語言或者匯編語言的時候對函數參數的處理:

function b(p) {
console.log(p);
}

function a(p) {
b("child");
console.log(p);
}

a("parent");

當函數發生嵌套調用的時候,執行 a 函數的時候我們會先將參數壓入棧中,然后執行 b 函數,同樣將參數壓入棧中,b 函數執行完畢就將參數出棧。此時回到 a 函數就能正確取到 p 參數的值了。

對應于 Watcher 的收集,我們同樣可以使用一個棧來保存,執行函數前將 Watcher 壓入棧,執行函數完畢后將 Watcher 彈出棧即可。其中,Dep.target 始終指向棧頂 Watcher ,代表當前正在執行的函數。

回到 Dep 代碼中,我們提供一個壓棧和出棧的方法。

import { remove } from "./util";

let uid = 0;

export default class Dep {
... 省略
}
Dep.target = null; // 靜態變量,全局唯一

// The current target watcher being evaluated.
// This is globally unique because only one watcher
// can be evaluated at a time.
const targetStack = [];

export function pushTarget(target) {
targetStack.push(target);
Dep.target = target;
}

export function popTarget() {
targetStack.pop();
Dep.target = targetStack[targetStack.length - 1]; // 賦值為棧頂元素
}

然后 Watcher 中,執行函數之前進行入棧,執行后進行出棧。

import { pushTarget, popTarget } from "./dep";
export default class Watcher {
constructor(Fn) {
this.getter = Fn;
this.depIds = new Set(); // 擁有 has 函數可以判斷是否存在某個 id
this.deps = [];
this.newDeps = []; // 記錄新一次的依賴
this.newDepIds = new Set();
this.get();
}

/**
* Evaluate the getter, and re-collect dependencies.
*/
get() {
/************修改的地方*******************************/
pushTarget(this); // 保存包裝了當前正在執行的函數的 Watcher
/*******************************************/
let value;
try {
value = this.getter.call();
} catch (e) {
throw e;
} finally {
/************修改的地方*******************************/
popTarget();
/*******************************************/
this.cleanupDeps();
}
return value;
}
...
}

測試

回到開頭的場景,再來執行一下:

import { observe } from "./reactive";
import Watcher from "./watcher";
const data = {
text: "hello, world",
inner: "內部",
};
observe(data);

const updateMyComponent = () => {
console.log("子組件收到:", data.inner);
};

const updateParentComponent = () => {
new Watcher(updateMyComponent);
console.log("父組件收到:", data.text);
};

new Watcher(updateParentComponent);

data.text = "hello, liang";

執行 new Watcher(updateParentComponent); 的時候將 Watcher 入棧。

進入 updateParentComponent 函數,執行 new Watcher(updateMyComponent); 的時候將 Watcher 入棧。

執行 updateMyComponent 函數,data.inner 收集當前 Dep.target ,執行完畢后 Watcher 出棧。

繼續執行 updateParentComponent 函數,data.text 收集當前 Dep.target 。

此時依賴就變得正常了,data.text 會觸發 updateParentComponent 函數,從而輸出如下:

子組件收到: 內部
父組件收到:hello, world
子組件收到: 內部
父組件收到:hello, liang

總結

今天這個相對好理解一些,通過棧解決了嵌套調用的情況。

責任編輯:武曉燕 來源: windliang
相關推薦

2022-03-29 09:59:58

響應式系統Vue2

2022-04-06 07:28:47

數組響應式系統

2022-04-14 08:46:46

響應式系統js

2022-04-03 19:27:35

Vue2響應式系統

2022-04-12 10:05:18

響應式系統異步隊列

2022-03-31 10:15:10

分支切換響應式系統

2022-04-10 11:04:40

響應式系統setdelete

2022-08-31 08:09:35

Vue2AST模版

2024-09-02 16:10:19

vue2前端

2023-03-02 11:51:00

數據分析師企業

2024-03-07 12:54:06

數據分析師企業

2019-04-25 14:20:56

數據分析套路工具

2021-05-19 14:25:19

前端開發技術

2024-03-15 11:47:19

Vue2前端權限控制

2022-06-26 00:00:02

Vue3響應式系統

2021-03-09 22:29:46

Vue 響應式API

2023-11-19 18:53:27

Vue2MVVM

2016-10-19 20:47:55

vuevue-cli移動端

2020-09-25 07:40:39

技術開發選型

2019-12-06 10:44:53

Vue 3.0響應式系統前端
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本xx视频免费观看 | 综合久久av | 一区二区三区中文字幕 | 精品一区精品二区 | 91视频大全 | 中文在线日韩 | 日韩中文字幕在线观看 | 色五月激情五月 | 久久国产精品免费一区二区三区 | 2021狠狠干 | 国产精品久久国产精品 | 男人天堂视频在线观看 | 免费看黄色视屏 | 91欧美精品成人综合在线观看 | 91在线观看免费视频 | 米奇成人网 | 精品一区二区在线看 | 大象视频一区二区 | 午夜播放器在线观看 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 亚洲成在线观看 | 黄色网络在线观看 | 涩涩鲁亚洲精品一区二区 | 成人免费看片又大又黄 | 国产精品资源在线 | 亚洲麻豆 | 中文字幕在线视频免费观看 | 亚洲欧洲中文日韩 | 一区二区三区回区在观看免费视频 | 日日操天天射 | 久久亚洲国产精品日日av夜夜 | 亚洲色图图片 | 国产成人免费在线 | 91精品久久久久久久久 | 日本视频免费 | 美女黄网站 | 成人在线一区二区三区 | 日本天天操 | 日日操视频 | 国产精品99| 精品乱码久久久久 |