|
| 1 | +import { RangeInFile } from ".."; |
| 2 | +import { NextEditProvider } from "./NextEditProvider"; |
| 3 | +import { NextEditOutcome } from "./types"; |
| 4 | + |
| 5 | +interface ProcessedItem { |
| 6 | + location: RangeInFile; |
| 7 | + outcome: NextEditOutcome; // Result from the model |
| 8 | +} |
| 9 | + |
| 10 | +export class PrefetchQueue { |
| 11 | + private static instance: PrefetchQueue | null = null; |
| 12 | + |
| 13 | + private unprocessedQueue: RangeInFile[] = []; |
| 14 | + private processedQueue: ProcessedItem[] = []; |
| 15 | + private prefetchLimit: number; |
| 16 | + private abortController: AbortController; |
| 17 | + |
| 18 | + private usingFullFileDiff: boolean = true; |
| 19 | + |
| 20 | + private constructor(prefetchLimit: number = 3) { |
| 21 | + this.prefetchLimit = prefetchLimit; |
| 22 | + this.abortController = new AbortController(); |
| 23 | + } |
| 24 | + |
| 25 | + public static getInstance(prefetchLimit: number = 3): PrefetchQueue { |
| 26 | + if (!PrefetchQueue.instance) { |
| 27 | + PrefetchQueue.instance = new PrefetchQueue(prefetchLimit); |
| 28 | + } |
| 29 | + |
| 30 | + return PrefetchQueue.instance; |
| 31 | + } |
| 32 | + |
| 33 | + initialize(usingFullFileDiff: boolean) { |
| 34 | + this.usingFullFileDiff = usingFullFileDiff; |
| 35 | + } |
| 36 | + |
| 37 | + // Queue management methods |
| 38 | + enqueueUnprocessed(location: RangeInFile): void { |
| 39 | + this.unprocessedQueue.push(location); |
| 40 | + } |
| 41 | + |
| 42 | + private dequeueUnprocessed(): RangeInFile | undefined { |
| 43 | + return this.unprocessedQueue.shift(); |
| 44 | + } |
| 45 | + |
| 46 | + enqueueProcessed(item: ProcessedItem): void { |
| 47 | + this.processedQueue.push(item); |
| 48 | + } |
| 49 | + |
| 50 | + dequeueProcessed(): ProcessedItem | undefined { |
| 51 | + return this.processedQueue.shift(); |
| 52 | + } |
| 53 | + |
| 54 | + // Process items from unprocessed queue |
| 55 | + async process(ctx: any): Promise<void> { |
| 56 | + while ( |
| 57 | + this.unprocessedQueue.length > 0 && |
| 58 | + this.processedQueue.length < this.prefetchLimit && |
| 59 | + !this.abortController.signal.aborted |
| 60 | + ) { |
| 61 | + const location = this.dequeueUnprocessed(); |
| 62 | + if (!location) break; |
| 63 | + |
| 64 | + try { |
| 65 | + const outcome = |
| 66 | + await NextEditProvider.getInstance().provideInlineCompletionItemsWithChain( |
| 67 | + ctx, |
| 68 | + location, |
| 69 | + this.abortController.signal, |
| 70 | + this.usingFullFileDiff, |
| 71 | + ); |
| 72 | + |
| 73 | + if (!outcome) continue; |
| 74 | + |
| 75 | + this.enqueueProcessed({ |
| 76 | + location, |
| 77 | + outcome, |
| 78 | + }); |
| 79 | + } catch (error) { |
| 80 | + if (!this.abortController.signal.aborted) { |
| 81 | + // Handle error |
| 82 | + console.error("Error processing item:", error); |
| 83 | + } |
| 84 | + // If aborted, we just stop processing |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Abort all operations |
| 91 | + abort(): void { |
| 92 | + this.abortController.abort(); |
| 93 | + this.clear(); |
| 94 | + |
| 95 | + // Create a new AbortController for future operations |
| 96 | + this.abortController = new AbortController(); |
| 97 | + } |
| 98 | + |
| 99 | + // Clear all queues |
| 100 | + clear(): void { |
| 101 | + this.unprocessedQueue = []; |
| 102 | + this.processedQueue = []; |
| 103 | + } |
| 104 | + |
| 105 | + // Additional helper methods |
| 106 | + get unprocessedCount(): number { |
| 107 | + return this.unprocessedQueue.length; |
| 108 | + } |
| 109 | + |
| 110 | + get processedCount(): number { |
| 111 | + return this.processedQueue.length; |
| 112 | + } |
| 113 | + |
| 114 | + peekProcessed(): ProcessedItem | undefined { |
| 115 | + return this.processedQueue[0]; |
| 116 | + } |
| 117 | + |
| 118 | + setPreetchLimit(limit: number): void { |
| 119 | + this.prefetchLimit = limit; |
| 120 | + } |
| 121 | +} |
0 commit comments