Moving new code base into separate files, creating tests, archiving old code base until refactor is complete.

This commit is contained in:
Jim Cowart 2011-09-09 01:23:07 -04:00
parent 7c8c5d7754
commit 18ee34b4f8
31 changed files with 578 additions and 273 deletions

View file

@ -0,0 +1,55 @@
QUnit.specify("postal.js", function(){
describe("bindingsResolver", function(){
describe("When calling regexify", function() {
describe("With a topic containing no special escape chars", function() {
var result = bindingsResolver.regexify("CoolTopic");
console.log(result);
it("Should equal 'CoolTopic'", function(){
assert(result).equals("CoolTopic");
});
});
describe("With a topic containing periods", function() {
var result = bindingsResolver.regexify("Top.Middle.Bottom");
console.log(result);
it("Only the periods should be escaped", function(){
assert(result).equals("Top\\.Middle\\.Bottom");
});
});
describe("With a topic containing a hash", function() {
var result = bindingsResolver.regexify("Top#Bottom");
console.log(result);
it("Only the hash should be escaped", function(){
assert(result).equals("Top[A-Z,a-z,0-9]*Bottom");
});
});
describe("With a topic containing a hash and periods", function() {
var result = bindingsResolver.regexify("Top.#.Bottom");
console.log(result);
it("The hash should be escaped for alphanumeric regex", function(){
assert(result).equals("Top\\.[A-Z,a-z,0-9]*\\.Bottom");
});
});
describe("With a topic containing a hash and asterisk", function() {
var result = bindingsResolver.regexify("Top#Bottom*");
console.log(result);
it("The hash should be escaped for alphanumeric regex", function(){
assert(result).equals("Top[A-Z,a-z,0-9]*Bottom.*");
});
});
describe("With a topic containing a hash, asterisk and periods", function() {
var result = bindingsResolver.regexify("Top.#.Bottom.*");
console.log(result);
it("The hash should be escaped for alphanumeric regex", function(){
assert(result).equals("Top\\.[A-Z,a-z,0-9]*\\.Bottom\\..*");
});
});
describe("With a topic containing an asterisk and periods", function() {
var result = bindingsResolver.regexify("Top.*.Bottom");
console.log(result);
it("The asterisk should be escaped", function(){
assert(result).equals("Top\\..*\\.Bottom");
});
});
});
});
});

View file

@ -0,0 +1,151 @@
QUnit.specify("postal.js", function(){
describe("ChannelDefinition", function(){
describe("When initializing a basic channel definition", function() {
var chDef = new ChannelDefinition();
it("Should default the exchange", function() {
assert(chDef.configuration.exchange).equals(DEFAULT_EXCHANGE);
});
it("Should default the topic", function() {
assert(chDef.configuration.topic).equals("");
});
it("Should default the callback", function() {
assert(chDef.configuration.callback).equals(NO_OP);
});
it("Should default the priority", function() {
assert(chDef.configuration.priority).equals(DEFAULT_PRIORITY);
});
it("Should default the constraints", function() {
assert(chDef.configuration.constraints.length).equals(0);
});
it("Should default the disposeAfter", function() {
assert(chDef.configuration.disposeAfter).equals(DEFAULT_DISPOSEAFTER);
});
it("Should default the onHandled", function() {
assert(chDef.configuration.onHandled).equals(NO_OP);
});
it("Should default the onHandled", function() {
assert(chDef.configuration.context).isNull();
});
});
describe("When setting the exchange through fluent configuration", function(){
var chDefe = new ChannelDefinition();
chDefe.exchange("TestExchange");
it("Should set the exchange", function() {
assert(chDefe.configuration.exchange).equals("TestExchange");
});
});
describe("When setting the topic through fluent configuration", function(){
var chDeft = new ChannelDefinition();
chDeft.topic("TestTopic");
it("Should set the topic", function() {
assert(chDeft.configuration.topic).equals("TestTopic");
});
});
describe("When setting the definition to defer", function(){
var chDefd = new ChannelDefinition();
chDefd.defer();
it("Should set defer to true", function() {
assert(chDefd.configuration.defer).isTrue();
});
});
describe("When setting the definition to disposeAfter 2 invocations", function(){
var chDefda = new ChannelDefinition();
chDefda.disposeAfter(2);
it("Should set disposeAfter to 2", function() {
assert(chDefda.configuration.disposeAfter).equals(2);
});
});
describe("When setting ignoreDuplicates", function(){
var chDefid = new ChannelDefinition();
chDefid.ignoreDuplicates();
it("Should add a DistinctPredicate constraint to the configuration constraints", function() {
assert(chDefid.configuration.constraints.length).equals(1);
});
});
describe("When setting whenHandledThenExecute", function(){
var chDefwhte = new ChannelDefinition();
chDefwhte.whenHandledThenExecute(function() { });
it("Should add an onHandled callback", function() {
assert(typeof chDefwhte.configuration.onHandled).equals("function");
});
});
describe("When adding a constraint", function(){
var chDefc = new ChannelDefinition();
chDefc.withConstraint(function() { });
it("Should add a constraint", function() {
assert(chDefc.configuration.constraints.length).equals(1);
});
});
describe("When adding multiple constraints", function(){
var chDefcs = new ChannelDefinition();
chDefcs.withConstraints([function() { }, function() { }, function() { }]);
it("Should add a constraint", function() {
assert(chDefcs.configuration.constraints.length).equals(3);
});
});
describe("When setting the context", function(){
var chDefctx = new ChannelDefinition(),
obj = {};
chDefctx.withContext(obj);
it("Should set context", function() {
assert(chDefctx.configuration.context).equals(obj);
});
});
describe("When setting debounce", function(){
var chDefdb = new ChannelDefinition();
chDefdb.withDebounce(1000);
it("Should set debounce", function() {
assert(chDefdb.configuration.debounce).equals(1000);
});
});
describe("When setting delay", function(){
var chDefdb = new ChannelDefinition();
chDefdb.withDelay(1000);
it("Should set delay", function() {
assert(chDefdb.configuration.delay).equals(1000);
});
});
describe("When setting priority", function(){
var chDefp = new ChannelDefinition();
chDefp.withPriority(10);
it("Should set priority", function() {
assert(chDefp.configuration.priority).equals(10);
});
});
describe("When setting throttle", function(){
var chDefth = new ChannelDefinition();
chDefth.withThrottle(1000);
it("Should set throttle", function() {
assert(chDefth.configuration.throttle).equals(1000);
});
});
});
});

