- Architecting Angular Applications with Redux,RxJS,and NgRx
- Christoffer Noring
- 252字
- 2021-08-27 19:56:20
Adding EventEmitter
The two last bullets can really be condensed into one theme, namely eventing, or the ability to register to and fire off events.
So what does a cleanup of the store look like, and why would we need to clean it up? The reason for cleaning it up is it makes for simpler code. There is a standard library that is often used when constructing a store, called EventEmitter. The library handles just what we mentioned previously, namely it is able to register and fire off events. It is a simple implementation of the pub-sub pattern. Basically, EventEmitter allows you to subscribe to certain events and also allows you to trigger events. For more information on the pattern itself, have a look at the following link: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern.
You could definitely write your own code for this, but it's nice to be able to use a dedicated library so you can focus on other things that matter, such as solving business problems.
We decided to use the EventEmitter library and we do so in the following way:
// store-event-emitter.js
export const Store = (() => {
const eventEmitter = new EventEmitter();
return {
addListener: listener => {
eventEmitter.on("changed", listener);
},
emitChange: () => {
eventEmitter.emit("changed");
},
getSelectedItem: () => store["selectedItem"]
};
})();
This makes our code a little cleaner because we no longer need to hold an internal list of subscribers. There are more changes we can make though, so let us talk about that in the next section.
- 6G潛在關(guān)鍵技術(shù)(下冊)
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- Go Web Scraping Quick Start Guide
- 互聯(lián)網(wǎng)基礎(chǔ)資源技術(shù)與應(yīng)用發(fā)展態(tài)勢(2021—2023)
- 網(wǎng)絡(luò)安全技術(shù)與解決方案(修訂版)
- Unity Artificial Intelligence Programming
- 4G小基站系統(tǒng)原理、組網(wǎng)及應(yīng)用
- VMware NSX網(wǎng)絡(luò)虛擬化入門
- Android UI Design
- 現(xiàn)代通信系統(tǒng)(第5版)
- 信息技術(shù)安全評估準(zhǔn)則:源流、方法與實踐
- 物聯(lián)網(wǎng)與智慧廣電
- 一本書讀懂移動物聯(lián)網(wǎng)
- 視聽變革:廣電的新媒體戰(zhàn)略
- 加密與解密實戰(zhàn)全攻略