Skip to content

Commit

Permalink
Core: Handle invalid topics.
Browse files Browse the repository at this point in the history
  • Loading branch information
duereg authored and scottgonzalez committed May 31, 2012
1 parent 77dd425 commit f3d9dd2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/amplify.core.js
Expand Up @@ -14,6 +14,10 @@ var slice = [].slice,

var amplify = global.amplify = {
publish: function( topic ) {
if ( typeof topic !== "string" ) {
throw new Error( "You must provide a valid topic to publish." );
}

var args = slice.call( arguments, 1 ),
topicSubscriptions,
subscription,
Expand All @@ -37,6 +41,10 @@ var amplify = global.amplify = {
},

subscribe: function( topic, context, callback, priority ) {
if ( typeof topic !== "string" ) {
throw new Error( "You must provide a valid topic to create a subscription." );
}

if ( arguments.length === 3 && typeof callback === "number" ) {
priority = callback;
callback = context;
Expand Down Expand Up @@ -83,6 +91,10 @@ var amplify = global.amplify = {
},

unsubscribe: function( topic, callback ) {
if ( typeof topic !== "string" ) {
throw new Error( "You must provide a valid topic to remove a subscription." );
}

if ( !subscriptions[ topic ] ) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion core/readme.md
@@ -1,6 +1,6 @@
# amplify core

The AmplifyJS core library provides two methods (`amplify.publish` and `amplify.subscribe`). AmplifyJS provides methods to facilitate the Publish and Subscribe messaging pattern in your front-end application. The idea is that someone is broadcasting one or more messages (publishing) and someone else is listening to one or more messages (subscribing). By separating your logic out like this it allows for loose coupling of your components, which results in less brittle and more reusable code.
The AmplifyJS core library provides three methods (`amplify.publish`, `amplify.subscribe`, and `amplify.unsubscribe`). AmplifyJS provides methods to facilitate the Publish and Subscribe messaging pattern in your front-end application. The idea is that someone is broadcasting one or more messages (publishing) and someone else is listening to one or more messages (subscribing). By separating your logic out like this it allows for loose coupling of your components, which results in less brittle and more reusable code.

It is possible to implement the publish and subscribe model by using jQuery custom events, however, the AmplifyJS pub/sub component provides a slightly cleaner interface, prevents collisions between custom events and method names, and allows a priority to your messages.

Expand Down
25 changes: 25 additions & 0 deletions core/test/unit.js
@@ -1,5 +1,30 @@
module( "amplify.core pubsub" );

test( "topic", function() {
expect( 3 );

try {
amplify.publish( undefined, function() {} );
} catch( err ) {
strictEqual( err.message, "You must provide a valid topic to publish.",
"error with no topic to publish" );
}

try {
amplify.subscribe( undefined, function() {} );
} catch( err ) {
strictEqual( err.message, "You must provide a valid topic to create a subscription.",
"error with no topic to subscribe" );
}

try {
amplify.unsubscribe( undefined, function() {} );
} catch( err ) {
strictEqual( err.message, "You must provide a valid topic to remove a subscription.",
"error with no topic to unsubscribe" );
}
});

test( "continuation", function() {
expect( 7 );
amplify.subscribe( "continuation", function() {
Expand Down

0 comments on commit f3d9dd2

Please sign in to comment.