Skip to content

Commit

Permalink
Merge remote-tracking branch 'remote-code/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/pubsub.js
  • Loading branch information
Ryuurock committed Jan 16, 2018
2 parents fafd30d + 2dda79c commit 3a6a6f0
Show file tree
Hide file tree
Showing 28 changed files with 3,989 additions and 10,778 deletions.
14 changes: 11 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
; EditorConfig file: http://EditorConfig.org
; Install the "EditorConfig" plugin into your editor to use

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

# Matches the exact files either package.json or .travis.yml
[{package.json, .travis.yml}]
indent_style = space
indent_size = 2

; Needed if doing `git add --patch` to edit patches
[*.diff]
trim_trailing_whitespace = false
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
.tmp*
jquery.pubsub.js
/node_modules/
/out/
/out/coverage/
.nyc_output/
coverage
23 changes: 0 additions & 23 deletions .jslintrc

This file was deleted.

18 changes: 15 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ cache:
- node_modules

node_js:
- "0.12"
- "4.2"
- "4"
- "6"
- "8"

before_install:
- npm install coveralls

before_script:
- npm -q install
# these build targets only need to run once per build, so let's conserve a few resources
# ESLint only supports Node >=4
- if [ "x$TRAVIS_NODE_VERSION" = "x8" ]; then npm run lint; fi

script:
- npm test

after_success:
- if [ "x$TRAVIS_NODE_VERSION" = "x8" ]; then npm run coverage && cat ./coverage/lcov.info | coveralls lib; fi

git:
depth: 10
27 changes: 0 additions & 27 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,3 @@ And in case we didn't emphasize it enough: we love tests!
## Syntax

Install [Editor Config](http://editorconfig.org) for your text editor, this will ensure that the correct formatting is applied for each file type.

## Development

There are grunt tasks for helping with linting and testing the codebase.

### Test setup

The tests are implemented using [BusterJS](http://busterjs.org) and the excellent [Sinon.JS](http://sinonjs.org/). All dependencies can be installed with `npm install` (assuming you already have nodejs installed).

### Linting

```bash
$ grunt lint
```

### Testing with PhantomJS

If you have PhantomJS installed on your system, you can run the Buster tests by running

```bash
$ grunt test
```

or by running
```bash
$ npm test
```
36 changes: 0 additions & 36 deletions Gruntfile.js

This file was deleted.

108 changes: 34 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Travis build status](https://img.shields.io/travis/mroderick/PubSubJS.svg)](https://travis-ci.org/mroderick/PubSubJS) [![David](https://img.shields.io/david/mroderick/pubsubjs.svg)](https://david-dm.org/mroderick/PubSubJS) [![David](https://img.shields.io/david/dev/mroderick/pubsubjs.svg)](https://david-dm.org/mroderick/PubSubJS#info=devDependencies&view=table)
![npm version](https://img.shields.io/npm/v/pubsub-js.svg) ![npm license](https://img.shields.io/npm/l/pubsub-js.svg) ![npm downloads per month](https://img.shields.io/npm/dm/pubsub-js.svg)
[![Coverage Status](https://coveralls.io/repos/github/mroderick/PubSubJS/badge.svg)](https://coveralls.io/github/mroderick/PubSubJS)

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.

Expand All @@ -27,9 +28,11 @@ PubSubJS is designed to be used within a **single process**, and is not a good c

There are several ways of getting PubSubJS

* [Download a tagged version](https://github.com/mroderick/PubSubJS/tags) from GitHub
* Install via npm (`npm install pubsub-js`)
* Use it via CDN directly (http://www.jsdelivr.com/#!pubsubjs)
* Use it directly from a CDN directly
- http://www.jsdelivr.com/#!pubsubjs
- https://cdnjs.com/libraries/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**

Expand All @@ -39,52 +42,52 @@ There are several ways of getting PubSubJS

```javascript
// create a function to subscribe to topics
var mySubscriber = function( msg, data ){
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 );
var token = PubSub.subscribe('MY TOPIC', mySubscriber);

// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
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!' );
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 );
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 );
var token = PubSub.subscribe('MY TOPIC', mySubscriber);

// unsubscribe this subscriber from this topic
PubSub.unsubscribe( token );
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 );
var mySubscriber = function(msg, data) {
console.log(msg, data);
};

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

### Clear all subscriptions for a topic
Expand All @@ -110,25 +113,25 @@ PubSub.clearAllSubscriptions();

```javascript
// create a subscriber to receive all topics from a hierarchy of topics
var myToplevelSubscriber = function( msg, data ){
console.log( 'top level: ', msg, data );
var myToplevelSubscriber = function (msg, data) {
console.log('top level: ', msg, data);
}

// subscribe to all topics in the 'car' hierarchy
PubSub.subscribe( 'car', myToplevelSubscriber );
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 );
var mySpecificSubscriber = function (msg, data) {
console.log('specific: ', msg, data);
}

// subscribe only to 'car.drive' topics
PubSub.subscribe( 'car.drive', mySpecificSubscriber );
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' } );
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
Expand All @@ -145,80 +148,37 @@ when you make typos.

```javascript
// BAD
PubSub.subscribe("hello", function( msg, data ){
console.log( data )
PubSub.subscribe('hello', function (msg, data) {
console.log(data)
});

PubSub.publish("helo", "world");
PubSub.publish('helo', 'world');

// BETTER
var MY_TOPIC = "hello";
PubSub.subscribe(MY_TOPIC, function( msg, data ){
console.log( data )
var MY_TOPIC = 'hello';
PubSub.subscribe(MY_TOPIC, function (msg, data) {
console.log(data)
});

PubSub.publish(MY_TOPIC, "world");
PubSub.publish(MY_TOPIC, 'world');
```

### Immediate Exceptions for stack traces in developer tools

As of versions 1.3.2, you can force immediate exceptions (instead of delayed execeptions), which has the benefit of maintaining the stack trace when viewed in dev tools.
As of version 1.3.2, you can force immediate exceptions (instead of delayed execeptions), which has the benefit of maintaining the stack trace when viewed in dev tools.

This should be considered a development only option, as PubSubJS was designed to try to deliver your topics to all subscribers, even when some fail.

Setting immediate exceptions in development is easy, just tell PubSubJS about it after it's been loaded.
Setting immediate exceptions in development is easy, just tell PubSubJS about it after it has been loaded.

```javascript
PubSub.immediateExceptions = true;
```

## Plugin for jQuery

By default PubSubJS can be used in any browser or CommonJS environment, including [node](http://nodejs.org). Additionally, PubSubJS can be built specifically for jQuery using Rake.

$ rake jquery

or using Grunt

$ grunt jquery

Produces jquery.pubsub.js

### Use with jQuery

```javascript
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);
```

In the jQuery build, the global ```PubSub``` global is still available, so you can mix and match both ```Pubsub``` and ```$.pubsub``` as needed.

There is also an article about [Using PubSubJS with jQuery](http://roderick.dk/resources/using-pubsubjs-with-jquery/)

## Contributing to PubSubJS

Please see [CONTRIBUTING.md](CONTRIBUTING.md)

## Future of PubSubJS

* Better and more extensive usage examples


## More about Publish/Subscribe

Expand Down
Loading

0 comments on commit 3a6a6f0

Please sign in to comment.