Merged pull request #4 from @nicholascloud. Prepping for v0.6.4 bump

This commit is contained in:
Jim Cowart 2012-05-24 23:03:15 -04:00
parent e305870bda
commit bb4217f7fc
24 changed files with 502 additions and 96 deletions

View file

@ -46,5 +46,11 @@
Example 8 - using withDelay to delay evaluation of subscription
<ul class="results" id="example8"></ul>
</div>
<div>
Example 9 - using distinct to ignore any duplicate messages
<ul class="results" id="example9"></ul>
</div>
</body>
</html>

View file

@ -127,4 +127,19 @@ define( ['postal', 'postaldiags'], function ( postal, diags ) {
postal.channel( "He.Will.Knock.Four.Times" )
.publish( { value : "Knock!" } );
wdSubscription.unsubscribe();
// Using distinct() to ignore duplicate messages
var revealChannel = postal.channel('detect.cylon'),
revealSubscription = revealChannel.subscribe(function (who) {
$('<li></li>').text(who.name).appendTo($('#example9'));
}).distinct();
postal.channel('detect.cylon').publish({name: 'Boomer'});
postal.channel('detect.cylon').publish({name: 'Saul Tigh'});
postal.channel('detect.cylon').publish({name: 'William Adama'});
postal.channel('detect.cylon').publish({name: 'Helo'});
//ignored!
postal.channel('detect.cylon').publish({name: 'Boomer'});
postal.channel('detect.cylon').publish({name: 'Felix Gaeta'});
//ignored!
postal.channel('detect.cylon').publish({name: 'William Adama'});
} );

View file

