当前位置:网站首页>Implementation of publish and subscribe mode ----- hand tearing JS series
Implementation of publish and subscribe mode ----- hand tearing JS series
2022-06-11 03:34:00 【Love to eat virgin fruit】
1. What is publish and subscribe mode ?
It defines a one to many dependency between objects , When the state of an object changes , All objects that depend on it will be notified
Example :
Xiao Ming likes a series of articles by a blogger very much , But I don't know when to update , So Xiaoming subscribed to this series of blog posts of this blogger . When the blogger updates , Make sure Xiao Ming can receive the notice in time . This is a simple example of a publishing subscriber . Bloggers are publishers , Xiao Ming is a subscriber . Subscribe to this series of blog posts
2. application
1. Support simple communication , In the last example , When the object state is updated , Automatically notify subscribers of this object .
2. Publishers and subscribers are loosely coupled . Publishers simply notify subscribers , No matter how the subscriber runs . Subscribers only care about whether the state of the object changes , Decide whether to call the event method . stay asynchronous Widely used in requests . We don't need to pay attention to the internal state of the object in the asynchronous execution phase , We only care about the time when the event is completed
3. Code implementation
Basic steps of implementation :
Define an event container , Load event array ( Subscribers )
Define the add subscriber method (addEventListener())
Define trigger events (dispatchEvent)----- The publisher notifies the subscriber , Execute the corresponding event method
Define event removal methods (removeEventListener())---- Delete the subscription publication event of this object
class EventCenter{
//1. Define event containers , Used to load the event array
handlsers={
};
// Event name , Event parameters , Event method
addEventListener(type ,handlser)
{
// Create a new array
if(!this.handlsers[type])
{
this.handlsers[type]=[]
}
this.handlsers[type].push(handlser)
}
//2. Triggering event
dispatchEvent(type)
{
// No registration throws an error
if(!this.handlsers[type])
{
return new Error(" The event is not registered ")
}
// Triggering event
this.handlsers[type].forEach(handlser=>{
handlser(...params)
})
}
//3. Event removal
removeEventListener(type,handlser)
{
// This event does not exist
if(!this.handlsers[type])
{
return new Error(" The event is invalid ")
}
// Event free method , Delete the subscription and publication of this event
if(!handlser)
{
delete this.handlsers[type]
}else{
// Find the subscript of the publish subscribe array corresponding to the event method
const index=this.handlsers[type].findIndex(el=>el===handlser)
// Not found
if(index===-1)
return new Error(" This event is not bound ")
// Locate subscript , Delete the subscription and publishing events for this event
this.handlsers[type].splice(index,1)
if(this.handlsers[type].length===0)
{
delete this.handlsers[type]
}
}
}
}
Challenges continue to grow 100 God — The first day
边栏推荐
- PostgreSQL source code learning (21) -- fault recovery ② - transaction log initialization
- Jeecgboot learning_ Online form first experience
- Iqoo 8 measured hands-on experience: return of the king, never high profile
- JLINK latest version download
- thinkphp3.2.3反序列化利用链分析
- JS top icon menu click to switch background color JS special effect
- js最常用的排序---手撕js系列
- 名不副实的雅迪高端品牌VFLY,为何“不高端”?
- Demand and Prospect of 3D GIS Industry
- Canvas+svg line particle animation web page background
猜你喜欢

Azure kubernates service update | improve development experience and efficiency

Instructor add function_ Enable auto fill_ Instructor modification function

多线程四部曲之NSThread

突破中国品牌创新技术实力,TCL做对了什么?

Promise使用

JS click the sun and moon to switch between day and night JS special effect

canvas绘图——如何把图形放置画布中心

canvas+svg线条粒子动画网页背景

/The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter

OPPO K9试水“捆绑销售”,消费者“赚了”还是“亏了”?
随机推荐
js顶部图标菜单点击切换背景色js特效
UML系列文章(28)体系结构建模---协作
How should Xiaobai start the Amazon self support evaluation?
[safety science popularization] have you been accepted by social workers today?
名不副实的雅迪高端品牌VFLY,为何“不高端”?
The dependent version number in the dependencymanagement in the POM project parent in the idea is red
Promise使用
2022 年 5 月产品大事记
对象存储Minio使用教程
jlink最新版本下载
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
[safety science popularization] mining technology starts from the love story of a man of science and Engineering
UML series articles (28) architecture modeling - collaboration
Canvas drawing -- how to place the drawing in the center of the canvas
Oppo reno6 turned sour by "inner roll"
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Canvas rotation drawing H5 animation JS effect
postgresql 语句
023 MySQL索引优化口诀-索引失效的常见情况
正则表达式