View file

@ -0,0 +1,76 @@
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));
it("The first result should be false", function(){
assert(results[0]).isFalse();
});
it("The second result should be true", function(){
assert(results[1]).isTrue();
});
it("The third result should be true", function(){
assert(results[2]).isTrue();
});
});
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));
it("The first result should be false", function(){
assert(res[0]).isFalse();
});
it("The second result should be false", function(){
assert(res[1]).isFalse();
});
it("The third result should be false", function(){
assert(res[2]).isFalse();
});
});
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));
it("The first result should be false", function(){
assert(res[0]).isFalse();
});
it("The second result should be true", function(){
assert(res[1]).isTrue();
});
it("The third result should be false", function(){
assert(res[2]).isFalse();
});
it("The fourth result should be true", function(){
assert(res[3]).isTrue();
});
it("The fifth result should be false", function(){
assert(res[4]).isFalse();
});
it("The sixth result should be true", function(){
assert(res[5]).isTrue();
});
});
});
});

24
spec/Old/postal.core.html Normal file
View file

@ -0,0 +1,24 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../../lib/qunit.js"></script>
<script type="text/javascript" src="../../lib/pavlov.js"></script>
<script type="text/javascript" src="../../lib/amplify.core.js"></script>
<script type="text/javascript" src="../../lib/amplify.store.js"></script>
<script type="text/javascript" src="../../lib/underscore.js"></script>
<script type="text/javascript" src="../../src/old/misc.js"></script>
<script type="text/javascript" src="../../src/old/Bus.js"></script>
<script type="text/javascript" src="../../src/old/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -0,0 +1,27 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../../lib/qunit.js"></script>
<script type="text/javascript" src="../../lib/pavlov.js"></script>
<script type="text/javascript" src="../../lib/amplify.core.js"></script>
<script type="text/javascript" src="../../lib/amplify.store.js"></script>
<script type="text/javascript" src="../../lib/underscore.js"></script>
<script type="text/javascript" src="../../src/old/misc.js"></script>
<script type="text/javascript" src="../../src/old/Bus.js"></script>
<script type="text/javascript" src="../../src/old/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="../../src/old/capture/MessageCaptor.js"></script>
<script type="text/javascript" src="../../src/old/Diagnostics.js"></script>
<script type="text/javascript" src="broker.withCapture.spec.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -0,0 +1,28 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../../lib/qunit.js"></script>
<script type="text/javascript" src="../../lib/pavlov.js"></script>
<script type="text/javascript" src="../../lib/amplify.core.js"></script>
<script type="text/javascript" src="../../lib/amplify.store.js"></script>
<script type="text/javascript" src="../../lib/underscore.js"></script>
<script type="text/javascript" src="../../src/old/misc.js"></script>
<script type="text/javascript" src="../../src/old/Bus.js"></script>
<script type="text/javascript" src="../../src/old/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="../../src/old/replay/ReplayContext.js"></script>
<script type="text/javascript" src="../../src/old/capture/MessageCaptor.js"></script>
<script type="text/javascript" src="broker.withCapture.spec.js"></script>
<script type="text/javascript" src="broker.withReplay.spec.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -0,0 +1,26 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../../lib/qunit.js"></script>
<script type="text/javascript" src="../../lib/pavlov.js"></script>
<script type="text/javascript" src="../../lib/amplify.core.js"></script>
<script type="text/javascript" src="../../lib/amplify.store.js"></script>
<script type="text/javascript" src="../../lib/underscore.js"></script>
<script type="text/javascript" src="../../src/old/misc.js"></script>
<script type="text/javascript" src="../../src/old/Bus.js"></script>
<script type="text/javascript" src="../../src/old/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="../../src/old/replay/ReplayContext.js"></script>
<script type="text/javascript" src="broker.withReplay.spec.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