@ -17,6 +17,22 @@ var DEFAULT_CHANNEL = "/",
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +47,6 @@ var DistinctPredicate = function () {
return !eq;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

File diff suppressed because one or more lines are too long

View file

@ -17,6 +17,22 @@ var DEFAULT_CHANNEL = "/",
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +47,6 @@ var DistinctPredicate = function () {
return !eq;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

View file

@ -17,6 +17,22 @@ var DEFAULT_CHANNEL = "/",
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +47,6 @@ var DistinctPredicate = function () {
return !eq;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

View file

@ -50,5 +50,11 @@
Example 8 - using withDelay to delay evaluation of subscription
<ul class="results" id="example8"></ul>
</div>
<div>
Example 9 - using distinct to ignore any duplicate messages
<ul class="results" id="example9"></ul>
</div>
</body>
</html>

View file

@ -126,4 +126,19 @@ $( function () {
postal.channel( "He.Will.Knock.Four.Times" )
.publish( { value : "Knock!" } );
wdSubscription.unsubscribe();
// Using distinct() to ignore duplicate messages
var revealChannel = postal.channel('detect.cylon'),
revealSubscription = revealChannel.subscribe(function (who) {
$('<li></li>').text(who.name).appendTo($('#example9'));
}).distinct();
postal.channel('detect.cylon').publish({name: 'Boomer'});
postal.channel('detect.cylon').publish({name: 'Saul Tigh'});
postal.channel('detect.cylon').publish({name: 'William Adama'});
postal.channel('detect.cylon').publish({name: 'Helo'});
//ignored!
postal.channel('detect.cylon').publish({name: 'Boomer'});
postal.channel('detect.cylon').publish({name: 'Felix Gaeta'});
//ignored!
postal.channel('detect.cylon').publish({name: 'William Adama'});
} );

View file

@ -16,7 +16,7 @@ var DEFAULT_CHANNEL = "/",
NO_OP = function () {
};
var DistinctPredicate = function () {
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +31,22 @@ var DistinctPredicate = function () {
return !eq;
};
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

File diff suppressed because one or more lines are too long

View file

@ -17,6 +17,22 @@ var DEFAULT_CHANNEL = "/",
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +47,6 @@ var DistinctPredicate = function () {
return !eq;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

File diff suppressed because one or more lines are too long

View file

@ -17,6 +17,22 @@ var DEFAULT_CHANNEL = "/",
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +47,6 @@ var DistinctPredicate = function () {
return !eq;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

View file

@ -16,7 +16,7 @@ var DEFAULT_CHANNEL = "/",
NO_OP = function () {
};
var DistinctPredicate = function () {
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
@ -31,7 +31,22 @@ var DistinctPredicate = function () {
return !eq;
};
};
var DistinctPredicate = function () {
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};
var ChannelDefinition = function ( channelName, defaultTopic ) {
this.channel = channelName || DEFAULT_CHANNEL;
this._topic = defaultTopic || "";
@ -138,6 +153,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,76 @@
QUnit.specify( "postal.js", function () {
describe( "ConsecutiveDistinctPredicate", function () {
describe( "When calling the function with the same data multiple times", function () {
var pred = new ConsecutiveDistinctPredicate(),
data = { name : "Dr Who" },
results = [];
results.push( pred( data ) );
results.push( pred( data ) );
results.push( pred( data ) );
it( "The first result should be true", function () {
assert( results[0] ).isTrue();
} );
it( "The second result should be false", function () {
assert( results[1] ).isFalse();
} );
it( "The third result should be false", function () {
assert( results[2] ).isFalse();
} );
} );
describe( "When calling the function with different data every time", function () {
var predA = new ConsecutiveDistinctPredicate(),
data = { name : "Amelia" },
res = [];
res.push( predA( data ) );
data.name = "Rose";
res.push( predA( data ) );
data.name = "Martha";
res.push( predA( data ) );
it( "The first result should be true", function () {
assert( res[0] ).isTrue();
} );
it( "The second result should be true", function () {
assert( res[1] ).isTrue();
} );
it( "The third result should be true", function () {
assert( res[2] ).isTrue();
} );
} );
describe( "When calling the function with different data every two calls", function () {
var predA = new ConsecutiveDistinctPredicate(),
data = { name : "Amelia" },
res = [];
res.push( predA( data ) );
res.push( predA( data ) );
data.name = "Rose";
res.push( predA( data ) );
res.push( predA( data ) );
data.name = "Martha";
res.push( predA( data ) );
res.push( predA( data ) );
it( "The first result should be true", function () {
assert( res[0] ).isTrue();
} );
it( "The second result should be false", function () {
assert( res[1] ).isFalse();
} );
it( "The third result should be isTrue", function () {
assert( res[2] ).isTrue();
} );
it( "The fourth result should be false", function () {
assert( res[3] ).isFalse();
} );
it( "The fifth result should be true", function () {
assert( res[4] ).isTrue();
} );
it( "The sixth result should be false", function () {
assert( res[5] ).isFalse();
} );
} );
} );
} );

View file

@ -1,76 +1,198 @@
QUnit.specify( "postal.js", function () {
describe( "DistinctPredicate", function () {
describe( "When calling the function with the same data multiple times", function () {
var pred = new DistinctPredicate(),
data = { name : "Dr Who" },
results = [];
results.push( pred( data ) );
results.push( pred( data ) );
results.push( pred( data ) );
QUnit.specify('postal.js', function () {
describe('DistinctPredicate', function () {
it( "The first result should be true", function () {
assert( results[0] ).isTrue();
} );
it( "The second result should be false", function () {
assert( results[1] ).isFalse();
} );
it( "The third result should be false", function () {
assert( results[2] ).isFalse();
} );
} );
describe( "When calling the function with different data every time", function () {
var predA = new DistinctPredicate(),
data = { name : "Amelia" },
res = [];
res.push( predA( data ) );
data.name = "Rose";
res.push( predA( data ) );
data.name = "Martha";
res.push( predA( data ) );
describe('When calling the function with the same data object multiple times', function () {
var pred = new DistinctPredicate(),
results = [];
it( "The first result should be true", function () {
assert( res[0] ).isTrue();
} );
it( "The second result should be true", function () {
assert( res[1] ).isTrue();
} );
it( "The third result should be true", function () {
assert( res[2] ).isTrue();
} );
} );
describe( "When calling the function with different data every two calls", function () {
var predA = new DistinctPredicate(),
data = { name : "Amelia" },
res = [];
res.push( predA( data ) );
res.push( predA( data ) );
data.name = "Rose";
res.push( predA( data ) );
res.push( predA( data ) );
data.name = "Martha";
res.push( predA( data ) );
res.push( predA( data ) );
results.push(pred({career: 'ninja'}));
results.push(pred({career: 'ninja'}));
results.push(pred({career: 'ninja'}));
it( "The first result should be true", function () {
assert( res[0] ).isTrue();
} );
it( "The second result should be false", function () {
assert( res[1] ).isFalse();
} );
it('the first result should be true', function () {
assert(results[0]).isTrue();
});
it('the second result should be false', function () {
assert(results[1]).isFalse();
});
it('the third result should be false', function () {
assert(results[2]).isFalse();
});
});
it( "The third result should be isTrue", function () {
assert( res[2] ).isTrue();
} );
it( "The fourth result should be false", function () {
assert( res[3] ).isFalse();
} );
describe('When calling the function with the same primitive multiple times', function () {
var pred = new DistinctPredicate(),
results = [];
it( "The fifth result should be true", function () {
assert( res[4] ).isTrue();
} );
it( "The sixth result should be false", function () {
assert( res[5] ).isFalse();
} );
} );
} );
} );
results.push(pred('ninja'));
results.push(pred('ninja'));
results.push(pred('ninja'));
it('the first result should be true', function () {
assert(results[0]).isTrue();
});
it('the second result should be false', function () {
assert(results[1]).isFalse();
});
it('the third result should be false', function () {
assert(results[2]).isFalse();
});
});
describe('When calling the function with the same array multiple times', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred(['Jack Black', 'Kyle Gass']));
results.push(pred(['Jack Black', 'Kyle Gass']));
results.push(pred(['Jack Black', 'Kyle Gass']));
it('the first result should be true', function () {
assert(results[0]).isTrue();
});
it('the second result should be false', function () {
assert(results[1]).isFalse();
});
it('the third result should be false', function () {
assert(results[2]).isFalse();
});
});
// ------------------------------------------
describe('When calling the function with different data object every time', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred({codename: 'tinker'}));
results.push(pred({codename: 'tailor'}));
results.push(pred({codename: 'soldier'}));
results.push(pred({codename: 'spy'}));
it('all results should be true', function () {
var i = 0, length = results.length;
for (i; i < length; i += 1) {
assert(results[i]).isTrue();
}
});
});
describe('When calling the function with different primitive every time', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred(100.5));
results.push(pred(12));
results.push(pred(40.32));
results.push(pred(0));
it('all results should be true', function () {
var i = 0, length = results.length;
for (i; i < length; i += 1) {
assert(results[i]).isTrue();
}
});
});
describe('When calling the function with different array every time', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred([]));
results.push(pred(['chrome', 'firefox', 'ie', 'opera']));
results.push(pred(['windows', 'linux', 'osx']));
results.push(pred(['Leonardo', 'Raphael', 'Donatello', 'Michelangelo']));
it('all results should be true', function () {
var i = 0, length = results.length;
for (i; i < length; i += 1) {
assert(results[i]).isTrue();
}
});
});
// ------------------------------------------
describe('When calling the function with different data object between duplicates', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred({game: 'Diablo 3'}));
results.push(pred({game: 'Bioshock'}));
results.push(pred({game: 'Batman: Arkham City'}));
results.push(pred({game: 'Diablo 3'}));
results.push(pred({game: 'Team Fortress 2'}));
it('the first result should be true', function () {
assert(results[0]).isTrue();
});
it('the second result should be true', function () {
assert(results[1]).isTrue();
});
it('the third result should be true', function () {
assert(results[2]).isTrue();
});
it('the fourth result should be false', function () {
assert(results[3]).isFalse();
});
it('the fifth result should be true', function () {
assert(results[4]).isTrue();
});
});
describe('When calling the function with different primitive between duplicates', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred('Stan Marsh'));
results.push(pred('Kyle Broflovski'));
results.push(pred('Eric Cartman'));
results.push(pred('Stan Marsh'));
results.push(pred('Kenny McCormick'));
it('the first result should be true', function () {
assert(results[0]).isTrue();
});
it('the second result should be true', function () {
assert(results[1]).isTrue();
});
it('the third result should be true', function () {
assert(results[2]).isTrue();
});
it('the fourth result should be false', function () {
assert(results[3]).isFalse();
});
it('the fifth result should be true', function () {
assert(results[4]).isTrue();
});
});
describe('When calling the function with different array between duplicates', function () {
var pred = new DistinctPredicate(),
results = [];
results.push(pred([]));
results.push(pred(['Hannibal', 'Face', 'Murdock', 'Mr. T']));
results.push(pred([4, 8, 15, 16, 23, 42]));
results.push(pred([]));
results.push(pred([{x: 10, y: 20}, {x: 50, y: 100}]));
it('the first result should be true', function () {
assert(results[0]).isTrue();
});
it('the second result should be true', function () {
assert(results[1]).isTrue();
});
it('the third result should be true', function () {
assert(results[2]).isTrue();
});
it('the fourth result should be false', function () {
assert(results[3]).isFalse();
});
it('the fifth result should be true', function () {
assert(results[4]).isTrue();
});
});
});
});

