p-doom/crowd-code-dataset-0.2 huggingface dataset:
[content] src/extension.ts (typescript)
// Zed-inspired prediction scheduling:
// - Small debounce for rapid edits/cursor moves.
// - Throttle so we don't hit the model more often than every 300ms.
// - Tiny in-memory queue (max 2) where older queued requests are cancelled.
const PREDICTION_DEBOUNCE_MS = 150;
const PREDICTION_THROTTLE_MS = 300;

type PendingPrediction = { id: number; timer: NodeJS.Timeout };

let nextQueuedPredictionId = 0;
let pendingPredictions: PendingPrediction[] = [];
const cancelledPredictionIds = new Set<number>();
let lastPredictionTimestamp: number | undefined;
[content] src/extension.ts (typescript)
function debounce<T extends (...args: any[]) => void>(fn: T, waitMs: number) {
	let timer: NodeJS.Timeout | undefined;
	return (...args: Parameters<T>) => {
		if (timer) { clearTimeout(timer); }
		timer = setTimeout(() => fn(...args), waitMs);
	};
}
[content] src/extension.ts (typescript)
/**
 * Schedule a model preview refresh, coalescing rapid editor events and
 * throttling how often we actually talk to the model. This mirrors Zed's
 * pattern of "editor decides when to refresh", while the provider enforces
 * a per-buffer throttle and cancels stale queued work.
 *
 * - debounce = true  → called from implicit events (typing, cursor moves).
 * - userRequested = true → explicit commands should bypass most gating.
 */
function schedulePredictionRefresh(debounce: boolean, userRequested: boolean): void {
	// Respect explicit "hide until something changes again".
	if (!userRequested && suppressAutoPreview) {
		return;
	}

	const editor = vscode.window.activeTextEditor;
	if (!editor) {
		hidePreviewUI();
		return;
	}

	// For automatic refreshes, only show when the window is focused and the
	// buffer is non-empty. Explicit user commands can override this.
	if (!userRequested) {
		if (!vscode.window.state.focused) {
			hidePreviewUI();
			return;
		}
		if (editor.document.getText().length === 0) {
			hidePreviewUI();
			return;
		}
	}

	const now = Date.now();
	const id = ++nextQueuedPredictionId;

	let delay = 0;
	if (debounce) {
		delay = Math.max(delay, PREDICTION_DEBOUNCE_MS);
	}
	if (lastPredictionTimestamp != null) {
		const elapsed = now - lastPredictionTimestamp;
		if (elapsed < PREDICTION_THROTTLE_MS) {
			delay = Math.max(delay, PREDICTION_THROTTLE_MS - elapsed);
		}
	}

	const timer = setTimeout(() => {
		// If this queued refresh was cancelled (because newer ones arrived),
		// exit without talking to the model.
		if (cancelledPredictionIds.has(id)) {
			cancelledPredictionIds.delete(id);
			return;
		}

		lastPredictionTimestamp = Date.now();
		pendingPredictions = pendingPredictions.filter(p => p.id !== id);

		// The actual "provider refresh": this will abort any in-flight request
		// and only apply results if they correspond to the latest request id.
		void autoShowNextAction();
	}, delay);

	pendingPredictions.push({ id, timer });

	// Keep at most two queued refreshes; cancel the oldest when a third arrives.
	if (pendingPredictions.length > 2) {
		const oldest = pendingPredictions.shift();
		if (oldest) {
			cancelledPredictionIds.add(oldest.id);
			clearTimeout(oldest.timer);
		}
	}
}
[content] src/extension.ts (typescript)
/**
 * Schedule a model preview refresh, coalescing rapid editor events and
 * throttling how often we actually talk to the model. This mirrors Zed's
 * pattern of "editor decides when to refresh", while the provider enforces
 * a per-buffer throttle and cancels stale queued work.
 *
 * - debounce = true  → called from implicit events (typing, cursor moves).
 * - userRequested = true → explicit commands should bypass most gating.
 */
function schedulePredictionRefresh(debounce: boolean, userRequested: boolean): void {
	// Respect explicit "hide until something changes again".
	if (!userRequested && suppressAutoPreview) {
		return;
	}

	const editor = vscode.window.activeTextEditor;
	if (!editor) {
		hidePreviewUI();
		return;
	}

	// For automatic refreshes, only show when the window is focused and the
	// buffer is non-empty. Explicit user commands can override this.
	if (!userRequested) {
		if (!vscode.window.state.focused) {
			hidePreviewUI();
			return;
		}
		if (editor.document.getText().length === 0) {
			hidePreviewUI();
			return;
		}
	}

	const now = Date.now();
	const id = ++nextQueuedPredictionId;

	let delay = 0;
	if (debounce) {
		delay = Math.max(delay, PREDICTION_DEBOUNCE_MS);
	}
	if (lastPredictionTimestamp !== null && lastPredictionTimestamp !== undefined) {
		const elapsed = now - lastPredictionTimestamp;
		if (elapsed < PREDICTION_THROTTLE_MS) {
			delay = Math.max(delay, PREDICTION_THROTTLE_MS - elapsed);
		}
	}

	const timer = setTimeout(() => {
		// If this queued refresh was cancelled (because newer ones arrived),
		// exit without talking to the model.
		if (cancelledPredictionIds.has(id)) {
			cancelledPredictionIds.delete(id);
			return;
		}

		lastPredictionTimestamp = Date.now();
		pendingPredictions = pendingPredictions.filter(p => p.id !== id);

		// The actual "provider refresh": this will abort any in-flight request
		// and only apply results if they correspond to the latest request id.
		void autoShowNextAction();
	}, delay);

	pendingPredictions.push({ id, timer });

	// Keep at most two queued refreshes; cancel the oldest when a third arrives.
	if (pendingPredictions.length > 2) {
		const oldest = pendingPredictions.shift();
		if (oldest) {
			cancelledPredictionIds.add(oldest.id);
			clearTimeout(oldest.timer);
		}
	}
}