使用 pubsub-js 进行消息发布订阅

npm 包地址

github 包地址

pubsub-js 是一个轻量级的 JavaScript 基于主题的消息订阅发布库 ,压缩后小于1b。它具有使用简单、性能高效、支持多平台等优点,可以很好地满足各种需求。

功能特点:

  • 无依赖
  • 同步解耦
  • ES3 兼容。pubsub-js 能够在任何可以执行 JavaScript 的地方运行。
  • AMD / CommonJS 模块支持
  • 不修改订阅者(jQuery 自定义事件修改订阅者)
  • 易于理解和使用(得益于同步解耦)
  • 小(ish),压缩后小于 1kb

获取 pubsub-js

你可以通过以下几种方式获取 pubsub-js

  • 使用 NPM 包

首先,你需要在项目根目录下使用以下命令安装 pubsub-js

bash 复制代码
# 使用 pnpm 安装
pnpm add pubsub-js
# 使用 npm 安装
npm install --save pubsub-js
# 使用 yarn 安装
yarn add pubsub-js
  • 使用 CDN

你还可以通过 CDN 获取构建好的 pubsub-js 文件。将以下代码添加到 HTML 文件的 <script> 标签中:

html 复制代码
<script src="https://unpkg.com/pubsub-js"></script>
<!-- or -->
<script src="http://www.jsdelivr.com/#!pubsubjs"></script>
<!-- or -->
<script src="https://cdnjs.com/libraries/pubsub-js"></script>
  • 从 GitHub下载

GitHub下载地址

截止到目前,获取的最新版本是 v1.9.4,如图:

引入 pubsub-js

  • 通过 NPM 包引入

JavaScript 文件顶部使用 import 引入 pubsub-js

js 复制代码
// using ES6 modules
import PubSub from 'pubsub-js'

// using CommonJS modules
const PubSub = require('pubsub-js')
  • 使用 script 标签引入

通过直接在 HTML 文件中添加 <script> 标签,引入构建好的 pubsub-js 文件:

html 复制代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <!-- 引入 pubsub-js 文件 -->
    <script src="https://unpkg.com/pubsub-js"></script>
  </head>
</html>

简单使用

以更新未读消息数量为例。

  1. 首先,新建一个 pubsub.js 文件
js 复制代码
import PubSub from 'pubsub-js'

export default PubSub
  1. 使用 subscribe 订阅事件/ unsubscribe 取消订阅
js 复制代码
import { onMounted, onUnmounted, ref } from 'vue'
import PubSub from '@/utils/pubsub'

const count = ref(0)

const readmessage = () => {
  count.value = count.value - 1
}
onMounted(() => {
  PubSub.subscribe('messageread', readmessage)
  ...
})
onUnmounted(() => {
  PubSub.unsubscribe('messageread', readmessage)
})
  1. 使用 publish 发送消息
js 复制代码
import PubSub from '@/utils/pubsub'

...
PubSub.publish('messageread')
...

订阅 subscribe

  • 获取订阅
js 复制代码
// subscriptions by token from all topics
PubSub.getSubscriptions('token');
  • 订阅计数
js 复制代码
// count by token from all topics
PubSub.countSubscriptions('token');

取消/清除订阅 unsubscribe

订阅之后,一定要取消订阅。

  • 取消特定订阅
js 复制代码
// create a function to receive the topic
var mySubscriber = function (msg, data) {
    console.log(msg, data);
};

// add the function to the list of subscribers to a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe('MY TOPIC', mySubscriber);

// unsubscribe this subscriber from this topic
PubSub.unsubscribe(token);
  • 取消某个函数的所有订阅
js 复制代码
// create a function to receive the topic
var mySubscriber = function(msg, data) {
    console.log(msg, data);
};

// unsubscribe mySubscriber from ALL topics
PubSub.unsubscribe(mySubscriber);
  • 清除某个主题的所有订阅
js 复制代码
// no further notifications for 'a.b' and 'a.b.c' topics
// notifications for 'a' will still get published
PubSub.subscribe('a', myFunc1);
PubSub.subscribe('a.b', myFunc2);
PubSub.subscribe('a.b.c', myFunc3);

PubSub.unsubscribe('a.b');
  • 清除所有订阅
js 复制代码
// all subscriptions are removed
PubSub.clearAllSubscriptions();

pubsub-js 通过发布/订阅模式实现实现组件间的解耦合,可以减少代码的复杂度和维护成本,使代码设计更人性化。

相关推荐
侯六六29 分钟前
前端无感刷新Token实现(基于Axios)
前端
shadowcz00741 分钟前
Live Search API :给大模型装了一个“实时搜索引擎”的插件
linux·服务器·前端·数据库·搜索引擎
一个小白5551 小时前
nginx 的反向代理 负载均衡 动静分离 重写
linux·运维·前端·nginx·centos·firefox
panrunjun1 小时前
vue element-plus 集成多语言
前端·javascript·vue.js
蓝莓味的口香糖1 小时前
Vue常用自定义指令-积累的魅力【VUE】
前端·javascript·vue.js
沃野_juededa1 小时前
element ui 表格实现单选
javascript·vue.js·ui
未来之窗软件服务2 小时前
在 Excel 使用macro 常用函数 使用行数 招标专家系统————仙盟创梦IDE
前端·excel·vbs·excel插件·仙盟创梦ide
全马必破三2 小时前
前端性能优化方案
开发语言·前端·javascript
心海资源2 小时前
【心海资源】黄金首饰价格查询单页源码
前端·学习·开源软件
打小就很皮...3 小时前
《从虚拟 DOM 到 Diff 算法:深度解析前端高效更新的核心原理》-简版
前端·javascript·html