1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-16 06:44:50 +00:00

Minor refactoring

This commit is contained in:
Jeremy Ruston 2023-11-07 20:44:55 +00:00
parent 02f3065e4f
commit d7f0c5cb6b

View File

@ -225,13 +225,16 @@ Adds tiddler filtering methods to the $tw.Wiki object.
widget: an optional widget node for retrieving the current tiddler etc. widget: an optional widget node for retrieving the current tiddler etc.
*/ */
exports.compileFilter = function(filterString) { exports.compileFilter = function(filterString) {
// Set up the filter function cache
if(!this.filterCache) { if(!this.filterCache) {
this.filterCache = Object.create(null); this.filterCache = Object.create(null);
this.filterCacheCount = 0; this.filterCacheCount = 0;
} }
// Use the cached version of this filter function if it exists
if(this.filterCache[filterString] !== undefined) { if(this.filterCache[filterString] !== undefined) {
return this.filterCache[filterString]; return this.filterCache[filterString];
} }
// Parse the filter string
var filterParseTree; var filterParseTree;
try { try {
filterParseTree = this.parseFilter(filterString); filterParseTree = this.parseFilter(filterString);
@ -334,8 +337,8 @@ Adds tiddler filtering methods to the $tw.Wiki object.
} }
})()); })());
}); });
// Return a function that applies the operations to a source iterator of tiddler titles // Make the filter function
var fnMeasured = $tw.perf.measure("filter: " + filterString,function filterFunction(source,widget) { var fnFilter = function filterFunction(source,widget) {
if(!source) { if(!source) {
source = self.each; source = self.each;
} else if(typeof source === "object") { // Array or hashmap } else if(typeof source === "object") { // Array or hashmap
@ -355,7 +358,10 @@ Adds tiddler filtering methods to the $tw.Wiki object.
} }
self.filterRecursionCount = self.filterRecursionCount - 1; self.filterRecursionCount = self.filterRecursionCount - 1;
return results.toArray(); return results.toArray();
}); };
// Return a function that applies the operations to a source iterator of tiddler titles
var fnMeasured = $tw.perf.measure("filter: " + filterString,fnFilter);
// Cache the measured filter function
if(this.filterCacheCount >= 2000) { if(this.filterCacheCount >= 2000) {
// To prevent memory leak, we maintain an upper limit for cache size. // To prevent memory leak, we maintain an upper limit for cache size.
// Reset if exceeded. This should give us 95% of the benefit // Reset if exceeded. This should give us 95% of the benefit