Abstracted direct fetch calls across components and contexts into new functions within `src/lib/client.ts`. This provides a consistent and centralized interface for interacting with backend APIs. - Introduced `src/lib/client.ts` to encapsulate API request logic. - Standardized audio buffer types (`TTSAudioBuffer`, `TTSAudioBytes`) in `src/types/tts.ts`. - Moved client-specific request types (`TTSRequestPayload`, `TTSRequestHeaders`, `TTSRetryOptions`) to `src/types/client.ts`. - Updated API routes and consumer components/contexts to leverage the new client library functions and type definitions. - Removed `src/utils/audio.ts` as its utility functions are now part of `src/lib/client.ts`.
22 lines
747 B
TypeScript
22 lines
747 B
TypeScript
'use client';
|
|
|
|
import { useRef } from 'react';
|
|
import { LRUCache } from 'lru-cache';
|
|
import type { TTSAudioBuffer } from '@/types/tts';
|
|
|
|
/**
|
|
* Custom hook for managing audio cache using LRU strategy
|
|
* @param maxSize Maximum number of items to store in cache
|
|
* @returns Object containing cache methods
|
|
*/
|
|
export function useAudioCache(maxSize = 50) {
|
|
const cacheRef = useRef(new LRUCache<string, TTSAudioBuffer>({ max: maxSize }));
|
|
|
|
return {
|
|
get: (key: string) => cacheRef.current.get(key),
|
|
set: (key: string, value: TTSAudioBuffer) => cacheRef.current.set(key, value),
|
|
delete: (key: string) => cacheRef.current.delete(key),
|
|
has: (key: string) => cacheRef.current.has(key),
|
|
clear: () => cacheRef.current.clear(),
|
|
};
|
|
}
|