Adopt class syntax

This commit is contained in:
Jeremy Ruston 2025-06-20 17:46:31 +01:00
parent a4c84d727c
commit dcf0f449c4
2 changed files with 162 additions and 168 deletions

View file

@ -9,57 +9,52 @@ Class to dispatch actions when filters change
"use strict";
function BackgroundActionDispatcher(filterTracker,wiki) {
var self = this;
this.filterTracker = filterTracker;
this.wiki = wiki;
this.nextTrackedFilterId = 1;
this.trackedFilters = Object.create(null); // Hashmap by id
// Track the filter for the background actions
this.filterTracker.track({
filterString: "[all[tiddlers+shadows]tag[$:/tags/BackgroundAction]!is[draft]]",
fnEnter: function fnEnter(title) {
return self.trackFilter(title);
},
fnLeave: function fnLeave(title,enterValue) {
self.untrackFilter(enterValue);
},
fnChange: function fnChange(title,enterValue) {
self.untrackFilter(enterValue);
return self.trackFilter(title);
},
fnProcess: function fnProcess(changes) {
self.process(changes);
}
});
}
class BackgroundActionDispatcher {
constructor(filterTracker, wiki) {
this.filterTracker = filterTracker;
this.wiki = wiki;
this.nextTrackedFilterId = 1;
this.trackedFilters = Object.create(null); // Hashmap by id
// Track the filter for the background actions
this.filterTracker.track({
filterString: "[all[tiddlers+shadows]tag[$:/tags/BackgroundAction]!is[draft]]",
fnEnter: (title) => this.trackFilter(title),
fnLeave: (title, enterValue) => this.untrackFilter(enterValue),
fnChange: (title, enterValue) => {
this.untrackFilter(enterValue);
return this.trackFilter(title);
},
fnProcess: (changes) => this.process(changes)
});
}
BackgroundActionDispatcher.prototype.trackFilter = function(title) {
var tiddler = this.wiki.getTiddler(title),
id = this.nextTrackedFilterId++,
tracker = new BackgroundActionTracker({
trackFilter(title) {
const tiddler = this.wiki.getTiddler(title);
const id = this.nextTrackedFilterId++;
const tracker = new BackgroundActionTracker({
wiki: this.wiki,
title: title,
trackFilter: tiddler.fields["track-filter"],
actions: tiddler.fields.text
});
this.trackedFilters[id] = tracker;
return id;
};
BackgroundActionDispatcher.prototype.untrackFilter = function(enterValue) {
var tracker = this.trackedFilters[enterValue];
if(tracker) {
tracker.destroy();
this.trackedFilters[id] = tracker;
return id;
}
delete this.trackedFilters[enterValue];
};
BackgroundActionDispatcher.prototype.process = function(changes) {
for(var id in this.trackedFilters) {
this.trackedFilters[id].process(changes);
untrackFilter(enterValue) {
const tracker = this.trackedFilters[enterValue];
if(tracker) {
tracker.destroy();
}
delete this.trackedFilters[enterValue];
}
};
process(changes) {
for(const id in this.trackedFilters) {
this.trackedFilters[id].process(changes);
}
}
}
/*
Represents an individual tracked filter. Options include:
@ -68,57 +63,54 @@ title: title of the tiddler being tracked
trackFilter: filter string to track changes
actions: actions to be executed when the filter changes
*/
function BackgroundActionTracker(options) {
var self = this;
this.wiki = options.wiki;
this.title = options.title;
this.trackFilter = options.trackFilter;
this.actions = options.actions;
this.filterTracker = new $tw.FilterTracker(this.wiki);
this.hasChanged = false;
this.trackerID = this.filterTracker.track({
filterString: this.trackFilter,
fnEnter: function(title) {
self.hasChanged = true;
},
fnLeave: function(title,enterValue) {
self.hasChanged = true;
},
fnProcess: function(changes) {
if(self.hasChanged) {
self.hasChanged = false;
console.log("Processing background action",self.title);
var tiddler = self.wiki.getTiddler(self.title),
doActions = true;
if(tiddler && tiddler.fields.platforms) {
doActions = false;
var platforms = $tw.utils.parseStringArray(tiddler.fields.platforms);
if(($tw.browser && platforms.indexOf("browser") !== -1) || ($tw.node && platforms.indexOf("node") !== -1)) {
doActions = true;
class BackgroundActionTracker {
constructor(options) {
this.wiki = options.wiki;
this.title = options.title;
this.trackFilter = options.trackFilter;
this.actions = options.actions;
this.filterTracker = new $tw.FilterTracker(this.wiki);
this.hasChanged = false;
this.trackerID = this.filterTracker.track({
filterString: this.trackFilter,
fnEnter: (title) => { this.hasChanged = true; },
fnLeave: (title, enterValue) => { this.hasChanged = true; },
fnProcess: (changes) => {
if(this.hasChanged) {
this.hasChanged = false;
console.log("Processing background action", this.title);
const tiddler = this.wiki.getTiddler(this.title);
let doActions = true;
if(tiddler && tiddler.fields.platforms) {
doActions = false;
const platforms = $tw.utils.parseStringArray(tiddler.fields.platforms);
if(($tw.browser && platforms.indexOf("browser") !== -1) || ($tw.node && platforms.indexOf("node") !== -1)) {
doActions = true;
}
}
if(doActions) {
this.wiki.invokeActionString(
this.actions,
null,
{
currentTiddler: this.title
},{
parentWidget: $tw.rootWidget
}
);
}
}
if(doActions) {
self.wiki.invokeActionString(
self.actions,
null,
{
currentTiddler: self.title
},{
parentWidget: $tw.rootWidget
}
);
}
}
}
});
});
}
process(changes) {
this.filterTracker.handleChangeEvent(changes);
}
destroy() {
this.filterTracker.untrack(this.trackerID);
}
}
BackgroundActionTracker.prototype.process = function(changes) {
this.filterTracker.handleChangeEvent(changes);
};
BackgroundActionTracker.prototype.destroy = function() {
this.filterTracker.untrack(this.trackerID);
};
exports.BackgroundActionDispatcher = BackgroundActionDispatcher;

View file

@ -9,95 +9,97 @@ Class to track the results of a filter string
"use strict";
function FilterTracker(wiki) {
this.wiki = wiki;
this.trackers = [];
this.nextTrackerId = 1;
}
class FilterTracker {
constructor(wiki) {
this.wiki = wiki;
this.trackers = [];
this.nextTrackerId = 1;
}
FilterTracker.prototype.handleChangeEvent = function(changes) {
this.processTrackers();
this.processChanges(changes);
};
handleChangeEvent(changes) {
this.processTrackers();
this.processChanges(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. 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(options) {
// Add the tracker details
var tracker = {
id: this.nextTrackerId++,
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
};
this.trackers.push(tracker);
// Process the tracker
this.processTracker(this.trackers.length - 1);
return tracker.id;
};
/*
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)
*/
track(options) {
// Add the tracker details
const tracker = {
id: this.nextTrackerId++,
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
};
this.trackers.push(tracker);
// Process the tracker
this.processTracker(this.trackers.length - 1);
return tracker.id;
}
FilterTracker.prototype.untrack = function(id) {
for(var t=0; t<this.trackers.length; t++) {
if(this.trackers[t].id === id) {
this.trackers.splice(t,1);
break;
untrack(id) {
for(let t = 0; t < this.trackers.length; t++) {
if(this.trackers[t].id === id) {
this.trackers.splice(t, 1);
break;
}
}
}
};
FilterTracker.prototype.processTrackers = function() {
for(var t=0; t<this.trackers.length; t++) {
this.processTracker(t);
processTrackers() {
for(let t = 0; t < this.trackers.length; t++) {
this.processTracker(t);
}
}
};
FilterTracker.prototype.processTracker = function(index) {
var tracker = this.trackers[index];
var results = [];
// Evaluate the filter and remove duplicate results
$tw.utils.each(this.wiki.filterTiddlers(tracker.filterString),function(title) {
$tw.utils.pushTop(results,title);
});
// Process the newly entered results
$tw.utils.each(results,function(title) {
if(tracker.previousResults.indexOf(title) === -1 && !tracker.resultValues[title] && tracker.fnEnter) {
tracker.resultValues[title] = tracker.fnEnter(title) || true;
}
});
// Process the results that have just left
$tw.utils.each(tracker.previousResults,function(title) {
if(results.indexOf(title) === -1 && tracker.resultValues[title] && tracker.fnLeave) {
tracker.fnLeave(title,tracker.resultValues[title]);
delete tracker.resultValues[title];
}
});
// Update the previous results
tracker.previousResults = results;
};
FilterTracker.prototype.processChanges = function(changes) {
for(var t=0; t<this.trackers.length; t++) {
var tracker = this.trackers[t];
$tw.utils.each(changes,function(change,title) {
if(title && tracker.previousResults.indexOf(title) !== -1 && tracker.fnChange) {
// Call the change function and if it doesn't return a value then keep the old value
tracker.resultValues[title] = tracker.fnChange(title,tracker.resultValues[title]) || tracker.resultValues[title];
processTracker(index) {
const tracker = this.trackers[index];
const results = [];
// Evaluate the filter and remove duplicate results
$tw.utils.each(this.wiki.filterTiddlers(tracker.filterString), function(title) {
$tw.utils.pushTop(results, title);
});
// Process the newly entered results
$tw.utils.each(results, function(title) {
if(tracker.previousResults.indexOf(title) === -1 && !tracker.resultValues[title] && tracker.fnEnter) {
tracker.resultValues[title] = tracker.fnEnter(title) || true;
}
});
if(tracker.fnProcess) {
tracker.fnProcess(changes);
// Process the results that have just left
$tw.utils.each(tracker.previousResults, function(title) {
if(results.indexOf(title) === -1 && tracker.resultValues[title] && tracker.fnLeave) {
tracker.fnLeave(title, tracker.resultValues[title]);
delete tracker.resultValues[title];
}
});
// Update the previous results
tracker.previousResults = results;
}
processChanges(changes) {
for(let t = 0; t < this.trackers.length; t++) {
const tracker = this.trackers[t];
$tw.utils.each(changes, function(change, title) {
if(title && tracker.previousResults.indexOf(title) !== -1 && tracker.fnChange) {
// Call the change function and if it doesn't return a value then keep the old value
tracker.resultValues[title] = tracker.fnChange(title, tracker.resultValues[title]) || tracker.resultValues[title];
}
});
if(tracker.fnProcess) {
tracker.fnProcess(changes);
}
}
}
};
}
exports.FilterTracker = FilterTracker;