Function applyNormalizationCacheDeltas

  • Merges worker-produced deltas into the canonical normalization cache. Applies added and modified entries from each algorithm.

    Parameters

    • Optionaldeltas: Record<
          string,
          { added: Record<string, string>; modified: Record<string, string> },
      >

      Delta entries by algorithm.

    Returns void

    export function applyNormalizationCacheDeltas(
    deltas?: Record<
    string,
    {
    added: Record<string, string>;
    modified: Record<string, string>;
    }
    >,
    ): void {
    if (!deltas || Object.keys(deltas).length === 0) {
    console.debug("[Storage] 📖 No deltas to apply to normalization cache");
    return;
    }

    try {
    const cache = getTitleNormalizationCache();

    for (const [algorithm, delta] of Object.entries(deltas)) {
    if (!cache.caches[algorithm]) {
    cache.caches[algorithm] = {};
    }

    // Apply additions
    Object.assign(cache.caches[algorithm], delta.added);

    // Apply modifications
    Object.assign(cache.caches[algorithm], delta.modified);
    }

    cache.lastUpdated = Date.now();
    saveTitleNormalizationCache(cache);

    console.info(
    `[Storage] ✅ Applied normalization cache deltas for ${Object.keys(deltas).length} algorithms`,
    );
    } catch (error) {
    console.error(
    "[Storage] ❌ Error applying normalization cache deltas",
    error,
    );
    }
    }