19
spec/Old/runner.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var core = window.open("postal.core.html","core");
core.focus();
var wCapture = window.open("postal.withCapture.html","wCapture");
wCapture.focus();
var wReplay = window.open("postal.withReplay.html","wReplay");
wReplay.focus();
var wCaptureReplay = window.open("postal.withCapture_Replay.html","wCaptureReplay");
wCaptureReplay.focus();
</script>
</head>
<body>
</body>
</html>

View file

@ -1,24 +0,0 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../lib/qunit.js"></script>
<script type="text/javascript" src="../lib/pavlov.js"></script>
<script type="text/javascript" src="../lib/amplify.core.js"></script>
<script type="text/javascript" src="../lib/amplify.store.js"></script>
<script type="text/javascript" src="../lib/underscore.js"></script>
<script type="text/javascript" src="../src/misc.js"></script>
<script type="text/javascript" src="../src/Bus.js"></script>
<script type="text/javascript" src="../src/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,27 +0,0 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../lib/qunit.js"></script>
<script type="text/javascript" src="../lib/pavlov.js"></script>
<script type="text/javascript" src="../lib/amplify.core.js"></script>
<script type="text/javascript" src="../lib/amplify.store.js"></script>
<script type="text/javascript" src="../lib/underscore.js"></script>
<script type="text/javascript" src="../src/misc.js"></script>
<script type="text/javascript" src="../src/Bus.js"></script>
<script type="text/javascript" src="../src/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="../src/capture/MessageCaptor.js"></script>
<script type="text/javascript" src="../src/Diagnostics.js"></script>
<script type="text/javascript" src="broker.withCapture.spec.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,28 +0,0 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../lib/qunit.js"></script>
<script type="text/javascript" src="../lib/pavlov.js"></script>
<script type="text/javascript" src="../lib/amplify.core.js"></script>
<script type="text/javascript" src="../lib/amplify.store.js"></script>
<script type="text/javascript" src="../lib/underscore.js"></script>
<script type="text/javascript" src="../src/misc.js"></script>
<script type="text/javascript" src="../src/Bus.js"></script>
<script type="text/javascript" src="../src/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="../src/replay/ReplayContext.js"></script>
<script type="text/javascript" src="../src/capture/MessageCaptor.js"></script>
<script type="text/javascript" src="broker.withCapture.spec.js"></script>
<script type="text/javascript" src="broker.withReplay.spec.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,26 +0,0 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../lib/qunit.js"></script>
<script type="text/javascript" src="../lib/pavlov.js"></script>
<script type="text/javascript" src="../lib/amplify.core.js"></script>
<script type="text/javascript" src="../lib/amplify.store.js"></script>
<script type="text/javascript" src="../lib/underscore.js"></script>
<script type="text/javascript" src="../src/misc.js"></script>
<script type="text/javascript" src="../src/Bus.js"></script>
<script type="text/javascript" src="../src/Postal.js"></script>
<script type="text/javascript" src="testSetup.js"></script>
<script type="text/javascript" src="../src/replay/ReplayContext.js"></script>
<script type="text/javascript" src="broker.withReplay.spec.js"></script>
<script type="text/javascript" src="broker.spec.js"></script>
<link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

View file

@ -1,19 +1,25 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
var core = window.open("postal.core.html","core");
core.focus();
var wCapture = window.open("postal.withCapture.html","wCapture");
wCapture.focus();
var wReplay = window.open("postal.withReplay.html","wReplay");
wReplay.focus();
var wCaptureReplay = window.open("postal.withCapture_Replay.html","wCaptureReplay");
wCaptureReplay.focus();
</script>
<script type="text/javascript" src="../lib/jquery-1.5.2.js"></script>
<script type="text/javascript" src="../lib/qunit.js"></script>
<script type="text/javascript" src="../lib/pavlov.js"></script>
<script type="text/javascript" src="../lib/amplify.core.js"></script>
<script type="text/javascript" src="../lib/amplify.store.js"></script>
<script type="text/javascript" src="../lib/underscore.js"></script>
<script type="text/javascript" src="../src/BindingsResolver.js"></script>
<script type="text/javascript" src="../src/DistinctPredicate.js"></script>
<script type="text/javascript" src="../src/ChannelDefinition.js"></script>
<script type="text/javascript" src="BindingsResolver.spec.js"></script>
<script type="text/javascript" src="DistinctPredicate.spec.js"></script>
<script type="text/javascript" src="ChannelDefinition.spec.js"></script>
<link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
</body>
</html>

14
src/BindingsResolver.js Normal file
View file

@ -0,0 +1,14 @@
var bindingsResolver = {
cache: { },
compare: function(binding, topic) {
var rgx = new RegExp("^" + this.regexify(binding) + "$"); // match from start to end of string
return rgx.test(topic);
},
regexify: function(binding) {
return binding.replace(/\./g,"\\.") // escape actual periods
.replace(/\*/g, ".*") // asterisks match any value
.replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word'
}
};

View file

