mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-04-17 05:20:57 +00:00
Added cache to bindingsResolver and tests to cover it
This commit is contained in:
parent
1390114766
commit
a5d91d32f4
5 changed files with 90 additions and 13 deletions
|
|
@ -148,8 +148,18 @@ 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);
|
||||
if(this.cache[topic] && this.cache[topic][binding]) {
|
||||
return true;
|
||||
}
|
||||
var rgx = new RegExp("^" + this.regexify(binding) + "$"), // match from start to end of string
|
||||
result = rgx.test(topic);
|
||||
if(result) {
|
||||
if(!this.cache[topic]) {
|
||||
this.cache[topic] = {};
|
||||
}
|
||||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
regexify: function(binding) {
|
||||
|
|
|
|||
|
|
@ -146,8 +146,18 @@ 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);
|
||||
if(this.cache[topic] && this.cache[topic][binding]) {
|
||||
return true;
|
||||
}
|
||||
var rgx = new RegExp("^" + this.regexify(binding) + "$"), // match from start to end of string
|
||||
result = rgx.test(topic);
|
||||
if(result) {
|
||||
if(!this.cache[topic]) {
|
||||
this.cache[topic] = {};
|
||||
}
|
||||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
regexify: function(binding) {
|
||||
|
|
|
|||
|
|
@ -44,5 +44,47 @@ QUnit.specify("postal.js", function(){
|
|||
});
|
||||
});
|
||||
});
|
||||
describe("When calling compare", function(){
|
||||
describe("With topic Top.Middle.Bottom and binding Top.Middle.Bottom", function(){
|
||||
var result = bindingsResolver.compare("Top.Middle.Bottom", "Top.Middle.Bottom"),
|
||||
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.Bottom"];
|
||||
it("Result should be true", function() {
|
||||
assert(result).isTrue();
|
||||
});
|
||||
it("Should create a resolver cache entry", function(){
|
||||
assert(cached).isTrue();
|
||||
});
|
||||
});
|
||||
describe("With topic Top.Middle.Bottom and binding Top.#.Bottom", function(){
|
||||
var result = bindingsResolver.compare("Top.#.Bottom", "Top.Middle.Bottom"),
|
||||
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.#.Bottom"];
|
||||
it("Result should be true", function() {
|
||||
assert(result).isTrue();
|
||||
});
|
||||
it("Should create a resolver cache entry", function(){
|
||||
assert(cached).isTrue();
|
||||
});
|
||||
});
|
||||
describe("With topic Top.Middle.Bottom and binding Top.*.Bottom", function(){
|
||||
var result = bindingsResolver.compare("Top.*.Bottom", "Top.Middle.Bottom"),
|
||||
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.*.Bottom"];
|
||||
it("Result should be true", function() {
|
||||
assert(result).isTrue();
|
||||
});
|
||||
it("Should create a resolver cache entry", function(){
|
||||
assert(cached).isTrue();
|
||||
});
|
||||
});
|
||||
describe("With topic Top.Middle.Bottom and binding #.*.Bottom", function(){
|
||||
var result = bindingsResolver.compare("#.*.Bottom", "Top.Middle.Bottom"),
|
||||
cached = bindingsResolver.cache["Top.Middle.Bottom"]["#.*.Bottom"];
|
||||
it("Result should be true", function() {
|
||||
assert(result).isTrue();
|
||||
});
|
||||
it("Should create a resolver cache entry", function(){
|
||||
assert(cached).isTrue();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -51,9 +51,8 @@ QUnit.specify("postal.js", function(){
|
|||
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();
|
||||
assert(_.any(chDefd.configuration.modifiers, function(item) { return item.type === "defer";})).isTrue();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -117,16 +116,20 @@ QUnit.specify("postal.js", function(){
|
|||
chDefdb.withDebounce(1000);
|
||||
|
||||
it("Should set debounce", function() {
|
||||
assert(chDefdb.configuration.debounce).equals(1000);
|
||||
assert(_.any(chDefdb.configuration.modifiers, function(item) {
|
||||
return item.type === "debounce" && item.milliseconds === 1000;
|
||||
})).isTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe("When setting delay", function(){
|
||||
var chDefdb = new ChannelDefinition();
|
||||
chDefdb.withDelay(1000);
|
||||
var chDefdl = new ChannelDefinition();
|
||||
chDefdl.withDelay(1000);
|
||||
|
||||
it("Should set delay", function() {
|
||||
assert(chDefdb.configuration.delay).equals(1000);
|
||||
assert(_.any(chDefdl.configuration.modifiers, function(item) {
|
||||
return item.type === "delay" && item.milliseconds === 1000;
|
||||
})).isTrue();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -144,7 +147,9 @@ QUnit.specify("postal.js", function(){
|
|||
chDefth.withThrottle(1000);
|
||||
|
||||
it("Should set throttle", function() {
|
||||
assert(chDefth.configuration.throttle).equals(1000);
|
||||
assert(_.any(chDefth.configuration.modifiers, function(item) {
|
||||
return item.type === "throttle" && item.milliseconds === 1000;
|
||||
})).isTrue();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,8 +2,18 @@ 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);
|
||||
if(this.cache[topic] && this.cache[topic][binding]) {
|
||||
return true;
|
||||
}
|
||||
var rgx = new RegExp("^" + this.regexify(binding) + "$"), // match from start to end of string
|
||||
result = rgx.test(topic);
|
||||
if(result) {
|
||||
if(!this.cache[topic]) {
|
||||
this.cache[topic] = {};
|
||||
}
|
||||
this.cache[topic][binding] = true;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
regexify: function(binding) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue