Remove key hashing and explicitly clear the cache after printing characters

e1d6a47048e0d53921214607387f2687d2fb805f

Joe <joenunezb@gmail.com>

2 files changed, +11 -18Showing whitespace changes
public/script.js+2 -0
@@ -268,6 +268,7 @@ import { initServerHistory } from './scripts/server-history.js';
268import { initSettingsSearch } from './scripts/setting-search.js';268import { initSettingsSearch } from './scripts/setting-search.js';
269import { initBulkEdit } from './scripts/bulk-edit.js';269import { initBulkEdit } from './scripts/bulk-edit.js';
270import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';270import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
271import { clearFuzzySearchCaches } from './scripts/power-user.js';
271272
272//exporting functions and vars for mods273//exporting functions and vars for mods
273export {274export {
@@ -1515,6 +1516,7 @@ export async function printCharacters(fullRefresh = false) {
1515 });1516 });
15161517
1517 favsToHotswap();1518 favsToHotswap();
1519 clearFuzzySearchCaches();
1518}1520}
15191521
1520/** Checks the state of the current search, and adds/removes the search sorting option accordingly */1522/** Checks the state of the current search, and adds/removes the search sorting option accordingly */
public/scripts/power-user.js+9 -18
@@ -329,11 +329,11 @@ let browser_has_focus = true;
329const debug_functions = [];329const debug_functions = [];
330330
331const fuzzySearchCaches = {331const fuzzySearchCaches = {
332 characters: { keyHash: null, resultMap: new Map() },332 characters: { resultMap: new Map() },
333 worldInfo: { keyHash: null, resultMap: new Map() },333 worldInfo: { resultMap: new Map() },
334 personas: { keyHash: null, resultMap: new Map() },334 personas: { resultMap: new Map() },
335 tags: { keyHash: null, resultMap: new Map() },335 tags: { resultMap: new Map() },
336 groups: { keyHash: null, resultMap: new Map() },336 groups: { resultMap: new Map() },
337};337};
338338
339const fuzzySearchCategories = {339const fuzzySearchCategories = {
@@ -1864,22 +1864,14 @@ function hashFuseKeys(keys) {
1864 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score1864 * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
1865 */1865 */
1866function performFuzzySearch(type, data, keys, searchValue) {1866function performFuzzySearch(type, data, keys, searchValue) {
1867 const currentKeyHash = hashFuseKeys(keys);
1868 const cache = fuzzySearchCaches[type];1867 const cache = fuzzySearchCaches[type];
18691868
1870 // Check cache for existing results1869 // Check cache for existing results
1871 if (cache.keyHash === currentKeyHash && cache.resultMap.has(searchValue)) {1870 if (cache.resultMap.has(searchValue)) {
1872 // console.debug(`Using cached ${type} fuzzy search results for ${searchValue}`);
1873 return cache.resultMap.get(searchValue);1871 return cache.resultMap.get(searchValue);
1874 }1872 }
18751873
1876 // Clear cache if keys changed1874 // @ts-ignore
1877 if (cache.keyHash !== currentKeyHash) {
1878 // console.debug(`${type} Fuse keys changed, clearing cache`);
1879 cache.keyHash = currentKeyHash;
1880 cache.resultMap.clear();
1881 }
1882
1883 const fuse = new Fuse(data, {1875 const fuse = new Fuse(data, {
1884 keys: keys,1876 keys: keys,
1885 includeScore: true,1877 includeScore: true,
@@ -1890,19 +1882,18 @@ function performFuzzySearch(type, data, keys, searchValue) {
18901882
1891 const results = fuse.search(searchValue);1883 const results = fuse.search(searchValue);
1892 cache.resultMap.set(searchValue, results);1884 cache.resultMap.set(searchValue, results);
1893
1894 // console.debug(`${type} fuzzy search results for ${searchValue}`, results);
1895 return results;1885 return results;
1896}1886}
18971887
1888
1898/**1889/**
1899 * Clears all fuzzy search caches1890 * Clears all fuzzy search caches
1900 */1891 */
1901export function clearFuzzySearchCaches() {1892export function clearFuzzySearchCaches() {
1902 for (const cache of Object.values(fuzzySearchCaches)) {1893 for (const cache of Object.values(fuzzySearchCaches)) {
1903 cache.keyHash = null;
1904 cache.resultMap.clear();1894 cache.resultMap.clear();
1905 }1895 }
1896 console.log('Fuzzy search caches cleared');
1906}1897}
19071898
1908/**1899/**