• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

mroderick/PubSubJS: Dependency free publish/subscribe for JavaScript

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

mroderick/PubSubJS

开源软件地址:

https://github.com/mroderick/PubSubJS

开源编程语言:

JavaScript 94.5%

开源软件介绍:

PubSubJS

Travis build statusDependenciesDevDependenciesNPM versionMIT LicenseNPM downloads/monthCoverage Status

PubSubJS is a topic-based publish/subscribe library written in JavaScript.

PubSubJS has synchronisation decoupling, so topics are published asynchronously. This helps keep your program predictable as the originator of topics will not be blocked while consumers process them.

For the adventurous, PubSubJS also supports synchronous topic publication. This can give a speedup in some environments (browsers, not all), but can also lead to some very difficult to reason about programs, where one topic triggers publication of another topic in the same execution chain.

Single process

PubSubJS is designed to be used within a single process, and is not a good candidate for multi-process applications (like Node.js – Cluster with many sub-processes). If your Node.js app is a single process app, you're good. If it is (or is going to be) a multi-process app, you're probably better off using redis Pub/Sub or similar

Key features

  • Dependency free
  • synchronisation decoupling
  • ES3 compatible. PubSubJS should be able to run everywhere that can execute JavaScript. Browsers, servers, ebook readers, old phones, game consoles.
  • AMD / CommonJS module support
  • No modification of subscribers (jQuery custom events modify subscribers)
  • Easy to understand and use (thanks to synchronisation decoupling)
  • Small(ish), less than 1kb minified and gzipped

Getting PubSubJS

There are several ways of getting PubSubJS

Note: the last version of this library available via bower is v1.5.4

Examples

First you have to import the module:

import PubSub from 'pubsub-js'

// or when using CommonJS
const PubSub = require('pubsub-js');

Basic example

// 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 asynchronously
PubSub.publish('MY TOPIC', 'hello world!');

// publish a topic synchronously, 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!');

Cancel specific subscription

// 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);

Cancel all subscriptions for a function

// create a function to receive the topic
var mySubscriber = function(msg, data) {
    console.log(msg, data);
};

// unsubscribe mySubscriber from ALL topics
PubSub.unsubscribe(mySubscriber);

Clear all subscriptions for a topic

PubSub.subscribe('a', myFunc1);
PubSub.subscribe('a.b', myFunc2);
PubSub.subscribe('a.b.c', myFunc3);

PubSub.unsubscribe('a.b');
// no further notifications for 'a.b' and 'a.b.c' topics
// notifications for 'a' will still get published

Clear all subscriptions

PubSub.clearAllSubscriptions();
// all subscriptions are removed

Get Subscriptions

PubSub.getSubscriptions('token');
// subscriptions by token from all topics

Count Subscriptions

PubSub.countSubscriptions('token');
// count by token from all topics

Error Handling

// isPublished is a boolean that represents if any subscribers was registered for this topic
var isPublished = PubSub.publish('a');

// token will be false if something went wrong and subscriber was not registered
var token = PubSub.subscribe('MY TOPIC', mySubscriber); 

Hierarchical addressing

// 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

Tips

Use "constants" for topics and not string literals. PubSubJS uses strings as topics, and will happily try to deliver your topics with ANY topic. So, save yourself from frustrating debugging by letting the JavaScript engine complain when you make typos.

Example of use of "constants"


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap