網(wǎng)站首頁 編程語言 正文
前言
什么是computed計(jì)算屬性?它會(huì)根據(jù)所依賴的數(shù)據(jù)動(dòng)態(tài)顯示新的計(jì)算結(jié)果, 該計(jì)算結(jié)果會(huì)被緩存起來。如果是Vue開發(fā)者,對這個(gè)功能并不陌生,而且很常用。對于React開發(fā)者,如果用過mobx,那其實(shí)也不陌生,一個(gè)裝飾器就生效了??。那如果是Redux呢??(沉默中。。。)有了,reselect嘛,哈哈??。啪,騙子,這是假的計(jì)算屬性,它要手動(dòng)提供全部依賴,每個(gè)依賴都是一個(gè)函數(shù)回調(diào)確定依賴值,每次寫這么多代碼是有多想敲壞我的機(jī)械鍵盤(嘶吼)。
這么說,redux和計(jì)算屬性無緣?也不能這么說,辦法總比困難多。雖然redux是單向數(shù)據(jù)流,無法做響應(yīng)式操作,不過,我們可以創(chuàng)造出一個(gè)監(jiān)聽對象
import { Store } from 'redux';
const collector = [];
class ObjectDeps {
protected readonly deps: string[];
protected readonly name: string;
protected readonly store: Store;
protected snapshot: any;
constructor(store: Store, name: string, deps: string[] = []) {
this.store = store;
this.name = name;
this.deps = deps;
collector.push(this);
}
proxy(currentState) {
if (state === null || typeof state != 'object') return state;
const proxyData = Array.isArray(state) : [] : {};
const currentDeps = this.deps.slice();
const keys = Object.keys(currentState);
for (let i = keys.length; i-- > 0; ) {
const key = keys[i]!;
Object.defineProperty(proxyData, key, {
enumerable: true,
get: () => {
if (visited) {
return new ObjectDeps(
this.store,
this.name,
currentDeps.slice(),
).proxy(currentState)[key];
}
visited = true;
this.deps.push(key);
return this.proxy((this.snapshot = currentState[key]));
},
});
}
}
}
樸實(shí)無華,沒有基于ES6的Proxy,因?yàn)榧嫒菪圆缓谩<热皇乔岸说膽?yīng)用,自然是要照顧到ES5的環(huán)境的,因此選擇defineProerty是個(gè)不錯(cuò)的方案。
有了監(jiān)聽驅(qū)動(dòng),那監(jiān)聽豈不是易如反掌?
// 假設(shè)user里的對象為:{ firstName: 'lady', lastName: 'gaga' }
const userState = store.getState()['user'];
function computedFullName() {
const proxy = new ObjectDeps(store, 'user').proxy(userState);
return proxy.firstName + '-' + proxy.lastName;
}
const fullname = computedFullName();
現(xiàn)在我們看看collector里收集到多少個(gè)依賴
console.log(collector); // [ ObjectDeps, ObjectDeps ]
不錯(cuò),兩條依賴,第一條的deps鏈為['user', 'firstName'],第二條為['user', 'lastName']。
原理分析:
- 每次創(chuàng)建proxy時(shí),構(gòu)造函數(shù)均會(huì)執(zhí)行collector.push(this)向采集器加入自己。
- proxy訪問firstName時(shí),其實(shí)訪問的是getter,getter中有一條this.deps.push(key)立即收集依賴,并返回下一級的proxy值。以此類推,即使是proxy.a.b.c.d這種深度操作也來者不拒,因?yàn)槊看卧L問下一級都能收集依賴并合并到deps數(shù)組中。
- proxy訪問lastName時(shí),由于proxy實(shí)例其實(shí)已經(jīng)被firstName占用了(通過visited變量判斷),所以getter邏輯中會(huì)直接返回一個(gè)新的ObjectDeps實(shí)例,此時(shí)lastName已經(jīng)和我們看到的proxy變量沒有任何關(guān)系了。
自動(dòng)收集依賴已經(jīng)實(shí)現(xiàn)了,我們試一下如何緩存屬性
class ObjectDeps {
protected snapshot: any;
proxy() {...}
isDirty() {
return this.snapshot !== this.getSnapshot();
}
protected getSnapshot() {
const deps = this.deps;
let snapshot = this.store.getState();
for (let i = 0; i < deps.length; ++i) {
if (snapshot == null || typeof snapshot !== 'object') {
break;
}
snapshot = snapshot[deps[i]!];
}
return snapshot;
}
}
通過isDirty()的判斷,即再次獲得deps下的最新值和舊值做對比,便可以知道這個(gè)依賴是否為臟值。這一步便是緩存的關(guān)鍵。
現(xiàn)在你相信reselect是騙子了吧,明明可以自動(dòng)依賴,非要多寫幾行代碼增加心智負(fù)擔(dān)?拜托,不是每個(gè)人都需要KPI壓力的。
老師,我想直接在項(xiàng)目中使用上這個(gè)什么computed屬性,應(yīng)該去哪里找現(xiàn)成的呢?廢話,當(dāng)然是去山東找藍(lán)翔。看看藍(lán)翔大法:
import { defineModel, useComputed } from 'foca';
export const userModel = defineModel('user', {
initialState: {
firstName: 'lady',
lastName: 'gaga',
},
computed: {
// 清爽
fullName() {
return this.state.firstName + '-' + this.state.lastName;
},
},
});
// App.tsx
const App: FC = () => {
const fullName = useComputed(userModel.fullName);
return <div>{fullName}</div>;
};
嗯?剛剛發(fā)生了什么,好像看到dva飛過去?飛你個(gè)頭,是哥寫的React狀態(tài)管理庫foca,基于redux和react-redux,剛才的computed解析就是從里面摘抄的(具體實(shí)現(xiàn)邏輯請看這里)。雖然是個(gè)軟廣告,不過redux也算是支持computed了,各位大佬就不要天天噴redux這個(gè)不好那個(gè)不好了行吧??,二次封裝才是真愛。
人生苦短,手握神器,少寫代碼,早點(diǎn)下班最要緊:https://github.com/foca-js/foca
總結(jié)
原文鏈接:https://segmentfault.com/a/1190000041859375
相關(guān)推薦
- 2022-03-14 -bash: 未預(yù)期的符號 `(‘ 附近有語法錯(cuò)誤的解決辦法
- 2022-02-18 RuntimeError: CUDA error: invalid device ordinal
- 2022-05-11 為什么一級封鎖協(xié)議不能保證不讀取到臟數(shù)據(jù)
- 2022-03-26 C語言中指針常量和常量指針的區(qū)別_C 語言
- 2022-11-21 基于C++實(shí)現(xiàn)一個(gè)日期計(jì)算器_C 語言
- 2021-12-10 k8s部署ingress-nginx的方法步驟_nginx
- 2022-07-01 淺談C語言中的sizeof()和strlen()的區(qū)別_C 語言
- 2022-04-07 你知道怎么在?HTML?頁面中使用?React嗎_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支