Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
georapbox committed Jan 20, 2017
1 parent d6d2e90 commit e8dd681
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 18 deletions.
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# CHANGELOG

## v3.0.0

### Breaking changes

The default API method aliases are deprecated and removed from v3.0.0 onwards. However there is a new method `alias` introduced, that allows to create your own aliases. Therefore, if you already use those aliases in a project you can use the `alias` method to provide your own.

Below is a map of the default aliases that existed prior to version 3.0.0:

| Original method | Alias method |
| --------------- | ------------- |
| `subscribe` | `on` |
| `subscribeOnce` | `once` |
| `publishSync` | `triggerSync` |
| `unsubscribe` | `off` |
| `hasSubscribers` | `has` |

To create your own aliases:

```js
var pubsub = new PubSub().alias({
subscribe: 'on',
subscribeOnce: 'once',
publish: 'trigger',
publishSync: 'triggerSync',
unsubscribe: 'off',
hasSubscribers: 'has'
});
```

### Other updates

- Add public method `unsubscribeAll` to clear all subscriptions whatsoever.
- Add public method `alias` to create your own method aliases. (See above)
- Provide source-map for the minified library.

## v2.1.0
- Add support for publishing events synchronously using `publishSync` method.
- Add public method `hasSubscribers` to check if there are subscribers for a specific topic.
Expand Down
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (grunt) {

uglify: {
options: {
sourceMap: 'dist/',
banner: '/**\n' +
' * <%= pkg.name %>\n' +
' * <%= pkg.description %>\n' +
Expand Down
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ $ npm install PubSub
$ bower install georapbox.pubsub.js
```

## Changelog

For API updates and breaking changes, check the [CHANGELOG](https://github.com/georapbox/PubSub/blob/master/CHANGELOG.md).

## API

* [PubSub](#PubSub)
Expand All @@ -35,6 +39,8 @@ $ bower install georapbox.pubsub.js
* [.publishSync(topic, [data])](#PubSub+publishSync) ⇒ <code>boolean</code>
* [.unsubscribe(topic)](#PubSub+unsubscribe) ⇒ <code>boolean</code> &#124; <code>string</code>
* [.hasSubscribers(topic)](#PubSub+hasSubscribers) ⇒ <code>Boolean</code>
* [.unsubscribeAll()](#PubSub+unsubscribeAll) ⇒ <code>[PubSub](#PubSub)</code>
* [.alias(aliasMap)](#PubSub+alias) ⇒ <code>[PubSub](#PubSub)</code>

<a name="new_PubSub_new"></a>

Expand Down Expand Up @@ -177,15 +183,45 @@ pubsub.on('message', function (data) {
pubsub.hasSubscribers('message');
// -> true
```
<a name="PubSub+unsubscribeAll"></a>

### pubSub.unsubscribeAll() ⇒ <code>[PubSub](#PubSub)</code>
Clears all subscriptions whatsoever.

**Kind**: instance method of <code>[PubSub](#PubSub)</code>
**Returns**: <code>[PubSub](#PubSub)</code> - The PubSub instance.
**this**: <code>{PubSub}</code>
**Example**
```js
var pubsub = new PubSub();
...
...
pubsub.unsubscribeAll();
```
<a name="PubSub+alias"></a>

### pubSub.alias(aliasMap) ⇒ <code>[PubSub](#PubSub)</code>
Creates aliases for public methods.

### Methods aliases
- `on` - `subscribe`
- `once` - `subscribeOnce`
- `trigger` - `publish`
- `triggerSync` - `publishSync`
- `off` - `unsubscribe`
- `has` - `hasSubscribers`
**Kind**: instance method of <code>[PubSub](#PubSub)</code>
**Returns**: <code>[PubSub](#PubSub)</code> - The PubSub instance.
**this**: <code>{PubSub}</code>

| Param | Type | Description |
| --- | --- | --- |
| aliasMap | <code>object</code> | A plain object that maps the public methods to their aliases. |

**Example**
```js
var pubsub = new PubSub().alias({
subscribe: 'on',
subscribeOnce: 'once',
publish: 'trigger',
publishSync: 'triggerSync',
unsubscribe: 'off',
hasSubscribers: 'has'
});
```

## Minify

Expand Down
6 changes: 4 additions & 2 deletions dist/pubsub.min.js

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

1 change: 1 addition & 0 deletions dist/pubsub.min.js.map

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PubSub",
"version": "2.1.0",
"version": "3.0.0",
"description": "Javascript implementation of the Publish/Subscribe pattern.",
"main": "src/pubsub.js",
"scripts": {
Expand Down
66 changes: 58 additions & 8 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 2.1.0
* @version 3.0.0
* @author George Raptis <[email protected]> (georapbox.github.io)
* @homepage https://github.com/georapbox/PubSub#readme
* @repository git+https://github.com/georapbox/PubSub.git
Expand Down Expand Up @@ -68,6 +68,7 @@
function PubSub() {
this.topics = {}; // Storage for topics that can be broadcast or listened to.
this.subUid = -1; // A topic identifier.
return this;
}

/**
Expand Down Expand Up @@ -252,13 +253,62 @@
return false;
};

// Alias for public methods.
PubSub.prototype.on = alias('subscribe');
PubSub.prototype.once = alias('subscribeOnce');
PubSub.prototype.trigger = alias('publish');
PubSub.prototype.triggerSync = alias('publishSync');
PubSub.prototype.off = alias('unsubscribe');
PubSub.prototype.has = alias('hasSubscribers');
/**
* Clears all subscriptions whatsoever.
*
* @memberof PubSub
* @this {PubSub}
* @return {PubSub} The PubSub instance.
* @example
*
* var pubsub = new PubSub();
* ...
* ...
* pubsub.unsubscribeAll();
*/
PubSub.prototype.unsubscribeAll = function () {
var prop;

for (prop in this.topics) {
if (Object.hasOwnProperty.call(this.topics, prop)) {
this.topics[prop] = [];
}
}

return this;
};

/**
* Creates aliases for public methods.
*
* @memberof PubSub
* @this {PubSub}
* @param {object} aliasMap A plain object that maps the public methods to their aliases.
* @return {PubSub} The PubSub instance.
* @example
*
* var pubsub = new PubSub().alias({
* subscribe: 'on',
* subscribeOnce: 'once',
* publish: 'trigger',
* publishSync: 'triggerSync',
* unsubscribe: 'off',
* hasSubscribers: 'has'
* });
*/
PubSub.prototype.alias = function (aliasMap) {
var prop;

for (prop in aliasMap) {
if (Object.hasOwnProperty.call(aliasMap, prop)) {
if (PubSub.prototype[prop]) {
PubSub.prototype[aliasMap[prop]] = alias(prop);
}
}
}

return this;
};

return PubSub;
}));
36 changes: 36 additions & 0 deletions tests/pubsub.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,39 @@ describe('Check if there are subscribers for a specific topic.', function () {
expect(ps.hasSubscribers('message')).toBe(false);
});
});

// Clear all subscriptions at once.
describe('Clears all subscriptions at once', function () {
it('Should unsubscribe from all subscriptions', function () {
var ps = new PubSub();
var listener = function listener() {}

// Subscribe to some events
ps.subscribe('eventA', listener);
ps.subscribe('eventB', listener);
ps.subscribe('eventC', listener);
ps.subscribeOnce('eventA', listener);
ps.subscribeOnce('eventB', listener);
ps.subscribeOnce('eventC', listener);

// Unsubscribe from all
ps.unsubscribeAll();

expect(ps.hasSubscribers('eventA')).toBe(false);
expect(ps.hasSubscribers('eventB')).toBe(false);
expect(ps.hasSubscribers('eventC')).toBe(false);
});
});

// Alias methods
describe('Public methods alias', function () {
it('Should create aliases "on" and "off" for "subscribe" and "unsubscribe" methods respectively', function () {
var ps = new PubSub().alias({
subscribe: 'on',
unsubscribe: 'off'
});

expect(PubSub.prototype.on).not.toBeUndefined();
expect(PubSub.prototype.off).not.toBeUndefined();
});
});

0 comments on commit e8dd681

Please sign in to comment.