added .once() shortcut to SubscriptionDefinition and related test

This commit is contained in:
Jim Cowart 2012-11-07 21:30:51 -05:00
parent 11b3d00024
commit 5c2727acfd
12 changed files with 69 additions and 20 deletions

View file

@ -4,17 +4,17 @@
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.7.3
*/
(function ( root, doc, factory ) {
(function ( root, factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( ["underscore"], function ( _ ) {
return factory( _, root, doc );
return factory( _, root );
} );
} else {
// Browser globals
factory( root._, root, doc );
factory( root._, root );
}
}( this, document, function ( _, global, document, undefined ) {
}( this, function ( _, global, undefined ) {
var DEFAULT_CHANNEL = "/",
DEFAULT_PRIORITY = 50,
@ -169,6 +169,10 @@
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";

File diff suppressed because one or more lines are too long

View file

@ -4,17 +4,17 @@
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.7.3
*/
(function ( root, doc, factory ) {
(function ( root, factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( ["underscore"], function ( _ ) {
return factory( _, root, doc );
return factory( _, root );
} );
} else {
// Browser globals
factory( root._, root, doc );
factory( root._, root );
}
}( this, document, function ( _, global, document, undefined ) {
}( this, function ( _, global, undefined ) {
var DEFAULT_CHANNEL = "/",
DEFAULT_PRIORITY = 50,
@ -169,6 +169,10 @@
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";

File diff suppressed because one or more lines are too long

View file

@ -161,6 +161,10 @@ SubscriptionDefinition.prototype = {
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";

View file

@ -4,17 +4,17 @@
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.7.3
*/
(function ( root, doc, factory ) {
(function ( root, factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( ["underscore"], function ( _ ) {
return factory( _, root, doc );
return factory( _, root );
} );
} else {
// Browser globals
factory( root._, root, doc );
factory( root._, root );
}
}( this, document, function ( _, global, document, undefined ) {
}( this, function ( _, global, undefined ) {
var DEFAULT_CHANNEL = "/",
DEFAULT_PRIORITY = 50,
@ -169,6 +169,10 @@
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";

File diff suppressed because one or more lines are too long

View file

@ -4,17 +4,17 @@
License: Dual licensed MIT (http://www.opensource.org/licenses/mit-license) & GPL (http://www.opensource.org/licenses/gpl-license)
Version 0.7.3
*/
(function ( root, doc, factory ) {
(function ( root, factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( ["underscore"], function ( _ ) {
return factory( _, root, doc );
return factory( _, root );
} );
} else {
// Browser globals
factory( root._, root, doc );
factory( root._, root );
}
}( this, document, function ( _, global, document, undefined ) {
}( this, function ( _, global, undefined ) {
var DEFAULT_CHANNEL = "/",
DEFAULT_PRIORITY = 50,
@ -169,6 +169,10 @@
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";

File diff suppressed because one or more lines are too long

View file

@ -161,6 +161,10 @@ SubscriptionDefinition.prototype = {
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";

View file

@ -156,6 +156,27 @@ describe( "Postal", function () {
expect( msgReceivedCnt ).to.be( 5 );
} );
} );
describe( "When subscribing with once()", function () {
var msgReceivedCnt = 0;
before( function () {
channel = postal.channel( { channel : "MyChannel", topic : "MyTopic" } );
subscription = channel.subscribe( function ( data ) {
msgReceivedCnt++;
} ).once();
channel.publish( "Testing123" );
channel.publish( "Testing123" );
channel.publish( "Testing123" );
channel.publish( "Testing123" );
channel.publish( "Testing123" );
channel.publish( "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked 1 time", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
} );
describe( "When subscribing and ignoring duplicates", function () {
var subInvokedCnt = 0;
before( function () {

View file

@ -72,6 +72,10 @@ SubscriptionDefinition.prototype = {
return this;
},
once: function() {
this.disposeAfter(1);
},
withConstraint : function ( predicate ) {
if ( !_.isFunction( predicate ) ) {
throw "Predicate constraint must be a function";