View file

@ -9,12 +9,14 @@
<script type="text/javascript" src="../ext/underscore.js"></script>
<script type="text/javascript" src="../src/main/Constants.js"></script>
<script type="text/javascript" src="../src/main/DistinctPredicate.js"></script>
<script type="text/javascript" src="../src/main/ConsecutiveDistinctPredicate.js"></script>
<script type="text/javascript" src="../src/main/ChannelDefinition.js"></script>
<script type="text/javascript" src="../src/main/SubscriptionDefinition.js"></script>
<script type="text/javascript" src="../src/main/BindingsResolver.js"></script>
<script type="text/javascript" src="../src/main/LocalBus.js"></script>
<script type="text/javascript" src="../src/main/Api.js"></script>
<script type="text/javascript" src="DistinctPredicate.spec.js"></script>
<script type="text/javascript" src="ConsecutiveDistinctPredicate.spec.js"></script>
<script type="text/javascript" src="ChannelDefinition.spec.js"></script>
<script type="text/javascript" src="SubscriptionDefinition.spec.js"></script>
<script type="text/javascript" src="BindingsResolver.spec.js"></script>

View file

@ -0,0 +1,15 @@
var ConsecutiveDistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
if ( _.isString( data ) ) {
eq = data === previous;
previous = data;
}
else {
eq = _.isEqual( data, previous );
previous = _.clone( data );
}
return !eq;
};
};

