function clearAllSubscriptions(){ messages = {}; }
...
// no further notications for 'a.b' and 'a.b.c' topics
// notifications for 'a' will still get published
```
### Clear all subscriptions
```javascript
PubSub.clearAllSubscriptions();
// all subscriptions are removed
```
### Hierarchical addressing
```javascript
// create a subscriber to receive all topics from a hierarchy of topics
...
function clearSubscriptions(topic){ var m; for (m in messages){ if (messages.hasOwnProperty(m) && m.indexOf(topic) === 0){ delete messages[m]; } } }
n/a
publish = function ( message, data ){ return publish( message, data, false, PubSub.immediateExceptions ); }
...
// add the function to the list of subscribers for a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe( 'MY TOPIC', mySubscriber );
// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
// publish a topic syncronously, which is faster in some environments,
// but will get confusing when one topic triggers new topics in the
// same execution chain
// USE WITH CAUTION, HERE BE DRAGONS!!!
PubSub.publishSync( 'MY TOPIC', 'hello world!' );
```
...
publishSync = function ( message, data ){ return publish( message, data, true, PubSub.immediateExceptions ); }
...
// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
// publish a topic syncronously, which is faster in some environments,
// but will get confusing when one topic triggers new topics in the
// same execution chain
// USE WITH CAUTION, HERE BE DRAGONS!!!
PubSub.publishSync( 'MY TOPIC', 'hello world!' );
```
### Cancel specific subscription
```javascript
// create a function to receive the topic
var mySubscriber = function( msg, data ){
...
subscribe = function ( message, func ){ if ( typeof func !== 'function'){ return false; } // message is not registered yet if ( !messages.hasOwnProperty( message ) ){ messages[message] = {}; } // forcing token as String, to allow for future expansions without breaking usage // and allow for easy use as key names for the 'messages' object var token = 'uid_' + String(++lastUid); messages[message][token] = func; // return token for unsubscribing return token; }
...
var mySubscriber = function( msg, data ){
console.log( msg, data );
};
// add the function to the list of subscribers for a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe( 'MY TOPIC', mySubscriber );
// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
// publish a topic syncronously, which is faster in some environments,
// but will get confusing when one topic triggers new topics in the
// same execution chain
...
unsubscribe = function (value){ var descendantTopicExists = function(topic) { var m; for ( m in messages ){ if ( messages.hasOwnProperty(m) && m.indexOf(topic) === 0 ){ // a descendant of the topic exists: return true; } } return false; }, isTopic = typeof value === 'string' && ( messages.hasOwnProperty(value) || descendantTopicExists(value) ), isToken = !isTopic && typeof value === 'string', isFunction = typeof value === 'function', result = false, m, message, t; if (isTopic){ PubSub.clearSubscriptions(value); return; } for ( m in messages ){ if ( messages.hasOwnProperty( m ) ){ message = messages[m]; if ( isToken && message[value] ){ delete message[value]; result = value; // tokens are unique, so we can just stop here break; } if (isFunction) { for ( t in message ){ if (message.hasOwnProperty(t) && message[t] === value){ delete message[t]; result = true; } } } } } return result; }
...
// add the function to the list of subscribers to a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe( 'MY TOPIC', mySubscriber );
// unsubscribe this subscriber from this topic
PubSub.unsubscribe( token );
```
### Cancel all subscriptions for a function
```javascript
// create a function to receive the topic
var mySubscriber = function( msg, data ){
...
function clearAllSubscriptions(){ messages = {}; }
...
// no further notications for 'a.b' and 'a.b.c' topics
// notifications for 'a' will still get published
```
### Clear all subscriptions
```javascript
PubSub.clearAllSubscriptions();
// all subscriptions are removed
```
### Hierarchical addressing
```javascript
// create a subscriber to receive all topics from a hierarchy of topics
...
function clearSubscriptions(topic){ var m; for (m in messages){ if (messages.hasOwnProperty(m) && m.indexOf(topic) === 0){ delete messages[m]; } } }
n/a
publish = function ( message, data ){ return publish( message, data, false, PubSub.immediateExceptions ); }
...
// add the function to the list of subscribers for a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe( 'MY TOPIC', mySubscriber );
// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
// publish a topic syncronously, which is faster in some environments,
// but will get confusing when one topic triggers new topics in the
// same execution chain
// USE WITH CAUTION, HERE BE DRAGONS!!!
PubSub.publishSync( 'MY TOPIC', 'hello world!' );
```
...
publishSync = function ( message, data ){ return publish( message, data, true, PubSub.immediateExceptions ); }
...
// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
// publish a topic syncronously, which is faster in some environments,
// but will get confusing when one topic triggers new topics in the
// same execution chain
// USE WITH CAUTION, HERE BE DRAGONS!!!
PubSub.publishSync( 'MY TOPIC', 'hello world!' );
```
### Cancel specific subscription
```javascript
// create a function to receive the topic
var mySubscriber = function( msg, data ){
...
subscribe = function ( message, func ){ if ( typeof func !== 'function'){ return false; } // message is not registered yet if ( !messages.hasOwnProperty( message ) ){ messages[message] = {}; } // forcing token as String, to allow for future expansions without breaking usage // and allow for easy use as key names for the 'messages' object var token = 'uid_' + String(++lastUid); messages[message][token] = func; // return token for unsubscribing return token; }
...
var mySubscriber = function( msg, data ){
console.log( msg, data );
};
// add the function to the list of subscribers for a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe( 'MY TOPIC', mySubscriber );
// publish a topic asyncronously
PubSub.publish( 'MY TOPIC', 'hello world!' );
// publish a topic syncronously, which is faster in some environments,
// but will get confusing when one topic triggers new topics in the
// same execution chain
...
unsubscribe = function (value){ var descendantTopicExists = function(topic) { var m; for ( m in messages ){ if ( messages.hasOwnProperty(m) && m.indexOf(topic) === 0 ){ // a descendant of the topic exists: return true; } } return false; }, isTopic = typeof value === 'string' && ( messages.hasOwnProperty(value) || descendantTopicExists(value) ), isToken = !isTopic && typeof value === 'string', isFunction = typeof value === 'function', result = false, m, message, t; if (isTopic){ PubSub.clearSubscriptions(value); return; } for ( m in messages ){ if ( messages.hasOwnProperty( m ) ){ message = messages[m]; if ( isToken && message[value] ){ delete message[value]; result = value; // tokens are unique, so we can just stop here break; } if (isFunction) { for ( t in message ){ if (message.hasOwnProperty(t) && message[t] === value){ delete message[t]; result = true; } } } } } return result; }
...
// add the function to the list of subscribers to a particular topic
// we're keeping the returned token, in order to be able to unsubscribe
// from the topic later on
var token = PubSub.subscribe( 'MY TOPIC', mySubscriber );
// unsubscribe this subscriber from this topic
PubSub.unsubscribe( token );
```
### Cancel all subscriptions for a function
```javascript
// create a function to receive the topic
var mySubscriber = function( msg, data ){
...