Fixed bug with subscription priority

This commit is contained in:
Jim Cowart 2012-05-22 14:35:36 -04:00
parent 855afeee96
commit d3ad06218b
16 changed files with 188 additions and 109 deletions

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the amd-module version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the amd-module version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the node.js version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the standard lib version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the amd-module version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
{
"name" : "postal",
"description" : "Pub/Sub library providing wildcard subscriptions, complex message handling, etc. Works server and client-side.",
"version" : "0.6.1",
"version" : "0.6.2",
"homepage" : "http://github.com/ifandelse/postal.js",
"repository" : {
"type" : "git",

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the node.js version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

View file

@ -2,7 +2,7 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/
// This is the standard lib version of postal.js
@ -192,6 +192,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},
@ -249,6 +250,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -293,18 +312,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

File diff suppressed because one or more lines are too long

View file

@ -121,6 +121,21 @@ QUnit.specify( "postal.js", function () {
assert( msgData ).equals( "Testing123" );
} );
} );
describe( "When subscribing multiple subscribers with different priority", function () {
var s1, s2, r1 = [];
before( function () {
s1 = postal.subscribe( { channel : "MyChannel", topic : "MyTopic", callback: function() { r1.push("lower"); } } ).withPriority(200);
s2 = postal.subscribe( { channel : "MyChannel", topic : "MyTopic", callback: function() { r1.push("higher"); } } ).withPriority(1);
postal.publish( { channel: "MyChannel", topic: "MyTopic", data: "Oh, Hai!" } );
} );
after( function () {
postal.utils.reset();
} );
it( "should invoke higher priority subscription first", function () {
assert(r1[0] ).isEqualTo("higher");
assert(r1[1] ).isEqualTo("lower");
} );
} );
describe( "When subscribing with a disposeAfter of 5", function () {
var msgReceivedCnt = 0;
before( function () {

View file

@ -11,6 +11,24 @@ var localBus = {
};
},
changePriority: function ( subDef ) {
var idx, found;
if(this.subscriptions[subDef.channel] && this.subscriptions[subDef.channel][subDef.topic]) {
this.subscriptions[subDef.channel][subDef.topic] = _.without(this.subscriptions[subDef.channel][subDef.topic], subDef);
idx = this.subscriptions[subDef.channel][subDef.topic].length - 1;
for ( ; idx >= 0; idx-- ) {
if ( this.subscriptions[subDef.channel][subDef.topic][idx].priority <= subDef.priority ) {
this.subscriptions[subDef.channel][subDef.topic].splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
this.subscriptions[subDef.channel][subDef.topic].unshift( subDef );
}
}
},
publish : function ( envelope ) {
_.each( this.wireTaps, function ( tap ) {
tap( envelope.data, envelope );
@ -55,18 +73,7 @@ var localBus = {
if ( !subs ) {
subs = this.subscriptions[subDef.channel][subDef.topic] = new Array( 0 );
}
idx = subs.length - 1;
for ( ; idx >= 0; idx-- ) {
if ( subs[idx].priority <= subDef.priority ) {
subs.splice( idx + 1, 0, subDef );
found = true;
break;
}
}
if ( !found ) {
subs.unshift( subDef );
}
subs.push( subDef );
return subDef;
},

View file

@ -117,6 +117,7 @@ SubscriptionDefinition.prototype = {
throw "Priority must be a number";
}
this.priority = priority;
postal.configuration.bus.changePriority( this );
return this;
},

View file

@ -2,5 +2,5 @@
postal.js
Author: Jim Cowart
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.6.1
Version 0.6.2
*/