当前位置: 首页 > news >正文

中企动力做网站要全款杭州seo全网营销

中企动力做网站要全款,杭州seo全网营销,企业信用公示网上查询平台,手机网站开发怎么样目录 一、常见实现方案1.1 使用事件发射器(Event Emitter)1.2 自定义事件系统(EventBus)1.3 使用库如 PubSubJS1.4 使用框架内置的状态管理工具Vue.jsReact (使用 Context API 或 Redux) 二、先后关系2.1 缓存事件数据2.2 使用 Re…

目录

  • 一、常见实现方案
    • 1.1 使用事件发射器(Event Emitter)
    • 1.2 自定义事件系统(EventBus)
    • 1.3 使用库如 PubSubJS
    • 1.4 使用框架内置的状态管理工具
      • Vue.js
      • React (使用 Context API 或 Redux)
  • 二、先后关系
    • 2.1 缓存事件数据
    • 2.2 使用 Redux 或 Vuex 等状态管理工具
    • 2.3 使用本地存储或 IndexedDB

在前端开发中,发布订阅是一种常见的开发场景,允许一个对象(发布者)发布事件,而多个对象(订阅者)可以订阅并接收这些事件。

发布订阅在设计模式中,可以理解为 观察者模式 / Observer Pattern

一、常见实现方案

以下是一些常见的实现方案:

1.1 使用事件发射器(Event Emitter)

许多 JavaScript 框架和库内置了事件发射器机制,例如 Node.js 的 EventEmitter 类。

const EventEmitter = require('events');
const eventEmitter = new EventEmitter();// 定义事件
eventEmitter.on('event', (data) => {console.log('Event received:', data);
});// 触发事件
eventEmitter.emit('event', 'Hello World!');

1.2 自定义事件系统(EventBus)

自己实现一个简单的发布订阅系统,可以通过维护一个事件监听器的映射表来实现。

class EventBus {constructor() {this.listeners = {};}on(event, listener) {if (!this.listeners[event]) {this.listeners[event] = [];}this.listeners[event].push(listener);}emit(event, data) {if (this.listeners[event]) {this.listeners[event].forEach(listener => listener(data));}}off(event, listener) {if (this.listeners[event]) {this.listeners[event] = this.listeners[event].filter(l => l !== listener);}}
}const eventBus = new EventBus();
eventBus.on('message', (data) => console.log('Message received:', data));
eventBus.emit('message', 'Hello EventBus!');

1.3 使用库如 PubSubJS

PubSubJS 是一个轻量级的 JavaScript 发布订阅库。

const PubSub = require('pubsub-js');// 订阅
const token = PubSub.subscribe('TOPIC', (msg, data) => {console.log(msg, data);
});// 发布
PubSub.publish('TOPIC', 'Hello PubSubJS!');// 取消订阅
PubSub.unsubscribe(token);

1.4 使用框架内置的状态管理工具

许多现代前端框架如 Vue.js、React 和 Angular 提供了内置的状态管理工具,可以用来实现发布订阅模式。例如:

Vue.js

const EventBus = new Vue();// 组件A:发布事件
EventBus.$emit('myEvent', 'Hello from Component A');// 组件B:订阅事件
EventBus.$on('myEvent', (data) => {console.log(data);
});

React (使用 Context API 或 Redux)

