ChemPal Documentation - v1.6.0
    Preparing search index...

    Function collectCachedTitles

    • Builds a deduplicated corpus of product titles from the local IndexedDB caches. No network requests are made — this reads supplier_query_cache (the raw per-supplier result sets, which carry the originating query) and the persisted search_results row (the products currently in the results table).

      Parameters

      • source: CorpusSourceOption = 'all'

        Which stores to read: "cache" (supplier query cache), "results" (the current search results), or "both". Defaults to "both".

      Returns Promise<CorpusEntry[]>

      The unique cached titles, each tagged with its supplier and origin store.

      const corpus = await collectCachedTitles();
      corpus.length; // => 1842
      corpus[0]; // => { title: "Sodium Chloride ACS", supplier: "Loudwolf", source: "queryCache", cachedQuery: "sodium chloride" }
      export async function collectCachedTitles(
      source: CorpusSourceOption = 'all',
      ): Promise<CorpusEntry[]> {
      const entries: CorpusEntry[] = [];
      const wantsCache = source === 'all' || source === 'both' || source === 'cache';
      const wantsResults = source === 'all' || source === 'both' || source === 'results';

      if (wantsCache || source === 'queryCache') {
      const cached = await getAllSupplierQueryCacheEntries();
      for (const entry of cached) {
      // The store is typed `unknown[]` but holds whatever was written, so a legacy
      // or partially-written row can be a non-array. Skip it rather than throwing.
      if (!Array.isArray(entry.data)) {
      continue;
      }
      for (const item of entry.data) {
      const title = readStringField(item, 'title');
      if (title === undefined) {
      continue;
      }
      entries.push({
      title,
      supplier: readStringField(item, 'supplier') ?? entry.__cacheMetadata.supplier,
      source: 'queryCache',
      cachedQuery: entry.__cacheMetadata.query,
      url: readStringField(item, 'url'),
      });
      }
      }
      }

      if (wantsResults || source === 'searchResults') {
      for (const product of await getSearchResults()) {
      if (product.title === '') {
      continue;
      }
      entries.push({
      title: product.title,
      supplier: product.supplier,
      source: 'searchResults',
      url: product.url,
      });
      }
      }

      const seen = new Set<string>();
      return entries.filter((entry) => {
      const key = `${entry.supplier ?? ''}::${entry.title.toLowerCase()}`;
      if (seen.has(key)) {
      return false;
      }
      seen.add(key);
      return true;
      });
      }