Skip to content

Commit

Permalink
Fix for #43
Browse files Browse the repository at this point in the history
Refactor the internals to not use arrays, so we are free to modify the list of subscribers without causing problems.
  • Loading branch information
mroderick committed Feb 11, 2014
1 parent 751b0c4 commit 033294a
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ https://github.com/mroderick/PubSubJS
messages = {},
lastUid = -1;

function hasKeys(obj){
var key;

for (key in obj){
if ( obj.hasOwnProperty(key) ){
return true;
}
}
return false;
}

/**
* Returns a function that throws the passed exception, for use as argument for setTimeout
* @param { Object } ex An Error object
Expand All @@ -60,17 +71,16 @@ https://github.com/mroderick/PubSubJS
function deliverMessage( originalMessage, matchedMessage, data, immediateExceptions ){
var subscribers = messages[matchedMessage],
callSubscriber = immediateExceptions ? callSubscriberWithImmediateExceptions : callSubscriberWithDelayedExceptions,
i, j;
s;

if ( !messages.hasOwnProperty( matchedMessage ) ) {
return;
}

// do not cache the length of the subscribers array, as it might change if there are unsubscribtions
// by subscribers during delivery of a topic
// see https://github.com/mroderick/PubSubJS/issues/26
for ( i = 0; i < subscribers.length; i++ ){
callSubscriber( subscribers[i].func, originalMessage, data );
for (s in subscribers){
if ( subscribers.hasOwnProperty(s)){
callSubscriber( subscribers[s], originalMessage, data );
}
}
}

Expand All @@ -93,13 +103,13 @@ https://github.com/mroderick/PubSubJS

function messageHasSubscribers( message ){
var topic = String( message ),
found = messages.hasOwnProperty( topic ) && messages[topic].length,
found = Boolean(messages.hasOwnProperty( topic ) && hasKeys(messages[topic])),
position = topic.lastIndexOf( '.' );

while ( !found && position !== -1 ){
topic = topic.substr( 0, position );
position = topic.lastIndexOf( '.' );
found = messages.hasOwnProperty( topic ) && messages[topic].length;
found = Boolean(messages.hasOwnProperty( topic ) && hasKeys(messages[topic]));
}

return found;
Expand Down Expand Up @@ -155,13 +165,13 @@ https://github.com/mroderick/PubSubJS

// message is not registered yet
if ( !messages.hasOwnProperty( message ) ){
messages[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 = String(++lastUid);
messages[message].push( { token : token, func : func } );
var token = 'uid_' + String(++lastUid);
messages[message][token] = func;

// return token for unsubscribing
return token;
Expand All @@ -179,18 +189,21 @@ https://github.com/mroderick/PubSubJS
succesfulReturnValue = isToken ? tokenOrFunction : true,

result = false,
m, i;
m, message, t, token;

for ( m in messages ){
if ( messages.hasOwnProperty( m ) ){
for ( i = messages[m].length-1 ; i >= 0; i-- ){
if ( messages[m][i][key] === tokenOrFunction ){
messages[m].splice( i, 1 );
result = succesfulReturnValue;

// tokens are unique, so we can just return here
if ( isToken ){
return result;
message = messages[m];

if ( isToken && message[tokenOrFunction] ){
delete message[tokenOrFunction];
// tokens are unique, so we can just return here
return tokenOrFunction;
} else if (!isToken) {
for ( t in message ){
if (message.hasOwnProperty(t) && message[t] === tokenOrFunction){
delete message[t];
result = succesfulReturnValue;
}
}
}
Expand Down

0 comments on commit 033294a

Please sign in to comment.