@ -1,27 +1,10 @@
var DEFAULT_EXCHANGE = "/",
DEFAULT_PRIORITY = 50,
DEFAULT_DISPOSEAFTER = 0,
NO_OP = function() { },
bus;
var bindingsResolver = {
cache: { },
compare: function(binding, topic) {
var rgx = new RegExp("^" + this.regexify(binding) + "$"); // match from start to end of string
return rgx.test(topic);
},
regexify: function(binding) {
return binding.replace(/\./g,"\\.") // escape actual periods
.replace(/\*/g, ".*") // asterisks match any value
.replace(/#/g, "[A-Z,a-z,0-9]*"); // hash matches any alpha-numeric 'word'
}
};
var bus;
var localBus = {
subscriptions: {},
wireTaps: [],
publish: function(envelope) {
_.each(this.wireTaps,function(tap) {
tap({
@ -101,7 +84,13 @@ var localBus = {
},
addWireTap: function(callback) {
console.log("WIRETAP: " + JSON.stringify(callback));
this.wireTaps.push(callback);
return function() {
var idx = this.wireTaps.indexOf(callback);
if(idx !== -1) {
this.wireTaps.splice(idx,1);
}
};
}
};
@ -140,138 +129,4 @@ var postal = {
}
};
postal.config.setBusBehavior(localBus);
var ChannelDefinition = function(exchange, topic) {
this.configuration = {
exchange: exchange || DEFAULT_EXCHANGE,
topic: topic || "",
callback: NO_OP,
priority: DEFAULT_PRIORITY,
constraints: [],
disposeAfter: DEFAULT_DISPOSEAFTER,
onHandled: NO_OP,
context: null
};
} ;
ChannelDefinition.prototype = {
exchange: function(exchange) {
this.configuration.exchange = exchange;
return this;
},
topic: function(topic) {
this.configuration.topic = topic;
return this;
},
defer: function() {
this.configuration.defer = true;
return this;
},
disposeAfter: function(receiveCount) {
if(_.isNaN(receiveCount)) {
throw "The value provided to disposeAfter (receiveCount) must be a number";
}
this.configuration.disposeAfter = receiveCount;
return this;
},
ignoreDuplicates: function() {
this.withConstraint(new DistinctPredicate());
return this;
},
whenHandledThenExecute: function(callback) {
if(! _.isFunction(callback)) {
throw "Value provided to 'whenHandledThenExecute' must be a function";
}
this.configuration.onHandled = callback;
return this;
},
withConstraint: function(predicate) {
if(! _.isFunction(predicate)) {
throw "Predicate constraint must be a function";
}
this.configuration.constraints.push(predicate);
return this;
},
withConstraints: function(predicates) {
if(_.isArray(predicates)) {
_.each(predicates, function(predicate) { this.withConstraint(predicate); } );
}
return this;
},
withContext: function(context) {
this.configuration.context = context;
return this;
},
withDebounce: function(milliseconds) {
if(_.isNaN(milliseconds)) {
throw "Milliseconds must be a number";
}
this.configuration.debounce = milliseconds;
return this;
},
withDelay: function(milliseconds) {
if(_.isNaN(milliseconds)) {
throw "Milliseconds must be a number";
}
this.configuration.delay = milliseconds;
return this;
},
withPriority: function(priority) {
if(_.isNaN(priority)) {
throw "Priority must be a number";
}
this.configuration.priority = priority;
return this;
},
withThrottle: function(milliseconds) {
if(_.isNaN(milliseconds)) {
throw "Milliseconds must be a number";
}
this.configuration.throttle = milliseconds;
return this;
},
subscribe: function(callback) {
this.configuration.callback = callback || NO_OP;
return postal.subscribe(this.configuration);
},
publish: function(data) {
postal.publish({
exchange: this.configuration.exchange,
data: data,
topic: this.configuration.topic
});
}
};
var DistinctPredicate = function() {
this.previous = undefined;
return function(data) {
var result = _.isEqual(data, this.previous);
this.previous = data;
return result;
}
};
/*
postal.exchange("myExchange")
.topic("myTopic.mySubTopic")
.ignoreDuplicates() // is this the best name?
.withConstraint( function(data) { return data.firstName === "Jim"; })
.subscribe(function() { });
*/
postal.config.setBusBehavior(localBus);

121
src/ChannelDefinition.js Normal file
View file

@ -0,0 +1,121 @@
var DEFAULT_EXCHANGE = "/",
DEFAULT_PRIORITY = 50,
DEFAULT_DISPOSEAFTER = 0,
NO_OP = function() { };
var ChannelDefinition = function(exchange, topic) {
this.configuration = {
exchange: exchange || DEFAULT_EXCHANGE,
topic: topic || "",
callback: NO_OP,
priority: DEFAULT_PRIORITY,
constraints: [],
disposeAfter: DEFAULT_DISPOSEAFTER,
onHandled: NO_OP,
context: null
};
} ;
ChannelDefinition.prototype = {
exchange: function(exchange) {
this.configuration.exchange = exchange;
return this;
},
topic: function(topic) {
this.configuration.topic = topic;
return this;
},
defer: function() {
this.configuration.defer = true;
return this;
},
disposeAfter: function(receiveCount) {
if(_.isNaN(receiveCount)) {
throw "The value provided to disposeAfter (receiveCount) must be a number";
}
this.configuration.disposeAfter = receiveCount;
return this;
},
ignoreDuplicates: function() {
this.withConstraint(new DistinctPredicate());
return this;
},
whenHandledThenExecute: function(callback) {
if(! _.isFunction(callback)) {
throw "Value provided to 'whenHandledThenExecute' must be a function";
}
this.configuration.onHandled = callback;
return this;
},
withConstraint: function(predicate) {
if(! _.isFunction(predicate)) {
throw "Predicate constraint must be a function";
}
this.configuration.constraints.push(predicate);
return this;
},
withConstraints: function(predicates) {
var self = this;
if(_.isArray(predicates)) {
_.each(predicates, function(predicate) { self.withConstraint(predicate); } );
}
return self;
},
withContext: function(context) {
this.configuration.context = context;
return this;
},
withDebounce: function(milliseconds) {
if(_.isNaN(milliseconds)) {
throw "Milliseconds must be a number";
}
this.configuration.debounce = milliseconds;
return this;
},
withDelay: function(milliseconds) {
if(_.isNaN(milliseconds)) {
throw "Milliseconds must be a number";
}
this.configuration.delay = milliseconds;
return this;
},
withPriority: function(priority) {
if(_.isNaN(priority)) {
throw "Priority must be a number";
}
this.configuration.priority = priority;
return this;
},
withThrottle: function(milliseconds) {
if(_.isNaN(milliseconds)) {
throw "Milliseconds must be a number";
}
this.configuration.throttle = milliseconds;
return this;
},
subscribe: function(callback) {
this.configuration.callback = callback || NO_OP;
return postal.subscribe(this.configuration);
},
publish: function(data) {
postal.publish({
exchange: this.configuration.exchange,
data: data,
topic: this.configuration.topic
});
}
};

8
src/DistinctPredicate.js Normal file
View file

@ -0,0 +1,8 @@
var DistinctPredicate = function() {
var previous;
return function(data) {
var result = _.isEqual(data, previous);
previous = _.clone(data);
return result;
};
};