Comparing version 1.4.1 to 1.4.2
{ | ||
"name": "PubSubJS", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"main": "src/pubsub.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
{ | ||
"name": "pubsub-js", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"description": "Dependency free publish/subscribe library", | ||
@@ -5,0 +5,0 @@ "main": "./src/pubsub.js", |
@@ -38,2 +38,13 @@ /* | ||
function hasKeys(obj){ | ||
var key; | ||
for (key in obj){ | ||
if ( obj.hasOwnProperty(key) ){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -64,3 +75,3 @@ * Returns a function that throws the passed exception, for use as argument for setTimeout | ||
callSubscriber = immediateExceptions ? callSubscriberWithImmediateExceptions : callSubscriberWithDelayedExceptions, | ||
i, j; | ||
s; | ||
@@ -71,7 +82,6 @@ if ( !messages.hasOwnProperty( matchedMessage ) ) { | ||
// 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 ); | ||
} | ||
} | ||
@@ -99,3 +109,3 @@ } | ||
var topic = String( message ), | ||
found = messages.hasOwnProperty( topic ) && messages[topic].length, | ||
found = Boolean(messages.hasOwnProperty( topic ) && hasKeys(messages[topic])), | ||
position = topic.lastIndexOf( '.' ); | ||
@@ -106,3 +116,3 @@ | ||
position = topic.lastIndexOf( '.' ); | ||
found = messages.hasOwnProperty( topic ) && messages[topic].length; | ||
found = Boolean(messages.hasOwnProperty( topic ) && hasKeys(messages[topic])); | ||
} | ||
@@ -163,3 +173,3 @@ | ||
if ( !messages.hasOwnProperty( message ) ){ | ||
messages[message] = []; | ||
messages[message] = {}; | ||
} | ||
@@ -169,4 +179,4 @@ | ||
// 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; | ||
@@ -185,18 +195,19 @@ // return token for unsubscribing | ||
var isToken = typeof tokenOrFunction === 'string', | ||
key = isToken ? 'token' : 'func', | ||
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; | ||
message = messages[m]; | ||
// tokens are unique, so we can just return here | ||
if ( isToken ){ | ||
return result; | ||
if ( isToken && message[tokenOrFunction] ){ | ||
delete message[tokenOrFunction]; | ||
result = tokenOrFunction; | ||
// tokens are unique, so we can just stop here | ||
break; | ||
} else if (!isToken) { | ||
for ( t in message ){ | ||
if (message.hasOwnProperty(t) && message[t] === tokenOrFunction){ | ||
delete message[t]; | ||
result = true; | ||
} | ||
@@ -203,0 +214,0 @@ } |
@@ -5,3 +5,3 @@ /*jslint white:true, plusplus:true*/ | ||
module, | ||
assert, | ||
buster, | ||
define, | ||
@@ -28,3 +28,4 @@ window | ||
var TestHelper = {}; | ||
var TestHelper = {}, | ||
assert = buster.assert; | ||
@@ -62,2 +63,2 @@ // helps us make sure that the order of the tests have no impact on their succes | ||
return TestHelper; | ||
})); | ||
})); |
@@ -5,3 +5,2 @@ /*jslint white:true*/ | ||
buster, | ||
assert, | ||
require, | ||
@@ -14,3 +13,4 @@ sinon, | ||
var PubSub = global.PubSub || require("../src/pubsub"); | ||
var PubSub = global.PubSub || require("../src/pubsub"), | ||
assert = buster.assert; | ||
@@ -62,2 +62,2 @@ /** | ||
}); | ||
}(this)); | ||
}(this)); |
@@ -5,3 +5,2 @@ /*jslint white:true*/ | ||
buster, | ||
assert, | ||
require, | ||
@@ -14,3 +13,4 @@ sinon | ||
var PubSub = global.PubSub || require("../src/pubsub"), | ||
TestHelper = global.TestHelper || require("../test/helper"); | ||
TestHelper = global.TestHelper || require("../test/helper"), | ||
assert = buster.assert; | ||
@@ -218,2 +218,2 @@ buster.testCase( "Hierarchical addressing", { | ||
}(this)); | ||
}(this)); |
@@ -5,3 +5,2 @@ /*jslint white:true*/ | ||
buster, | ||
assert, | ||
require, | ||
@@ -15,3 +14,4 @@ sinon | ||
PubSub = global.PubSub || require("../src/pubsub"), | ||
TestHelper = global.TestHelper || require("../test/helper"); | ||
TestHelper = global.TestHelper || require("../test/helper"), | ||
assert = buster.assert; | ||
@@ -73,2 +73,2 @@ buster.testCase( "jQuery integration", { | ||
}); | ||
}(this)); | ||
}(this)); |
@@ -5,4 +5,2 @@ /*jslint white:true, stupid:true*/ | ||
buster, | ||
assert, | ||
refute, | ||
require, | ||
@@ -15,3 +13,5 @@ sinon | ||
var PubSub = global.PubSub || require("../src/pubsub"), | ||
TestHelper = global.TestHelper || require("../test/helper"); | ||
TestHelper = global.TestHelper || require("../test/helper"), | ||
assert = buster.assert, | ||
refute = buster.refute; | ||
@@ -170,5 +170,33 @@ buster.testCase( "publish method", { | ||
delete PubSub.immediateExceptions; | ||
}, | ||
"publish should call all subscribers, even when there are unsubscriptions within" : function(done){ | ||
var topic = TestHelper.getUniqueString(), | ||
spy1 = this.spy(), | ||
func1 = function func1(){ | ||
PubSub.unsubscribe(func1); | ||
spy1(); | ||
}, | ||
spy2 = this.spy(), | ||
func2 = function func2(){ | ||
PubSub.unsubscribe(func2); | ||
spy2(); | ||
}, | ||
clock = this.useFakeTimers(); | ||
PubSub.subscribe(topic, func1); | ||
PubSub.subscribe(topic, func2); | ||
PubSub.publish(topic, 'some data'); | ||
clock.tick(1); | ||
assert(spy1.called, 'expected spy1 to be called'); | ||
assert(spy2.called, 'expected spy2 to be called'); | ||
clock.restore(); | ||
done(); | ||
} | ||
}); | ||
}(this)); | ||
}(this)); |
@@ -5,4 +5,2 @@ /*jslint white:true, plusplus:true */ | ||
buster, | ||
assert, | ||
refute, | ||
require, | ||
@@ -13,5 +11,7 @@ sinon | ||
"use strict"; | ||
var PubSub = global.PubSub || require("../src/pubsub"), | ||
TestHelper = global.TestHelper || require("../test/helper"); | ||
TestHelper = global.TestHelper || require("../test/helper"), | ||
assert = buster.assert, | ||
refute = buster.refute; | ||
@@ -27,3 +27,3 @@ buster.testCase( "subscribe method", { | ||
}, | ||
"should return new token for several subscriptions with same function" : function(){ | ||
@@ -43,3 +43,3 @@ var func = function(){}, | ||
}, | ||
"should return unique tokens for each namespaced subscription" : function(){ | ||
@@ -58,3 +58,3 @@ var func = function(){}, | ||
}, | ||
"should return unique token for unique functions" : function(){ | ||
@@ -65,3 +65,3 @@ var tokens = [], | ||
i; | ||
function bakeFunc( value ){ | ||
@@ -109,2 +109,2 @@ return function(){ | ||
}(this)); | ||
}(this)); |
@@ -5,4 +5,2 @@ /*jslint white:true, stupid:true*/ | ||
buster, | ||
assert, | ||
refute, | ||
require, | ||
@@ -15,3 +13,5 @@ sinon | ||
var PubSub = global.PubSub || require("../src/pubsub"), | ||
TestHelper = global.TestHelper || require("../test/helper"); | ||
TestHelper = global.TestHelper || require("../test/helper"), | ||
assert = buster.assert, | ||
refute = buster.refute; | ||
@@ -94,2 +94,2 @@ buster.testCase( "unsubscribe method", { | ||
}(this)); | ||
}(this)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
298749
8690