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

@ -8,23 +8,23 @@ Adds tiddler filtering methods to the $tw.Wiki object.
\*/ \*/
(function(){ (function(){
/*jslint node: true, browser: true */ /*jslint node: true, browser: true */
/*global $tw: false */ /*global $tw: false */
"use strict"; "use strict";
var widgetClass = require("$:/core/modules/widgets/widget.js").widget; var widgetClass = require("$:/core/modules/widgets/widget.js").widget;
/* Maximum permitted filter recursion depth */ /* Maximum permitted filter recursion depth */
var MAX_FILTER_DEPTH = 300; var MAX_FILTER_DEPTH = 300;
/* /*
Parses an operation (i.e. a run) within a filter string Parses an operation (i.e. a run) within a filter string
operators: Array of array of operator nodes into which results should be inserted operators: Array of array of operator nodes into which results should be inserted
filterString: filter string filterString: filter string
p: start position within the string p: start position within the string
Returns the new start position, after the parsed operation Returns the new start position, after the parsed operation
*/ */
function parseFilterOperation(operators,filterString,p) { function parseFilterOperation(operators,filterString,p) {
var nextBracketPos, operator; var nextBracketPos, operator;
// Skip the starting square bracket // Skip the starting square bracket
if(filterString.charAt(p++) !== "[") { if(filterString.charAt(p++) !== "[") {
@ -133,12 +133,12 @@ Adds tiddler filtering methods to the $tw.Wiki object.
} }
// Return the parsing position // Return the parsing position
return p; return p;
} }
/* /*
Parse a filter string Parse a filter string
*/ */
exports.parseFilter = function(filterString) { exports.parseFilter = function(filterString) {
filterString = filterString || ""; filterString = filterString || "";
var results = [], // Array of arrays of operator nodes {operator:,operand:} var results = [], // Array of arrays of operator nodes {operator:,operand:}
p = 0, // Current position in the filter string p = 0, // Current position in the filter string
@ -196,42 +196,45 @@ Adds tiddler filtering methods to the $tw.Wiki object.
} }
} }
return results; return results;
}; };
exports.getFilterOperators = function() { exports.getFilterOperators = function() {
if(!this.filterOperators) { if(!this.filterOperators) {
$tw.Wiki.prototype.filterOperators = {}; $tw.Wiki.prototype.filterOperators = {};
$tw.modules.applyMethods("filteroperator",this.filterOperators); $tw.modules.applyMethods("filteroperator",this.filterOperators);
} }
return this.filterOperators; return this.filterOperators;
}; };
exports.getFilterRunPrefixes = function() { exports.getFilterRunPrefixes = function() {
if(!this.filterRunPrefixes) { if(!this.filterRunPrefixes) {
$tw.Wiki.prototype.filterRunPrefixes = {}; $tw.Wiki.prototype.filterRunPrefixes = {};
$tw.modules.applyMethods("filterrunprefix",this.filterRunPrefixes); $tw.modules.applyMethods("filterrunprefix",this.filterRunPrefixes);
} }
return this.filterRunPrefixes; return this.filterRunPrefixes;
} }
exports.filterTiddlers = function(filterString,widget,source) { exports.filterTiddlers = function(filterString,widget,source) {
var fn = this.compileFilter(filterString); var fn = this.compileFilter(filterString);
return fn.call(this,source,widget); return fn.call(this,source,widget);
}; };
/* /*
Compile a filter into a function with the signature fn(source,widget) where: Compile a filter into a function with the signature fn(source,widget) where:
source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title) source: an iterator function for the source tiddlers, called source(iterator), where iterator is called as iterator(tiddler,title)
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
@ -366,7 +372,7 @@ Adds tiddler filtering methods to the $tw.Wiki object.
this.filterCache[filterString] = fnMeasured; this.filterCache[filterString] = fnMeasured;
this.filterCacheCount++; this.filterCacheCount++;
return fnMeasured; return fnMeasured;
}; };
})(); })();