| 1 | import QuickLRU from "quick-lru"; |
| 2 | |
| 3 | export interface prevEdit { |
| 4 | unidiff: string; |
| 5 | fileUri: string; |
| 6 | workspaceUri: string; |
| 7 | timestamp: number; |
| 8 | } |
| 9 | const maxPrevEdits = 5; |
| 10 | |
| 11 | export const prevEditLruCache = new QuickLRU<string, prevEdit>({ |
| 12 | maxSize: maxPrevEdits, |
| 13 | }); |
| 14 | |
| 15 | export const setPrevEdit = (edit: prevEdit): void => { |
| 16 | const uniqueSuffix = Math.random().toString(36).substring(2, 8); |
| 17 | const key = `${edit.fileUri}:${edit.timestamp}:${uniqueSuffix}`; |
| 18 | prevEditLruCache.set(key, edit); |
| 19 | }; |
| 20 | |
| 21 | export const getPrevEditsDescending = (): prevEdit[] => { |
| 22 | const edits: prevEdit[] = []; |
| 23 | for (const [_, edit] of prevEditLruCache.entriesDescending()) { |
| 24 | if (edits.length >= maxPrevEdits) { |
| 25 | break; |
| 26 | } |
| 27 | edits.push(edit); |
| 28 | } |
| 29 | return edits; |
| 30 | }; |