<div align="center">
<p>
<img src="https://raw.githubusercontent.com/mroderick/PubSubJS/master/branding/logo.png"/ alt="PubSubJS" title="PubSubJS" width="200" height="200">
</p>
<p>
<a href="https://travis-ci.org/mroderick/PubSubJS"><img src="https://img.shields.io/travis/mroderick/PubSubJS.svg?style=flat-square" alt="Travis build status" title="Travis build status" /></a><!--
--><a href="https://david-dm.org/mroderick/PubSubJS"><img src="https://img.shields.io/david/mroderick/pubsubjs.svg?style=flat-square" alt="Dependencies" title="Powered by David" /></a><!--
--><a href="https://david-dm.org/mroderick/PubSubJS#info=devDependencies&view=table"><img src="https://img.shields.io/david/dev/mroderick/pubsubjs.svg?style=flat-square" alt="DevDependencies" title="Powered by David" /></a><!--
--><a href="https://www.npmjs.com/package/pubsub-js"><img src="https://img.shields.io/npm/v/pubsub-js.svg?style=flat-square" alt="NPM version" title="Click to go to NPM" /></a><!--
--><a href="https://github.com/mroderick/PubSubJS/blob/master/LICENSE.md"><img src="https://img.shields.io/npm/l/pubsub-js.svg?style=flat-square" alt="MIT License" title="License" /></a><!--
--><a href="https://www.npmjs.com/package/pubsub-js"><img src="https://img.shields.io/npm/dm/pubsub-js.svg?style=flat-square" alt="NPM downloads/month" title="Click to go to NPM" /></a><!--
--><a href="https://coveralls.io/github/mroderick/PubSubJS"><img src="https://img.shields.io/coveralls/github/mroderick/PubSubJS.svg?style=flat-square" alt="Coverage Status" title="View Coverage"/></a>
</p>
</div>
PubSubJS is a [topic-based](http://en.wikipedia.org/wiki/Publishâsubscribe_pattern#Message_filtering) [publish/subscribe](http://en.wikipedia.org/wiki/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](http://nodejs.org/api/cluster.html) 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](http://redis.io/topics/pubsub) 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
* Install via npm (`npm install pubsub-js`)
* Use it directly from a CDN directly
- http://www.jsdelivr.com/#!pubsubjs
- https://cdnjs.com/libraries/pubsub-js
- https://unpkg.com/pubsub-js
* [Download a tagged version](https://github.com/mroderick/PubSubJS/tags) from GitHub
**Note: the last version of this library available via bower is v1.5.4**
## Examples
First you have to import the module:
```javascript
import PubSub from 'pubsub-js'
// or when using CommonJS
const PubSub = require('pubsub-js');
```
### Basic example
```javascript
// 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
```javascript
// 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
```javascript
// 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
```javascript
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
```javascript
PubSub.clearAllSubscriptions();
// all subscriptions are removed
```
### Get Subscriptions
```javascript
PubSub.getSubscriptions('token');
// subscriptions by token from all topics
```
### Count Subscriptions
```javascript
PubSub.countSubscriptions('token');
// count by token from all topics
```
### Error Handling
```javascript
// 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
```javascript
// 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"
```javascript
// ð Bad usage
PubSub.subscribe('hello', function (msg, data) {
console.log(data)
});
PubSub.publish('hello', 'world');
// ð Better usage
var MY_TOPIC = 'hello';
PubSub.subscribe(MY_TOPIC, function (msg, data) {
console.log(data)
});
PubSub.publish(MY_TOPIC, 'world');
```
### Example of use of "symbol constants" with ES6/7 syntax
```javascript
// event-types.js
export const MY_TOPIC = Symbol('MY_TOPIC')
// somefile.js
import { MY_TOPIC } from './event-types.js'
PubSub.subscribe(MY_TOPIC, function (msg, data) {
console.log(data)
});
PubSub.p
谷粒商城P84 点击选择分类后不发送请求
需积分: 0 43 浏览量
2024-03-01
20:17:30
上传
评论
收藏 71KB ZIP 举报
harb970
- 粉丝: 11
- 资源: 2
最新资源
- tensorflow-2.9.3-cp39-cp39-win-amd64.whl
- tensorflow-2.9.3-cp37-cp37m-win-amd64.whl
- tensorflow-gpu-2.9.2-cp37-cp37m-win-amd64.whl
- matlab的低通,高通,带通滤波器代码设计
- 系统字体下载 字体文件-仿宋-黑体-楷体-楷体-GB2312-宋体-常规 文件.zip
- 演示设计-PPT模板-水墨风格-艺术与文化展示-水墨风格PPT模板:优雅设计,适合艺术与文化演示.zip
- view_video.php.html
- 涂个壁纸.apk
- hot-spicy pot.csv
- 二进制格式的BPSK中频数据,可以用来进行解调系统的开发
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