mroderick / pubsubjs
JavaScript 的无依赖发布/订阅库
This package is not auto-updated.
Last update: 2024-09-24 08:04:03 UTC
README
PubSubJS 是一个基于主题的 发布/订阅 库,使用 JavaScript 编写。
PubSubJS 具有同步解耦功能,因此主题发布是异步的。这有助于使程序可预测,因为主题的发起者不会在消费者处理它们时被阻塞。
对于喜欢冒险的人来说,PubSubJS 还支持同步主题发布。这可以在某些环境中(浏览器,并非所有)提供速度提升,但也可能导致一些非常难以理解的程序,其中一个主题在相同的执行链中触发另一个主题的发布。
有关基准测试,请参阅 JS 发布/订阅方法的比较
单进程
PubSubJS 设计用于在 单进程 中使用,并且不是多进程应用程序(如具有许多子进程的 Node.js – Cluster)的好选择。如果您的 Node.js 应用程序是单进程应用程序,那么您就很好。如果它是(或将成为)多进程应用程序,那么您可能最好使用 redis Pub/Sub 或类似技术。
主要功能
- 无依赖
- 同步解耦
- ES3 兼容。PubSubJS 应该能够在可以执行 JavaScript 的任何地方运行。浏览器、服务器、电子书阅读器、旧手机、游戏机。
- AMD / CommonJS 模块支持
- 不修改订阅者(jQuery 自定义事件会修改订阅者)
- 易于理解和使用(归功于同步解耦)
- 小巧(小于 1kb 的最小化并压缩版本)
获取 PubSubJS
获取 PubSubJS 有几种方法
- 从 GitHub 下载标记版本
- 通过 npm 安装(
npm install pubsub-js) - 通过 bower 安装(
bower install pubsub-js)
示例
基本示例
// create a function to subscribe to topics var mySubscriber = function( msg, data ){ console.log( msg, data ); }; // add the function to the list of subscribers for 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 ); // publish a topic asyncronously PubSub.publish( 'MY TOPIC', 'hello world!' ); // publish a topic syncronously, which is faster in some environments, // but will get confusing when one topic triggers new topics in the // same execution chain // USE WITH CAUTION, HERE BE DRAGONS!!! PubSub.publishSync( 'MY TOPIC', 'hello world!' );
取消特定订阅
// 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 );
取消函数的所有订阅
// 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 mySubscriber from ALL topics PubSub.unsubscribe( mySubscriber );
分层寻址
// create a subscriber to receive all topics from a hierarchy of topics var myToplevelSubscriber = function( msg, data ){ console.log( 'top level: ', msg, data ); } // subscribe to all topics in the 'car' hierarchy PubSub.subscribe( 'car', myToplevelSubscriber ); // create a subscriber to receive only leaf topic from hierarchy op topics var mySpecificSubscriber = function( msg, data ){ console.log('specific: ', msg, data ); } // subscribe only to 'car.drive' topics PubSub.subscribe( 'car.drive', mySpecificSubscriber ); // Publish some topics PubSub.publish( 'car.purchase', { name : 'my new car' } ); PubSub.publish( 'car.drive', { speed : '14' } ); PubSub.publish( 'car.sell', { newOwner : 'someone else' } ); // In this scenario, myToplevelSubscriber will be called for all // topics, three times in total // But, mySpecificSubscriber will only be called once, as it only // subscribes to the 'car.drive' topic
技巧
使用 "常量" 而不是字符串字面量作为主题。PubSubJS 使用字符串作为主题,并且会愉快地尝试使用任何主题交付您的主题。所以,为了避免令人沮丧的调试,让 JavaScript 引擎在您犯拼写错误时抱怨。
"常量" 的使用示例
// BAD PubSub.subscribe("hello", function( msg, data ){ console.log( data ) }); PubSub.publish("helo", "world"); // BETTER var MY_TOPIC = "hello"; PubSub.subscribe(MY_TOPIC, function( msg, data ){ console.log( data ) }); PubSub.publish(MY_TOPIC, "world");
开发工具中的堆栈跟踪立即异常
从版本 1.3.2 开始,您可以强制立即异常(而不是延迟异常),这有利于在开发工具中查看时保持堆栈跟踪。
这应被视为仅适用于开发的选择,因为 PubSubJS 被设计为尝试将您的主题交付给所有订阅者,即使某些失败。
在开发中设置立即异常很简单,只需在加载 PubSubJS 之后告诉它即可。
PubSub.immediateExceptions = true;
jQuery 插件
默认情况下,PubSubJS 可用于任何浏览器或 CommonJS 环境,包括 node。此外,PubSubJS 可以专门为 jQuery 构建特定版本。
$ rake jquery
生成 jquery.pubsub.js
与 jQuery 一起使用
var topic = 'greeting', data = 'world' subscriber = function sayHello( data ){ console.log( 'hello ' + data ); }; // add a subscription var token = $.pubsub('subscribe', topic, subscriber ); // unsubscribing $.pubsub('unsubscribe', token) // remove a specific subscription $.pubsub('unsubscribe', subscriber); // remove all subscriptions for subscriber // publishing a topic $.pubsub('publish', topic, data); // publishing topic syncronously $.pubsub('publishSync', topic, data);
在jQuery构建中,全局的PubSub仍然可用,因此您可以根据需要混合使用Pubsub和$$.pubsub。
还有一篇关于使用PubSubJS与jQuery的文章。
为PubSubJS做出贡献
PubSubJS的未来
- 更好的和更广泛的用法示例
关于发布/订阅的更多信息
版本控制
PubSubJS使用语义版本控制进行可预测的版本控制。
变更日志
请参阅https://github.com/mroderick/PubSubJS/releases
许可证
MIT:http://mrgnrdrck.mit-license.org
替代方案
以下是一些也实现了基于主题的发布/订阅的JavaScript项目的替代方案。
- http://www.joezimjs.com/projects/publish-subscribe-jquery-plugin/
- http://amplifyjs.com/api/pubsub/
- http://radio.uxder.com/ —— 面向'频道',无依赖项
- https://github.com/pmelander/Subtopic - 支持 vanilla、underscore、jQuery,并且还在NuGet中可用