Remove key hashing and explicitly clear the cache after printing characters
| @@ -268,6 +268,7 @@ import { initServerHistory } from './scripts/server-history.js'; | |||
| 268 | import { initSettingsSearch } from './scripts/setting-search.js'; | 268 | import { initSettingsSearch } from './scripts/setting-search.js'; |
| 269 | import { initBulkEdit } from './scripts/bulk-edit.js'; | 269 | import { initBulkEdit } from './scripts/bulk-edit.js'; |
| 270 | import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js'; | 270 | import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js'; |
| 271 | import { clearFuzzySearchCaches } from './scripts/power-user.js'; | ||
| 271 | 272 | ||
| 272 | //exporting functions and vars for mods | 273 | //exporting functions and vars for mods |
| 273 | export { | 274 | export { |
| @@ -1515,6 +1516,7 @@ export async function printCharacters(fullRefresh = false) { | |||
| 1515 | }); | 1516 | }); |
| 1516 | 1517 | ||
| 1517 | favsToHotswap(); | 1518 | favsToHotswap(); |
| 1519 | clearFuzzySearchCaches(); | ||
| 1518 | } | 1520 | } |
| 1519 | 1521 | ||
| 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 */ |
| @@ -329,11 +329,11 @@ let browser_has_focus = true; | |||
| 329 | const debug_functions = []; | 329 | const debug_functions = []; |
| 330 | 330 | ||
| 331 | const fuzzySearchCaches = { | 331 | const 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 | }; |
| 338 | 338 | ||
| 339 | const fuzzySearchCategories = { | 339 | const fuzzySearchCategories = { |
| @@ -1864,22 +1864,14 @@ function hashFuseKeys(keys) { | |||
| 1864 | * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score | 1864 | * @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score |
| 1865 | */ | 1865 | */ |
| 1866 | function performFuzzySearch(type, data, keys, searchValue) { | 1866 | function performFuzzySearch(type, data, keys, searchValue) { |
| 1867 | const currentKeyHash = hashFuseKeys(keys); | ||
| 1868 | const cache = fuzzySearchCaches[type]; | 1867 | const cache = fuzzySearchCaches[type]; |
| 1869 | 1868 | ||
| 1870 | // Check cache for existing results | 1869 | // 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 | } |
| 1875 | 1873 | ||
| 1876 | // Clear cache if keys changed | 1874 | // @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) { | |||
| 1890 | 1882 | ||
| 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 | } |
| 1897 | 1887 | ||
| 1888 | |||
| 1898 | /** | 1889 | /** |
| 1899 | * Clears all fuzzy search caches | 1890 | * Clears all fuzzy search caches |
| 1900 | */ | 1891 | */ |
| 1901 | export function clearFuzzySearchCaches() { | 1892 | export 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 | } |
| 1907 | 1898 | ||
| 1908 | /** | 1899 | /** |