All files / src/common/hooks useReadingQueueCacheOptimizer.ts

5.26% Statements 2/38
100% Branches 0/0
0% Functions 0/1
5.26% Lines 2/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x       1x                                                                                        
import { useEffect } from 'react';
import { ReadingQueueItem, User } from '@common/types';
import { CacheManager } from '@common/utils/cacheUtils'; // Assuming CacheManager type is exported
 
export const useReadingQueueCacheOptimizer = (
  cacheManager: CacheManager | null,
  user: User | null | undefined,
  readingQueue: ReadingQueueItem[]
) => {
  useEffect(() => {
    if (cacheManager && user?.id && readingQueue) {
      // Warm up critical caches for better performance
      cacheManager.warmCache(user.id, 'high');
 
      // Pre-warm newsletter details for queue items
      if (readingQueue.length > 0) {
        // Batch pre-load first 5 newsletters for instant access
        const newsletterIds = readingQueue.slice(0, 5).map((item) => item.newsletter_id);
 
        if (newsletterIds.length > 0) {
          setTimeout(() => {
            cacheManager.batchInvalidateQueries([
              {
                type: 'newsletter-detail',
                ids: newsletterIds,
              },
            ]);
          }, 100);
        }
 
        // Pre-load next batch in background
        if (readingQueue.length > 5) {
          const nextBatchIds = readingQueue.slice(5, 10).map((item) => item.newsletter_id);
          if (nextBatchIds.length > 0) {
            setTimeout(() => {
              cacheManager.batchInvalidateQueries([
                {
                  type: 'newsletter-detail',
                  ids: nextBatchIds,
                },
              ]);
            }, 500);
          }
        }
      }
    }
  }, [cacheManager, user?.id, readingQueue]); // Ensure user.id is in dependency array if user can change
};