Skip to content

Commit

Permalink
Merge pull request #7 from fernandogmar/master
Browse files Browse the repository at this point in the history
AMD / CommonJS support
  • Loading branch information
mroderick committed Mar 31, 2012
2 parents 60c1740 + a9f022e commit 31ad779
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Aptana files #
################
.project
.settings/
.tmp*


9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Don't say I didn't warn you.
* Easy to understand (messages are async by default)
* Small(ish)
* Compatible! ES3 compatiable, should be able to run everywhere that can execute JavaScript
* AMD / CommonJS module

## Examples

Expand Down Expand Up @@ -78,7 +79,6 @@ The tests are done using [BusterJS](http://busterjs.org) and the excellent [Sino
## Future of PubSubJS

* Build script to create the following wrappers
* AMD / CommonJS module
* jQuery plugin
* Ender.js wrapper
* Hierarchical addressing of topics, using either dots (some.hierarchy.of.topics) or slashes (/some/hierarchy/of/topics).
Expand All @@ -91,4 +91,9 @@ The tests are done using [BusterJS](http://busterjs.org) and the excellent [Sino

## Versioning

PubSubJS uses [Semantic Versioning](http://semver.org/) for predictable versioning.
PubSubJS uses [Semantic Versioning](http://semver.org/) for predictable versioning.

## Changelog

* v1.0.3
* AMD / CommonJS module support (@fernandogmar)
47 changes: 38 additions & 9 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,31 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* PubSub.unsubscribe( token );
* }, 0)
**/
var PubSub = {};
(function(p){
"use strict";
(function() {
"use strict";

var PubSub = {
version: "1.0.3"
};

// Establish the root object, `window` in the browser, or `global` on the server.
var root = this;

// Export the Underscore object for **Node.js** and **"CommonJS"**, with
// backwards-compatibility for the old `require()` API. If we're not in
// CommonJS, add `PubSub` to the global object via a string identifier for
// the Closure Compiler "advanced" mode. Registration as an AMD module
// via define() happens at the end of this file.
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = PubSub;
}
exports.PubSub = PubSub;
}
else {
root['PubSub'] = PubSub;
}

p.version = "1.0.2";

var messages = {},
lastUid = -1;
Expand Down Expand Up @@ -105,7 +125,7 @@ var PubSub = {};
* - sync (Boolean): Forces publication to be syncronous, which is more confusing, but faster
* Publishes the the message, passing the data to it's subscribers
**/
p.publish = function( message, data ){
PubSub.publish = function( message, data ){
return publish( message, data, false );
};

Expand All @@ -116,7 +136,7 @@ var PubSub = {};
* - sync (Boolean): Forces publication to be syncronous, which is more confusing, but faster
* Publishes the the message synchronously, passing the data to it's subscribers
**/
p.publishSync = function( message, data ){
PubSub.publishSync = function( message, data ){
return publish( message, data, true );
};

Expand All @@ -126,7 +146,7 @@ var PubSub = {};
* - func (Function): The function to call when a new message is published
* Subscribes the passed function to the passed message. Every returned token is unique and should be stored if you need to unsubscribe
**/
p.subscribe = function( message, func ){
PubSub.subscribe = function( message, func ){
// message is not registered yet
if ( !messages.hasOwnProperty( message ) ){
messages[message] = [];
Expand All @@ -146,7 +166,7 @@ var PubSub = {};
* - token (String): The token of the function to unsubscribe
* Unsubscribes a specific subscriber from a specific message using the unique token
**/
p.unsubscribe = function( token ){
PubSub.unsubscribe = function( token ){
var m, i, j;
for ( m in messages ){
if ( messages.hasOwnProperty( m ) ){
Expand All @@ -160,4 +180,13 @@ var PubSub = {};
}
return false;
};
}(PubSub));

// AMD define happens at the end for compatibility with AMD loaders
// that don't enforce next-turn semantics on modules.
if (typeof define === 'function' && define.amd) {
define('pubsub', function(){
return PubSub;
});
}

}).call(this);
2 changes: 1 addition & 1 deletion test/test-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(function( global ){
"use strict";

var EXPECTED_VERSION = "1.0.2";
var EXPECTED_VERSION = "1.0.3";

// helps us make sure that the order of the tests have no impact on their succes
function getUniqueString(){
Expand Down

0 comments on commit 31ad779

Please sign in to comment.