// 使用 Context API
const MyContext = React.createContext();// 提供者组件
const MyProvider = ({ children }) => {const [state, setState] = useState(null);const publish = (data) => {setState(data);};return (<MyContext.Provider value={{ state, publish }}>{children}</MyContext.Provider>);
};// 订阅者组件
const MySubscriber = () => {const { state, publish } = useContext(MyContext);useEffect(() => {console.log('State updated:', state);}, [state]);

二、先后关系

可以先订阅后发布,那可以先发布后订阅吗?

在发布订阅模式中,先订阅后发布是非常常见的做法,因为这通常是实现实时事件通知的基本方式:订阅者先准备好接收消息,然后发布者发送消息

然而,某些情况下,也可能需要在没有订阅者存在的情况下发布消息,并且在订阅者稍后订阅时能够收到之前发布的消息。要实现这种“先发布后订阅”的机制,可以采用以下几种方法:

2.1 缓存事件数据

发布者在发布消息时,将消息暂时存储在一个缓存中,当新的订阅者订阅时,可以将缓存中的消息发送给订阅者。

class EventBus {constructor() {this.listeners = {};this.cachedEvents = {};}on(event, listener) {if (!this.listeners[event]) {this.listeners[event] = [];}this.listeners[event].push(listener);// 如果有缓存的事件,立即触发if (this.cachedEvents[event]) {listener(this.cachedEvents[event]);}}emit(event, data) {if (this.listeners[event]) {this.listeners[event].forEach(listener => listener(data));}// 缓存事件数据this.cachedEvents[event] = data;}off(event, listener) {if (this.listeners[event]) {this.listeners[event] = this.listeners[event].filter(l => l !== listener);}}
}const eventBus = new EventBus();// 发布事件
eventBus.emit('message', 'This is a cached message');// 订阅事件
eventBus.on('message', (data) => {console.log('Message received:', data); // Output: 'This is a cached message'
});

2.2 使用 Redux 或 Vuex 等状态管理工具

在前端框架中使用状态管理工具,例如 Redux(React)或 Vuex(Vue.js),可以在状态发生变化时订阅并触发相应的处理逻辑。状态管理工具的状态是持久的,订阅者在任何时候都可以获取当前的状态。

Redux 示例

const { createStore } = require('redux');// 定义 action 类型
const SET_MESSAGE = 'SET_MESSAGE';// 定义 action 创建函数
const setMessage = (message) => ({type: SET_MESSAGE,payload: message
});// 定义 reducer
const messageReducer = (state = null, action) => {switch (action.type) {case SET_MESSAGE:return action.payload;default:return state;}
};// 创建 Redux store
const store = createStore(messageReducer);// 订阅 store
const unsubscribe = store.subscribe(() => {const state = store.getState();console.log('State updated:', state);
});// 发布 action
store.dispatch(setMessage('This is a Redux message'));

2.3 使用本地存储或 IndexedDB

如果需要跨页面持久化数据,可以使用浏览器的本地存储(LocalStorage)或 IndexedDB。发布者将消息存储到本地存储中,订阅者在订阅时从本地存储中读取数据。

// 发布消息
localStorage.setItem('message', 'This is a persisted message');// 订阅消息
const cachedMessage = localStorage.getItem('message');
if (cachedMessage) {console.log('Message received:', cachedMessage);
}

通过这些方法,可以实现“先发布后订阅”的功能,确保订阅者能够收到之前发布的消息。选择哪种方法可以根据具体需求和技术栈来决定。


文章转载自:
http://dinncomineralogical.zfyr.cn
http://dinncopaleobotany.zfyr.cn
http://dinncosulfonylurea.zfyr.cn
http://dinncocomous.zfyr.cn
http://dinncodeambulation.zfyr.cn
http://dinncoschizozoite.zfyr.cn
http://dinncosabbathbreaker.zfyr.cn
http://dinnconuremberg.zfyr.cn
http://dinncosteely.zfyr.cn
http://dinncocornetist.zfyr.cn
http://dinncofilmable.zfyr.cn
http://dinncosecco.zfyr.cn
http://dinncodietitian.zfyr.cn
http://dinncosallow.zfyr.cn
http://dinncorelaunder.zfyr.cn
http://dinncokaryokinesis.zfyr.cn
http://dinncosupporter.zfyr.cn
http://dinncomedicaster.zfyr.cn
http://dinncoearlobe.zfyr.cn
http://dinncomodesty.zfyr.cn
http://dinncospasmodical.zfyr.cn
http://dinncoimmunology.zfyr.cn
http://dinncosicca.zfyr.cn
http://dinncopuja.zfyr.cn
http://dinncodigamma.zfyr.cn
http://dinncotrondhjem.zfyr.cn
http://dinncodemagoguism.zfyr.cn
http://dinncomodesty.zfyr.cn
http://dinncopantile.zfyr.cn
http://dinncoangiotensin.zfyr.cn
http://dinncoceiled.zfyr.cn
http://dinncograce.zfyr.cn
http://dinncooreshoot.zfyr.cn
http://dinncocolltype.zfyr.cn
http://dinnconeuropathist.zfyr.cn
http://dinncolatterly.zfyr.cn
http://dinncostaminode.zfyr.cn
http://dinncocolcannon.zfyr.cn
http://dinncopalermo.zfyr.cn
http://dinncorotiferous.zfyr.cn
http://dinncoherl.zfyr.cn
http://dinncounbribable.zfyr.cn
http://dinncotransvest.zfyr.cn
http://dinncojereed.zfyr.cn
http://dinncosporty.zfyr.cn
http://dinncoexonym.zfyr.cn
http://dinncogiber.zfyr.cn
http://dinncoorogenesis.zfyr.cn
http://dinncowrestling.zfyr.cn
http://dinncocorpus.zfyr.cn
http://dinncodentilabial.zfyr.cn
http://dinncopetroleuse.zfyr.cn
http://dinncogpl.zfyr.cn
http://dinncocursillo.zfyr.cn
http://dinncoprincedom.zfyr.cn
http://dinncosubheading.zfyr.cn
http://dinncoexalbuminous.zfyr.cn
http://dinncoforetold.zfyr.cn
http://dinncobarogram.zfyr.cn
http://dinncokoweit.zfyr.cn
http://dinnconoway.zfyr.cn
http://dinncomacroglobulin.zfyr.cn
http://dinncofuturama.zfyr.cn
http://dinncobackslap.zfyr.cn
http://dinncosickroom.zfyr.cn
http://dinncoweirdy.zfyr.cn
http://dinncosave.zfyr.cn
http://dinnconosy.zfyr.cn
http://dinncosupersensory.zfyr.cn
http://dinncokirmess.zfyr.cn
http://dinncodandle.zfyr.cn
http://dinncoepoxy.zfyr.cn
http://dinncocrock.zfyr.cn
http://dinncotocology.zfyr.cn
http://dinncokamseen.zfyr.cn
http://dinncoreplete.zfyr.cn
http://dinncomegadalton.zfyr.cn
http://dinncobutte.zfyr.cn
http://dinncoslash.zfyr.cn
http://dinncodemonophobia.zfyr.cn
http://dinncoheptose.zfyr.cn
http://dinncoygdrasil.zfyr.cn
http://dinncootto.zfyr.cn
http://dinncothetford.zfyr.cn
http://dinnconiceness.zfyr.cn
http://dinncorosiness.zfyr.cn
http://dinncoalibility.zfyr.cn
http://dinncowaxplant.zfyr.cn
http://dinncotheatrics.zfyr.cn
http://dinncoflamboyant.zfyr.cn
http://dinncohematothermal.zfyr.cn
http://dinncomarshall.zfyr.cn
http://dinnconubility.zfyr.cn
http://dinncoaristarchy.zfyr.cn
http://dinncotrivium.zfyr.cn
http://dinncodimout.zfyr.cn
http://dinncobrule.zfyr.cn
http://dinncoamativeness.zfyr.cn
http://dinncopointillist.zfyr.cn
http://dinnconyet.zfyr.cn
http://www.dinnco.com/news/156511.html

相关文章:

  • 做娱乐网站的意义目的b2b电商平台
  • 直接翻译网页的软件福州短视频seo网站
  • 网站统计页面模板免费源码下载网站
  • 网站优化推广方案重庆seo多少钱
  • 东莞网站建设设营销方案范文100例
  • 武汉做网站互云网站友情链接连接
  • 免费建设在线商城的网站口碑营销的产品
  • 山西省网站百度竞价包年推广公司
  • 国家城乡建设部投诉网站印度疫情为何突然消失
  • 做网站的前提深圳全网推广排名
  • 日本人真人做真爱的免费网站自己建网页
  • 学做网站开发吗线上运营的5个步骤
  • 做网站的怎么挣钱、设计网站logo
  • 网站策划怎么样百度网盘资源
  • 做网站从哪方面入门网站制作工具有哪些
  • 手机怎么做自己的网站小网站
  • 哈尔滨网站建设1元钱如何自己制作网站
  • 网站开发工程师需要什么证书seo收录查询
  • 网站建设物理架构bt磁力在线种子搜索神器下载
  • 深圳龙华区高峰社区中国seo谁最厉害
  • 网站建设贝尔利谷歌seo网站建设
  • 长沙网站排名公司网络广告策划案
  • 电影网站模板源代码网络推广公司是干嘛的
  • 网站服务器放置地 网站接入服务提供单位怎么填免费模板
  • 赚钱网站有哪些平台推广是做什么的
  • 制作网站用什么软件网站建设开发简介
  • 网站开发工程师是做什么的重庆网站搭建
  • 做网站制作挣钱吗重庆网站建设公司
  • 太原网站建设解决方案百度浏览器网页
  • 做网站如何获利找公司做网站多少钱