| 11 | * 3. 提供统一的数据访问接口 |
| 12 | */ |
| 13 | export class HighlightRepository implements IHighlightRepository { |
| 14 | private cache: Map<string, HiNote[]> = new Map(); |
| 15 | private dataManager: HiNoteDataManager; |
| 16 | |
| 17 | constructor(dataManager: HiNoteDataManager) { |
| 18 | this.dataManager = dataManager; |
| 19 | } |
| 20 | |
| 21 | async initialize(): Promise<void> { |
| 22 | await this.dataManager.initialize(); |
| 23 | await this.loadAllHighlightsToCache(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * 从存储层加载所有高亮到缓存 |
| 28 | */ |
| 29 | private async loadAllHighlightsToCache(): Promise<void> { |
| 30 | try { |
| 31 | const highlightFiles = await this.dataManager.getAllHighlightFiles(); |
| 32 | |
| 33 | for (const filePath of highlightFiles) { |
| 34 | if (!this.cache.has(filePath)) { |
| 35 | this.cache.set(filePath, []); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | for (const filePath of highlightFiles) { |
| 40 | void this.loadFileHighlightsAsync(filePath); |
| 41 | } |
| 42 | } catch (error) { |
| 43 | console.error('[HighlightRepository] 加载高亮文件列表失败:', error); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * 异步加载单个文件的高亮数据 |
| 49 | */ |
| 50 | private async loadFileHighlightsAsync(filePath: string): Promise<void> { |
| 51 | try { |
| 52 | const highlights = await this.dataManager.getFileHighlights(filePath); |
| 53 | if (highlights.length > 0) { |
| 54 | this.cache.set(filePath, highlights); |
| 55 | } else { |
| 56 | this.cache.delete(filePath); |
| 57 | } |
| 58 | } catch (error) { |
| 59 | console.warn(`[HighlightRepository] 加载文件 ${filePath} 的高亮数据失败:`, error); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | async getFileHighlights(filePath: string): Promise<HiNote[]> { |
| 64 | if (this.cache.has(filePath)) { |
| 65 | return this.cache.get(filePath) || []; |
| 66 | } |
| 67 | |
| 68 | const highlights = await this.dataManager.getFileHighlights(filePath); |
| 69 | this.cache.set(filePath, highlights); |
| 70 | return highlights; |
nothing calls this directly
no outgoing calls
no test coverage detected