mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Encapsulate logic into filters instead of spreading around
This commit is contained in:
@ -55,6 +55,19 @@ export function isFilterState(a, b) {
|
||||
return aKey === bKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* The fuzzy search categories
|
||||
* @type {{ characters: string, worldInfo: string, personas: string, tags: string, groups: string }}
|
||||
*/
|
||||
export const fuzzySearchCategories = Object.freeze({
|
||||
characters: 'characters',
|
||||
worldInfo: 'worldInfo',
|
||||
personas: 'personas',
|
||||
tags: 'tags',
|
||||
groups: 'groups',
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Helper class for filtering data.
|
||||
* @example
|
||||
@ -72,6 +85,12 @@ export class FilterHelper {
|
||||
*/
|
||||
scoreCache;
|
||||
|
||||
/**
|
||||
* Cache for fuzzy search results per category.
|
||||
* @type {Object.<string, { resultMap: Map<string, any> }>}
|
||||
*/
|
||||
fuzzySearchCaches;
|
||||
|
||||
/**
|
||||
* Creates a new FilterHelper
|
||||
* @param {Function} onDataChanged Callback to trigger when the filter data changes
|
||||
@ -79,6 +98,13 @@ export class FilterHelper {
|
||||
constructor(onDataChanged) {
|
||||
this.onDataChanged = onDataChanged;
|
||||
this.scoreCache = new Map();
|
||||
this.fuzzySearchCaches = {
|
||||
[fuzzySearchCategories.characters]: { resultMap: new Map() },
|
||||
[fuzzySearchCategories.worldInfo]: { resultMap: new Map() },
|
||||
[fuzzySearchCategories.personas]: { resultMap: new Map() },
|
||||
[fuzzySearchCategories.tags]: { resultMap: new Map() },
|
||||
[fuzzySearchCategories.groups]: { resultMap: new Map() },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -151,7 +177,7 @@ export class FilterHelper {
|
||||
return data;
|
||||
}
|
||||
|
||||
const fuzzySearchResults = fuzzySearchWorldInfo(data, term);
|
||||
const fuzzySearchResults = fuzzySearchWorldInfo(data, term, this.fuzzySearchCaches);
|
||||
this.cacheScores(FILTER_TYPES.WORLD_INFO_SEARCH, new Map(fuzzySearchResults.map(i => [i.item?.uid, i.score])));
|
||||
|
||||
const filteredData = data.filter(entity => fuzzySearchResults.find(x => x.item === entity));
|
||||
@ -170,7 +196,7 @@ export class FilterHelper {
|
||||
return data;
|
||||
}
|
||||
|
||||
const fuzzySearchResults = fuzzySearchPersonas(data, term);
|
||||
const fuzzySearchResults = fuzzySearchPersonas(data, term, this.fuzzySearchCaches);
|
||||
this.cacheScores(FILTER_TYPES.PERSONA_SEARCH, new Map(fuzzySearchResults.map(i => [i.item.key, i.score])));
|
||||
|
||||
const filteredData = data.filter(name => fuzzySearchResults.find(x => x.item.key === name));
|
||||
@ -289,9 +315,9 @@ export class FilterHelper {
|
||||
|
||||
// Save fuzzy search results and scores if enabled
|
||||
if (power_user.fuzzy_search) {
|
||||
const fuzzySearchCharactersResults = fuzzySearchCharacters(searchValue);
|
||||
const fuzzySearchGroupsResults = fuzzySearchGroups(searchValue);
|
||||
const fuzzySearchTagsResult = fuzzySearchTags(searchValue);
|
||||
const fuzzySearchCharactersResults = fuzzySearchCharacters(searchValue, this.fuzzySearchCaches);
|
||||
const fuzzySearchGroupsResults = fuzzySearchGroups(searchValue, this.fuzzySearchCaches);
|
||||
const fuzzySearchTagsResult = fuzzySearchTags(searchValue, this.fuzzySearchCaches);
|
||||
this.cacheScores(FILTER_TYPES.SEARCH, new Map(fuzzySearchCharactersResults.map(i => [`character.${i.refIndex}`, i.score])));
|
||||
this.cacheScores(FILTER_TYPES.SEARCH, new Map(fuzzySearchGroupsResults.map(i => [`group.${i.item.id}`, i.score])));
|
||||
this.cacheScores(FILTER_TYPES.SEARCH, new Map(fuzzySearchTagsResult.map(i => [`tag.${i.item.id}`, i.score])));
|
||||
@ -343,11 +369,14 @@ export class FilterHelper {
|
||||
* @param {object} options - Optional call parameters
|
||||
* @param {boolean} [options.clearScoreCache=true] - Whether the score cache should be cleared.
|
||||
* @param {Object.<FilterType, any>} [options.tempOverrides={}] - Temporarily override specific filters for this filter application
|
||||
* @param {boolean} [options.clearFuzzySearchCaches=true] - Whether the fuzzy search caches should be cleared.
|
||||
* @returns {any[]} The filtered data.
|
||||
*/
|
||||
applyFilters(data, { clearScoreCache = true, tempOverrides = {} } = {}) {
|
||||
applyFilters(data, { clearScoreCache = true, tempOverrides = {}, clearFuzzySearchCaches = true } = {}) {
|
||||
if (clearScoreCache) this.clearScoreCache();
|
||||
|
||||
if (clearFuzzySearchCaches) this.clearFuzzySearchCaches();
|
||||
|
||||
// Save original filter states
|
||||
const originalStates = {};
|
||||
for (const key in tempOverrides) {
|
||||
@ -411,4 +440,20 @@ export class FilterHelper {
|
||||
this.scoreCache = new Map();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears fuzzy search caches
|
||||
* @param {keyof typeof fuzzySearchCategories} [type] Optional cache type to clear. If not provided, clears all caches
|
||||
*/
|
||||
clearFuzzySearchCaches(type = null) {
|
||||
if (type && this.fuzzySearchCaches[type]) {
|
||||
this.fuzzySearchCaches[type].resultMap.clear();
|
||||
console.log(`Fuzzy search cache cleared for: ${type}`);
|
||||
} else {
|
||||
for (const cache of Object.values(this.fuzzySearchCaches)) {
|
||||
cache.resultMap.clear();
|
||||
}
|
||||
console.log('All fuzzy search caches cleared');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user