Skip to content

Commit

Permalink
allow multiple data arguments when publishing a topic
Browse files Browse the repository at this point in the history
  • Loading branch information
georapbox committed Sep 5, 2017
1 parent 0849de9 commit 69aff3b
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 33 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# CHANGELOG

## v3.2.7
Allow passing multiple data arguments to `publish` and `publishSync` methods.
```js
var pubsub = new PubSub();

pubsub.subscribe('event', function (data) {
console.log(data);
// => Array [{fname: 'John'}, {lname: 'Doe'}, [1, 2, 3], 'Lorem ipsum dolor sit amet.']

console.log(data[0]);
// => Object {lname: 'John'}

console.log(data[1]);
// => Object {lname: 'Doe'}

console.log(data[2]);
// => Array [1, 2, 3]

console.log(data[3]);
// => String "Lorem ipsum dolor sit amet."
});

pubsub.publish('event', {fname: 'John'}, {lname: 'Doe'}, [1, 2, 3], 'Lorem ipsum dolor sit amet.');
```

## v3.2.6
- Ensure that listeners registered on the same topic are invoked in the order they are added.
- Minor updates on documentation.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ For synchronous topic publication check `publishSync`.
| Param | Type | Description |
| --- | --- | --- |
| topic | <code>string</code> | The topic's name |
| [data] | <code>\*</code> | The data to be passed to its subscribers |
| [data] | <code>...\*</code> | The data to be passed to its subscribers |

**Example**
```js
Expand All @@ -139,7 +139,7 @@ Publishes a topic **synchronously**, passing the data to its subscribers.
| Param | Type | Description |
| --- | --- | --- |
| topic | <code>string</code> | The topic's name |
| [data] | <code>\*</code> | The data to be passed to its subscribers |
| [data] | <code>...\*</code> | The data to be passed to its subscribers |

**Example**
```js
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "georapbox.pubsub.js",
"version": "3.2.5",
"version": "3.2.7",
"homepage": "https://github.com/georapbox/PubSub",
"authors": [
"George Raptis <[email protected]>"
Expand Down
4 changes: 2 additions & 2 deletions dist/pubsub.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/pubsub.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PubSub",
"version": "3.2.6",
"version": "3.2.7",
"description": "Javascript implementation of the Publish/Subscribe pattern.",
"main": "src/pubsub.js",
"scripts": {
Expand Down Expand Up @@ -32,12 +32,12 @@
},
"homepage": "https://github.com/georapbox/PubSub#readme",
"devDependencies": {
"eslint": "~4.2.0",
"eslint": "~4.6.1",
"grunt": "~1.0.1",
"grunt-cli": "~1.2.0",
"grunt-contrib-uglify": "~3.0.1",
"grunt-remove-logging": "~0.2.0",
"jasmine-core": "~2.6.4",
"jasmine-core": "~2.8.0",
"jsdoc-to-markdown": "~3.0.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.2.0",
Expand Down
12 changes: 7 additions & 5 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* PubSub.js
* Javascript implementation of the Publish/Subscribe pattern.
*
* @version 3.2.6
* @version 3.2.7
* @author George Raptis <[email protected]> (georapbox.github.io)
* @homepage https://github.com/georapbox/PubSub#readme
* @repository https://github.com/georapbox/PubSub.git
Expand Down Expand Up @@ -186,7 +186,7 @@
* @memberof PubSub
* @this {PubSub}
* @param {string} topic The topic's name
* @param {*} [data] The data to be passed to its subscribers
* @param {...*} [data] The data to be passed to its subscribers
* @return {boolean} Returns `true` if topic exists and event is published; otheriwse `false`
* @example
*
Expand All @@ -197,7 +197,8 @@
* });
*/
PubSub.prototype.publish = function (topic, data) {
return publish(this, topic, data, false);
var dataArgs = Array.prototype.slice.call(arguments, 1);
return publish(this, topic, dataArgs.length <= 1 ? data : dataArgs, false);
};

/**
Expand All @@ -206,7 +207,7 @@
* @memberof PubSub
* @this {PubSub}
* @param {string} topic The topic's name
* @param {*} [data] The data to be passed to its subscribers
* @param {...*} [data] The data to be passed to its subscribers
* @return {boolean} Returns `true` if topic exists and event is published; otheriwse `false`
* @example
*
Expand All @@ -217,7 +218,8 @@
* });
*/
PubSub.prototype.publishSync = function (topic, data) {
return publish(this, topic, data, true);
var dataArgs = Array.prototype.slice.call(arguments, 1);
return publish(this, topic, dataArgs.length <= 1 ? data : dataArgs, true);
};

/**
Expand Down
Loading

0 comments on commit 69aff3b

Please sign in to comment.