View file

@ -1,15 +1,16 @@
var DistinctPredicate = function () {
var previous;
return function ( data ) {
var eq = false;
if ( _.isString( data ) ) {
eq = data === previous;
previous = data;
}
else {
eq = _.isEqual( data, previous );
previous = _.clone( data );
}
return !eq;
};
};
var previous = [];
return function (data) {
var isDistinct = !_.any(previous, function (p) {
if (_.isObject(data) || _.isArray(data)) {
return _.isEqual(data, p);
}
return data === p;
});
if (isDistinct) {
previous.push(data);
}
return isDistinct;
};
};

View file

@ -63,6 +63,11 @@ SubscriptionDefinition.prototype = {
},
ignoreDuplicates : function () {
this.withConstraint( new ConsecutiveDistinctPredicate() );
return this;
},
distinct : function () {
this.withConstraint( new DistinctPredicate() );
return this;
},

View file

@ -5,6 +5,7 @@ define( ["underscore"], function ( _, undefined ) {
//import("Constants.js");
//import("DistinctPredicate.js");
//import("ConsecutiveDistinctPredicate.js");
//import("ChannelDefinition.js");
//import("SubscriptionDefinition.js");
//import("BindingsResolver.js");

View file

@ -5,6 +5,7 @@ var _ = require( 'underscore' );
//import("Constants.js");
//import("DistinctPredicate.js");
//import("ConsecutiveDistinctPredicate.js");
//import("ChannelDefinition.js");
//import("SubscriptionDefinition.js");
//import("BindingsResolver.js");

View file

@ -4,6 +4,7 @@
(function ( _, global, undefined ) {
//import("Constants.js");
//import("ConsecutiveDistinctPredicate.js");
//import("DistinctPredicate.js");
//import("ChannelDefinition.js");
//import("SubscriptionDefinition.js");