八種現(xiàn)代 JavaScript 響應(yīng)式模式
響應(yīng)性本質(zhì)上是關(guān)于系統(tǒng)如何對數(shù)據(jù)變化作出反應(yīng),有不同類型的響應(yīng)性。然而,在這篇文章中,我們關(guān)注的是響應(yīng)性,即響應(yīng)數(shù)據(jù)變化而采取行動。
作為一名前端開發(fā)者,Pavel Pogosov 每天都要面對這個問題。因?yàn)闉g覽器本身是一個完全異步的環(huán)境。現(xiàn)代 Web 界面必須快速響應(yīng)用戶的操作,這包括更新 UI、發(fā)送網(wǎng)絡(luò)請求、管理導(dǎo)航和執(zhí)行各種其他任務(wù)。
盡管人們常常將響應(yīng)性與框架聯(lián)系在一起,Pavel Pogosov 認(rèn)為通過純 JavaScript 實(shí)現(xiàn)響應(yīng)性可以學(xué)到很多。所以,我們將自己編寫一些模式代碼,并研究一些基于響應(yīng)性的原生瀏覽器 API。
目錄
- PubSub(發(fā)布-訂閱模式)
- 自定義事件作為瀏覽器版本的 PubSub
- 自定義事件目標(biāo)
- 觀察者模式
- 使用 Proxy 的響應(yīng)式屬性
- 單個對象屬性和響應(yīng)性
- 使用 MutationObserver 的響應(yīng)式 HTML 屬性
- 使用 IntersectionObserver 的響應(yīng)式滾動
1. PubSub(發(fā)布-訂閱模式)
class PubSub {
constructor() {
this.subscribers = {};
}
subscribe(event, callback) {
if (!this.subscribers[event]) {
this.subscribers[event] = [];
}
this.subscribers[event].push(callback);
}
// 向特定事件的所有訂閱者發(fā)布消息
publish(event, data) {
if (this.subscribers[event]) {
this.subscribers[event].forEach((callback) => {
callback(data);
});
}
}
}
const pubsub = new PubSub();
pubsub.subscribe('news', (message) => {
console.log(`訂閱者1收到了新聞:${message}`);
});
pubsub.subscribe('news', (message) => {
console.log(`訂閱者2收到了新聞:${message}`);
});
// 向 'news' 事件發(fā)布消息
pubsub.publish('news', '最新頭條新聞:...');
// 控制臺日志輸出:
// 訂閱者1收到了新聞:最新頭條新聞:...
// 訂閱者2收到了新聞:最新頭條新聞:...
一個常見的使用示例是 Redux。這款流行的狀態(tài)管理庫基于這種模式(或更具體地說,是 Flux 架構(gòu))。在 Redux 的上下文中,工作機(jī)制相當(dāng)簡單:
發(fā)布者:store 充當(dāng)發(fā)布者。當(dāng)一個 action 被派發(fā)時,store 會通知所有訂閱的組件狀態(tài)的變化。 訂閱者:應(yīng)用程序中的 UI 組件是訂閱者。它們訂閱 Redux store 并在狀態(tài)變化時接收更新。
自定義事件作為瀏覽器版本的 PubSub
瀏覽器通過 CustomEvent 類和 dispatchEvent 方法提供了一個用于觸發(fā)和訂閱自定義事件的 API。后者不僅能讓我們觸發(fā)事件,還能附加任何想要的數(shù)據(jù)。
const customEvent = new CustomEvent('customEvent', {
detail: '自定義事件數(shù)據(jù)', // 將所需數(shù)據(jù)附加到事件
});
const element = document.getElementById('.element-to-trigger-events');
element.addEventListener('customEvent', (event) => {
console.log(`訂閱者1收到了自定義事件:${event.detail}`);
});
element.addEventListener('customEvent', (event) => {
console.log(`訂閱者2收到了自定義事件:${event.detail}`);
});
// 觸發(fā)自定義事件
element.dispatchEvent(customEvent);
// 控制臺日志輸出:
// 訂閱者1收到了自定義事件:自定義事件數(shù)據(jù)
// 訂閱者2收到了自定義事件:自定義事件數(shù)據(jù)
自定義事件目標(biāo)
如果你不想在全局 window 對象上分派事件,可以創(chuàng)建你自己的事件目標(biāo)。
通過擴(kuò)展原生 EventTarget 類,你可以向其新實(shí)例分派事件。這確保你的事件僅在新類本身上觸發(fā),避免了全局傳播。此外,你可以直接將處理程序附加到這個特定實(shí)例上。
class CustomEventTarget extends EventTarget {
constructor() {
super();
}
// 觸發(fā)自定義事件的自定義方法
triggerCustomEvent(eventName, eventData) {
const event = new CustomEvent(eventName, { detail: eventData });
this.dispatchEvent(event);
}
}
const customTarget = new CustomEventTarget();
// 向自定義事件目標(biāo)添加事件監(jiān)聽器
customTarget.addEventListener('customEvent', (event) => {
console.log(`自定義事件收到了數(shù)據(jù):${event.detail}`);
});
// 觸發(fā)自定義事件
customTarget.triggerCustomEvent('customEvent', '你好,自定義事件!');
// 控制臺日志輸出:
// 自定義事件收到了數(shù)據(jù):你好,自定義事件!
觀察者模式
觀察者模式與 PubSub 非常相似。你訂閱 Subject,然后它通知其訂閱者(觀察者)關(guān)于變化,使他們能夠做出相應(yīng)的反應(yīng)。這種模式在構(gòu)建解耦和靈活的架構(gòu)中發(fā)揮了重要作用。
class Subject {
constructor() {
this.observers = [];
}
addObserver(observer) {
this.observers.push(observer);
}
// 從列表中移除觀察者
removeObserver(observer) {
const index = this.observers.indexOf(observer);
if (index !== -1) {
this.observers.splice(index, 1);
}
}
// 通知所有觀察者關(guān)于變化
notify() {
this.observers.forEach((observer) => {
observer.update();
});
}
}
class Observer {
constructor(name) {
this.name = name;
}
// 通知時調(diào)用的更新方法
update() {
console.log(`${this.name} 收到了更新。`);
}
}
const subject = new Subject();
const observer1 = new Observer('觀察者1');
const observer2 = new Observer('觀察者2');
// 將觀察者添加到主體
subject.addObserver(observer1);
subject.addObserver(observer2);
// 通知觀察者關(guān)于變化
subject.notify();
// 控制臺日志輸出:
// 觀察者1 收到了更新。
// 觀察者2 收到了更新。
使用 Proxy 的響應(yīng)式屬性
如果你想對對象的變化做出反應(yīng),Proxy 是一個好方法。它讓我們在設(shè)置或獲取對象字段的值時實(shí)現(xiàn)響應(yīng)性。
const person = {
name: 'Pavel',
age: 22,
};
const reactivePerson = new Proxy(person, {
// 攔截設(shè)置操作
set(target, key, value) {
console.log(`將 ${key} 設(shè)置為 ${value}`);
target[key] = value;
// 表示設(shè)置值是否成功
return true;
},
// 攔截獲取操作
get(target, key) {
console.log(`獲取 ${key}`);
return target[key];
},
});
reactivePerson.name = 'Sergei'; // 將 name 設(shè)置為 Sergei
console.log(reactivePerson.name); // 獲取 name: Sergei
reactivePerson.age = 23; // 將 age 設(shè)置為 23
console.log(reactivePerson.age); // 獲取 age: 23
單個對象屬性和響應(yīng)性
如果你不需要跟蹤對象中的所有字段,可以使用 Object.defineProperty 或一組 Object.defineProperties 來選擇特定的一個或幾個。
const person = {
_originalName: 'Pavel', // 私有屬性
}
Object.defineProperty(person, 'name', {
get() {
console.log('獲取屬性 name')
return this._originalName
},
set(value) {
console.log(`將屬性 name 設(shè)置為值 ${value}`)
this._originalName = value
},
})
console.log(person.name) // '獲取屬性 name' 和 'Pavel'
person.name = 'Sergei' // 將屬性 name 設(shè)置為值 Sergei
使用 MutationObserver 的響應(yīng)式 HTML 屬性
在 DOM 中實(shí)現(xiàn)響應(yīng)性的一種方法是使用 MutationObserver。其 API 允許我們觀察目標(biāo)元素及其子元素的屬性變化和文本內(nèi)容變化。
function handleMutations(mutationsList, observer) {
mutationsList.forEach((mutation) => {
// 觀察到的元素的一個屬性發(fā)生了變化
if (mutation.type === 'attributes') {
console.log(`屬性 '${mutation.attributeName}' 更改為 '${mutation.target.getAttribute(mutation.attributeName)}'`);
}
});
}
const observer = new MutationObserver(handleMutations);
const targetElement = document.querySelector('.element-to-observe');
// 開始觀察目標(biāo)元素
observer.observe(targetElement, { attributes: true });
使用 IntersectionObserver 的響應(yīng)式滾動
IntersectionObserver API 允許對目標(biāo)元素與另一個元素或視口區(qū)域的交集做出反應(yīng)。
function handleIntersection(entries, observer) {
entries.forEach((entry) => {
// 目標(biāo)元素在視口中
if (entry.isIntersecting) {
entry.target.classList.add('visible');
} else {
entry.target.classList.remove('visible');
}
});
}
const observer = new IntersectionObserver(handleIntersection);
const targetElement = document.querySelector('.element-to-observe');
// 開始觀察目標(biāo)元素
observer.observe(targetElement);