mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
Ensuring context in self-disposing callbacks
- allowing context modification after delayed configurations
This commit is contained in:
parent
ac3e7ea7d0
commit
8bdb2586a6
2 changed files with 40 additions and 5 deletions
|
|
@ -130,6 +130,17 @@ describe( "SubscriptionDefinition", function () {
|
|||
} ).withContext(context).defer();
|
||||
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact when modified later", function ( done ) {
|
||||
var context = {
|
||||
key : 1234
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).defer().withContext(context);
|
||||
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( "When delaying the callback", function () {
|
||||
|
|
@ -241,4 +252,27 @@ describe( "SubscriptionDefinition", function () {
|
|||
sDefe.callback.call( sDefe.context, 1 );
|
||||
});
|
||||
} );
|
||||
|
||||
describe( "When self disposing", function () {
|
||||
|
||||
it( "Should be inactive", function () {
|
||||
var sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
} ).withContext(context).disposeAfter( 1 );
|
||||
|
||||
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
|
||||
|
||||
expect( sDefe.inactive ).to.be( true );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key : 1234
|
||||
};
|
||||
var sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).withContext(context).disposeAfter( 200 );
|
||||
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
|
@ -36,11 +36,11 @@ SubscriptionDefinition.prototype = {
|
|||
},
|
||||
|
||||
defer : function () {
|
||||
var that = this;
|
||||
var fn = this.callback;
|
||||
var context = this.context;
|
||||
this.callback = function ( data, env ) {
|
||||
setTimeout( function () {
|
||||
fn.call( context, data, env );
|
||||
fn.call( that.context, data, env );
|
||||
}, 0 );
|
||||
};
|
||||
return this;
|
||||
|
|
@ -50,13 +50,14 @@ SubscriptionDefinition.prototype = {
|
|||
if ( _.isNaN( maxCalls ) || maxCalls <= 0 ) {
|
||||
throw "The value provided to disposeAfter (maxCalls) must be a number greater than zero.";
|
||||
}
|
||||
var that = this;
|
||||
var fn = this.callback;
|
||||
var dispose = _.after( maxCalls, _.bind( function () {
|
||||
this.unsubscribe();
|
||||
}, this ) );
|
||||
|
||||
this.callback = function () {
|
||||
fn.apply( this.context, arguments );
|
||||
fn.apply( that.context, arguments );
|
||||
dispose();
|
||||
};
|
||||
return this;
|
||||
|
|
@ -113,11 +114,11 @@ SubscriptionDefinition.prototype = {
|
|||
if ( _.isNaN( milliseconds ) ) {
|
||||
throw "Milliseconds must be a number";
|
||||
}
|
||||
var that = this;
|
||||
var fn = this.callback;
|
||||
var context = this.context;
|
||||
this.callback = function ( data, env ) {
|
||||
setTimeout( function () {
|
||||
fn.call( context, data, env );
|
||||
fn.call( that.context, data, env );
|
||||
}, milliseconds );
|
||||
};
|
||||
return this;
|
||||
|
|
|
|||
Loading…
Reference in a new issue