Skip to content

Commit

Permalink
Add public method subscribers() to get a readonly object of the curre…
Browse files Browse the repository at this point in the history
…nt subscribers.
  • Loading branch information
georapbox committed Jan 23, 2017
1 parent 76a09b7 commit b96bd1f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"commonjs": true,
"amd": true
},
"globals": {},
"globals": {
"PubSub": false
},
"rules": {
/* Possible Errors */

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v3.1.0
- `hasSubscribers` checks if there is at least one subscriber, no matter its name, if no argument is passed.
- Add public method `subscribers()` to get a readonly object of the current subscribers.

## v3.0.0

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ For API updates and breaking changes, check the [CHANGELOG](https://github.com/g
* [.unsubscribe(topic)](#PubSub+unsubscribe) ⇒ <code>boolean</code> &#124; <code>string</code>
* [.unsubscribeAll()](#PubSub+unsubscribeAll) ⇒ <code>[PubSub](#PubSub)</code>
* [.hasSubscribers([topic])](#PubSub+hasSubscribers) ⇒ <code>Boolean</code>
* [.subscribers()](#PubSub+subscribers) ⇒ <code>object</code>
* [.alias(aliasMap)](#PubSub+alias) ⇒ <code>[PubSub](#PubSub)</code>

<a name="new_PubSub_new"></a>
Expand Down Expand Up @@ -199,6 +200,25 @@ pubsub.on('message', function (data) {
pubsub.hasSubscribers('message');
// -> true
```
<a name="PubSub+subscribers"></a>

### pubSub.subscribers() ⇒ <code>object</code>
Gets all the subscribers as a key value pair of topic's name and event listener bound.

**Kind**: instance method of <code>[PubSub](#PubSub)</code>
**Returns**: <code>object</code> - A readonly object with all subscribers.
**this**: <code>{PubSub}</code>
**Example**
```js
var pubsub = new PubSub();

pubsub.subscribe('message', listener);
pubsub.subscribe('message', listener);
pubsub.subscribe('another_message', listener);

pubsub.subscribers();
// -> Object { message: Array[2], another_message: Array[1] }
```
<a name="PubSub+alias"></a>

### pubSub.alias(aliasMap) ⇒ <code>[PubSub](#PubSub)</code>
Expand Down
2 changes: 1 addition & 1 deletion 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.

41 changes: 41 additions & 0 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@
}('PubSub', this, function () {
'use strict';

function extend() {
var i, l, key;

for (i = 1, l = arguments.length; i < l; i++) {
for (key in arguments[i]) {
if (Object.prototype.hasOwnProperty.call(arguments[i], key)) {
if (arguments[i][key] && arguments[i][key].constructor &&
arguments[i][key].constructor === Object) {
arguments[0][key] = arguments[0][key] || {};
extend(arguments[0][key], arguments[i][key]);
} else {
arguments[0][key] = arguments[i][key];
}
}
}
}

return arguments[0];
}

function forOwn(obj, callback, thisArg) {
var key;

Expand Down Expand Up @@ -308,6 +328,27 @@
return false;
};

/**
* Gets all the subscribers as a key value pair of topic's name and event listener bound.
*
* @memberof PubSub
* @this {PubSub}
* @return {object} A readonly object with all subscribers.
* @example
*
* var pubsub = new PubSub();
*
* pubsub.subscribe('message', listener);
* pubsub.subscribe('message', listener);
* pubsub.subscribe('another_message', listener);
*
* pubsub.subscribers();
* // -> Object { message: Array[2], another_message: Array[1] }
*/
PubSub.prototype.subscribers = function () {
return extend({}, this._pubsub_topics);
};

/**
* Creates aliases for public methods.
*
Expand Down
29 changes: 15 additions & 14 deletions tests/pubsub.specs.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/* eslint-disable strict, no-unused-vars, no-use-before-define */

describe('new PubSub', function () {
it('Creates a new instance of PubSub.', function () {
expect(new PubSub()).not.toBeNull();
});
});


// Subscribe scenarios.
describe('Subscribe to an event.', function () {
it('Subscribes to an event called: "example-event".', function () {
var ps = new PubSub();

function listener() {}

var ps = new PubSub();
expect(ps.subscribe('example-event', listener)).toBe(0);
});
});


// Publish scenarios.
describe('Publish/Emmit an event.', function () {
function listener() {}
var ps = new PubSub();
function listener() {}
ps.subscribe('example-event', listener);

it('Publishes "example-event" passing data: "{ dummyKey : \'dummyValue\' }".', function () {
Expand All @@ -29,7 +30,7 @@ describe('Publish/Emmit an event.', function () {
dummyKey: 'dummyValue'
});

expect(ps.publish).toHaveBeenCalledWith('example-event', { dummyKey: 'dummyValue' });
expect(ps.publish).toHaveBeenCalledWith('example-event', {dummyKey: 'dummyValue'});
});

it('Publishes "example-event".', function () {
Expand All @@ -45,30 +46,31 @@ describe('Publish/Emmit an event.', function () {
});
});


// Unsubscribe scenarios.
describe('Unsubscribe from event.', function () {
it('Unsubscribes from event using the event name ("example-event").', function () {
var ps = new PubSub();
var unsub;

function listener() {}

var ps = new PubSub();
ps.subscribe('example-event', listener);
var unsub = ps.unsubscribe('example-event');
unsub = ps.unsubscribe('example-event');

expect(unsub).toBe('example-event');
expect(ps._pubsub_topics['example-event'].length).toBe(0);
expect(ps.hasSubscribers('example-event')).toBe(false);
});

it('Unsubscribes from event using tokenized reference to the subscription.', function () {
function listener() {}

var ps = new PubSub(),
sub = ps.subscribe('example-event', listener),
sub2 = ps.subscribe('example-event', listener),
sub3 = ps.subscribe('example-event', listener);

function listener() {}

expect(ps.unsubscribe(sub)).toBe(0);
expect(ps._pubsub_topics['example-event'].length).toBe(2);
expect(ps.subscribers()['example-event'].length).toBe(2);
});

it('Unsubscribes from an event that was not subscribed before.', function () {
Expand All @@ -79,7 +81,6 @@ describe('Unsubscribe from event.', function () {
});
});


// Check if there are subscribers for a specific topic.
describe('Check if there are subscribers for a specific topic.', function () {
it('Should return true when checking if there are subscribers for "message" event.', function () {
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('Check if there are subscribers for a specific topic.', function () {
describe('Clears all subscriptions at once', function () {
it('Should unsubscribe from all subscriptions', function () {
var ps = new PubSub();
var listener = function listener() {}
var listener = function listener() {};

// Subscribe to some events
ps.subscribe('eventA', listener);
Expand Down

0 comments on commit b96bd1f

Please sign in to comment.