(minCount: number, modeFilter?: HistoryMode)
| 18 | let pendingLoadTarget = 0; |
| 19 | let pendingLoadModeFilter: HistoryMode | undefined = undefined; |
| 20 | async function loadHistoryEntries(minCount: number, modeFilter?: HistoryMode): Promise<HistoryEntry[]> { |
| 21 | // Round up to next chunk to avoid repeated small reads |
| 22 | const target = Math.ceil(minCount / HISTORY_CHUNK_SIZE) * HISTORY_CHUNK_SIZE; |
| 23 | |
| 24 | // If a load is already pending with the same mode filter and will satisfy our needs, wait for it |
| 25 | if (pendingLoad && pendingLoadTarget >= target && pendingLoadModeFilter === modeFilter) { |
| 26 | return pendingLoad; |
| 27 | } |
| 28 | |
| 29 | // If a load is pending but won't satisfy our needs or has different filter, we need to wait for it |
| 30 | // to complete first, then start a new one (can't interrupt an ongoing read) |
| 31 | if (pendingLoad) { |
| 32 | await pendingLoad; |
| 33 | } |
| 34 | |
| 35 | // Start a new load |
| 36 | pendingLoadTarget = target; |
| 37 | pendingLoadModeFilter = modeFilter; |
| 38 | pendingLoad = (async () => { |
| 39 | const entries: HistoryEntry[] = []; |
| 40 | let loaded = 0; |
| 41 | for await (const entry of getHistory()) { |
| 42 | // If mode filter is specified, only include entries that match the mode |
| 43 | if (modeFilter) { |
| 44 | const entryMode = getModeFromInput(entry.display); |
| 45 | if (entryMode !== modeFilter) { |
| 46 | continue; |
| 47 | } |
| 48 | } |
| 49 | entries.push(entry); |
| 50 | loaded++; |
| 51 | if (loaded >= pendingLoadTarget) break; |
| 52 | } |
| 53 | return entries; |
| 54 | })(); |
| 55 | try { |
| 56 | return await pendingLoad; |
| 57 | } finally { |
| 58 | pendingLoad = null; |
| 59 | pendingLoadTarget = 0; |
| 60 | pendingLoadModeFilter = undefined; |
| 61 | } |
| 62 | } |
| 63 | export function useArrowKeyHistory(onSetInput: (value: string, mode: HistoryMode, pastedContents: Record<number, PastedContent>) => void, currentInput: string, pastedContents: Record<number, PastedContent>, setCursorOffset?: (offset: number) => void, currentMode?: HistoryMode): { |
| 64 | historyIndex: number; |
| 65 | setHistoryIndex: (index: number) => void; |
no test coverage detected