1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-21 02:03:13 +00:00

Refactor filter tracker

1. Pass params as an options object
2. Add fnProcess which is called during processing after enter, leave and change have been called
This commit is contained in:
Jeremy Ruston 2025-01-07 14:53:38 +00:00
parent 806960afc7
commit 508c74f8e4
3 changed files with 20 additions and 8 deletions

View File

@ -16,7 +16,6 @@ function FilterTracker(wiki) {
this.wiki = wiki;
this.trackers = [];
this.nextTrackerId = 1;
this.wiki.addEventListener("change",this.handleChangeEvent.bind(this));
}
FilterTracker.prototype.handleChangeEvent = function(changes) {
@ -25,20 +24,22 @@ FilterTracker.prototype.handleChangeEvent = function(changes) {
};
/*
Add a tracker to the filter tracker. Returns null if any of the parameters are invalid, or a tracker id if the tracker was added successfully
Add a tracker to the filter tracker. Returns null if any of the parameters are invalid, or a tracker id if the tracker was added successfully. Options include:
filterString: the filter string to track
fnEnter: function to call when a title enters the filter results. Called even if the tiddler does not actually exist. Called as (title), and should return a truthy value that is stored in the tracker as the "enterValue"
fnLeave: function to call when a title leaves the filter results. Called as (title,enterValue)
fnChange: function to call when a tiddler changes in the filter results. Only called for filter results that identify a tiddler or shadow tiddler. Called as (title,enterValue), and may optionally return a replacement enterValue
fnProcess: function to call each time the tracker is processed, after any enter, leave or change functions are called. Called as (changes)
*/
FilterTracker.prototype.track = function(filterString,fnEnter,fnLeave,fnChange) {
FilterTracker.prototype.track = function(options) {
// Add the tracker details
var tracker = {
id: this.nextTrackerId++,
filterString: filterString,
fnEnter: fnEnter,
fnLeave: fnLeave,
fnChange: fnChange,
filterString: options.filterString,
fnEnter: options.fnEnter,
fnLeave: options.fnLeave,
fnChange: options.fnChange,
fnProcess: options.fnProcess,
previousResults: [], // Results from the previous time the tracker was processed
resultValues: {} // Map by title to the value returned by fnEnter
};
@ -96,6 +97,9 @@ FilterTracker.prototype.processChanges = function(changes) {
tracker.resultValues[title] = tracker.fnChange(title,tracker.resultValues[title]) || tracker.resultValues[title];
}
});
if(tracker.fnProcess) {
tracker.fnProcess(changes);
}
}
};

View File

@ -59,7 +59,12 @@ exports.getInfoTiddlerFields = function(updateInfoTiddlersCallback) {
untrack(enterValue);
return track(title);
}
$tw.filterTracker.track("[all[tiddlers+shadows]tag[$:/tags/MediaQueryTracker]!is[draft]]",fnEnter,fnLeave,fnChange);
$tw.filterTracker.track({
filterString: "[all[tiddlers+shadows]tag[$:/tags/MediaQueryTracker]!is[draft]]",
fnEnter: fnEnter,
fnLeave: fnLeave,
fnChange: fnChange
});
}
return [];
};

View File

@ -46,6 +46,9 @@ exports.startup = function() {
$tw.perf = new $tw.Performance($tw.wiki.getTiddlerText(PERFORMANCE_INSTRUMENTATION_CONFIG_TITLE,"no") === "yes");
// Kick off the filter tracker
$tw.filterTracker = new $tw.FilterTracker($tw.wiki);
$tw.wiki.addEventListener("change",function(changes) {
$tw.filterTracker.handleChangeEvent(changes);
});
};
})();