mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
Merge branch 'subscription_fixes' of https://github.com/dertseha/postal.js into dertseha-subscription_fixes
This commit is contained in:
commit
ffb93fcb2f
3 changed files with 94 additions and 8 deletions
|
|
@ -119,6 +119,28 @@ describe( "SubscriptionDefinition", function () {
|
|||
sDefe.callback( "second", { topic : "TestTopic" } );
|
||||
results.push( "first" );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key : 1234
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).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 () {
|
||||
|
|
@ -135,6 +157,17 @@ describe( "SubscriptionDefinition", function () {
|
|||
sDefe.callback( "second", { topic : "TestTopic" } );
|
||||
results.push( "first" );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key : 1234
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).withContext(context).withDelay( 200 );
|
||||
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( "When debouncing the callback", function () {
|
||||
|
|
@ -166,6 +199,21 @@ describe( "SubscriptionDefinition", function () {
|
|||
done();
|
||||
}, 2400 );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key : 5678
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).withContext(context).withDebounce( 100 );
|
||||
|
||||
sDefe.callback.call( sDefe.context, 1 );
|
||||
setTimeout( function () {
|
||||
sDefe.callback.call( sDefe.context, 2 );
|
||||
}, 200 ); // should invoke callback
|
||||
});
|
||||
} );
|
||||
|
||||
describe( "When throttling the callback", function () {
|
||||
|
|
@ -191,5 +239,40 @@ describe( "SubscriptionDefinition", function () {
|
|||
done();
|
||||
}, 1500 );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key : 'abcd'
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).withContext(context).withThrottle( 500 );
|
||||
|
||||
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" } );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="/ext/mocha.css" />
|
||||
<link rel="stylesheet" href="../ext/mocha.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script type="text/javascript" src="/ext/jquery-1.7.1.js"></script>
|
||||
<script type="text/javascript" src="/ext/underscore.js"></script>
|
||||
<script src="/ext/expect.js"></script>
|
||||
<script src="/ext/mocha.js"></script>
|
||||
<script type="text/javascript" src="../ext/jquery-1.7.1.js"></script>
|
||||
<script type="text/javascript" src="../ext/underscore.js"></script>
|
||||
<script type="text/javascript" src="../ext/expect.js"></script>
|
||||
<script type="text/javascript" src="../ext/mocha.js"></script>
|
||||
<script type="text/javascript">
|
||||
mocha.setup({ ui: 'bdd', timeout: 60000, ignoreLeaks: true });
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -36,10 +36,11 @@ SubscriptionDefinition.prototype = {
|
|||
},
|
||||
|
||||
defer : function () {
|
||||
var that = this;
|
||||
var fn = this.callback;
|
||||
this.callback = function ( data, env ) {
|
||||
setTimeout( function () {
|
||||
fn( data, env );
|
||||
fn.call( that.context, data, env );
|
||||
}, 0 );
|
||||
};
|
||||
return this;
|
||||
|
|
@ -49,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;
|
||||
|
|
@ -112,10 +114,11 @@ SubscriptionDefinition.prototype = {
|
|||
if ( _.isNaN( milliseconds ) ) {
|
||||
throw "Milliseconds must be a number";
|
||||
}
|
||||
var that = this;
|
||||
var fn = this.callback;
|
||||
this.callback = function ( data, env ) {
|
||||
setTimeout( function () {
|
||||
fn( data, env );
|
||||
fn.call( that.context, data, env );
|
||||
}, milliseconds );
|
||||
};
|
||||
return this;
|
||||
|
|
|
|||
Loading…
Reference in a new issue