diff snippet cache tests
This commit is contained in:
parent
dba54c90a1
commit
cec16dc7fc
3 changed files with 82 additions and 23 deletions
50
core/autocomplete/snippets/diffSnippetCache.test.ts
Normal file
50
core/autocomplete/snippets/diffSnippetCache.test.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { DiffSnippetsCache } from "./diffSnippetCache";
|
||||
|
||||
test("DiffSnippetsCache getInstance returns singleton instance", () => {
|
||||
const instance1 = DiffSnippetsCache.getInstance();
|
||||
const instance2 = DiffSnippetsCache.getInstance();
|
||||
expect(instance1).toBe(instance2);
|
||||
});
|
||||
|
||||
test("DiffSnippetsCache set stores value and returns it", () => {
|
||||
const cache = DiffSnippetsCache.getInstance();
|
||||
const timestamp = Date.now();
|
||||
const value = { test: "data" };
|
||||
|
||||
const result = cache.set(timestamp, value);
|
||||
expect(result).toBe(value);
|
||||
expect(cache.get(timestamp)).toBe(value);
|
||||
});
|
||||
|
||||
test("DiffSnippetsCache get returns undefined for non-existent timestamp", () => {
|
||||
const cache = DiffSnippetsCache.getInstance();
|
||||
const timestamp = Date.now();
|
||||
|
||||
expect(cache.get(timestamp + 1000)).toBeUndefined();
|
||||
});
|
||||
|
||||
test("DiffSnippetsCache set clears cache when timestamp changes", () => {
|
||||
const cache = DiffSnippetsCache.getInstance();
|
||||
const timestamp1 = Date.now();
|
||||
const timestamp2 = timestamp1 + 1000;
|
||||
const value1 = { test: "data1" };
|
||||
const value2 = { test: "data2" };
|
||||
|
||||
cache.set(timestamp1, value1);
|
||||
cache.set(timestamp2, value2);
|
||||
|
||||
expect(cache.get(timestamp1)).toBeUndefined();
|
||||
expect(cache.get(timestamp2)).toBe(value2);
|
||||
});
|
||||
|
||||
test("DiffSnippetsCache set maintains cache with same timestamp", () => {
|
||||
const cache = DiffSnippetsCache.getInstance();
|
||||
const timestamp = Date.now();
|
||||
const value1 = { test: "data1" };
|
||||
const value2 = { test: "data2" };
|
||||
|
||||
cache.set(timestamp, value1);
|
||||
cache.set(timestamp, value2);
|
||||
|
||||
expect(cache.get(timestamp)).toBe(value2);
|
||||
});
|
||||
28
core/autocomplete/snippets/diffSnippetCache.ts
Normal file
28
core/autocomplete/snippets/diffSnippetCache.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
export class DiffSnippetsCache {
|
||||
private static instance: DiffSnippetsCache;
|
||||
private cache: Map<number, any> = new Map();
|
||||
private lastTimestamp: number = 0;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): DiffSnippetsCache {
|
||||
if (!DiffSnippetsCache.instance) {
|
||||
DiffSnippetsCache.instance = new DiffSnippetsCache();
|
||||
}
|
||||
return DiffSnippetsCache.instance;
|
||||
}
|
||||
|
||||
public set<T>(timestamp: number, value: T): T {
|
||||
// Clear old cache entry if exists
|
||||
if (this.lastTimestamp !== timestamp) {
|
||||
this.cache.clear();
|
||||
}
|
||||
this.lastTimestamp = timestamp;
|
||||
this.cache.set(timestamp, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public get(timestamp: number): any | undefined {
|
||||
return this.cache.get(timestamp);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ import { findUriInDirs } from "../../util/uri";
|
|||
import { ContextRetrievalService } from "../context/ContextRetrievalService";
|
||||
import { GetLspDefinitionsFunction } from "../types";
|
||||
import { HelperVars } from "../util/HelperVars";
|
||||
import { DiffSnippetsCache } from "./diffSnippetCache";
|
||||
|
||||
import {
|
||||
AutocompleteClipboardSnippet,
|
||||
|
|
@ -23,35 +24,14 @@ export interface SnippetPayload {
|
|||
clipboardSnippets: AutocompleteClipboardSnippet[];
|
||||
}
|
||||
|
||||
function racePromise<T>(promise: Promise<T[]>): Promise<T[]> {
|
||||
function racePromise<T>(promise: Promise<T[]>, timeout = 100): Promise<T[]> {
|
||||
const timeoutPromise = new Promise<T[]>((resolve) => {
|
||||
setTimeout(() => resolve([]), 100);
|
||||
setTimeout(() => resolve([]), timeout);
|
||||
});
|
||||
|
||||
return Promise.race([promise, timeoutPromise]);
|
||||
}
|
||||
|
||||
class DiffSnippetsCache {
|
||||
private cache: Map<number, any> = new Map();
|
||||
private lastTimestamp: number = 0;
|
||||
|
||||
public set<T>(timestamp: number, value: T): T {
|
||||
// Clear old cache entry if exists
|
||||
if (this.lastTimestamp !== timestamp) {
|
||||
this.cache.clear();
|
||||
}
|
||||
this.lastTimestamp = timestamp;
|
||||
this.cache.set(timestamp, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public get(timestamp: number): any | undefined {
|
||||
return this.cache.get(timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
const diffSnippetsCache = new DiffSnippetsCache();
|
||||
|
||||
// Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems,
|
||||
// or they might separately track recently edited ranges)
|
||||
async function getIdeSnippets(
|
||||
|
|
@ -114,6 +94,7 @@ const getDiffSnippets = async (
|
|||
ide: IDE,
|
||||
ideInfo: IdeInfo | undefined,
|
||||
): Promise<AutocompleteDiffSnippet[]> => {
|
||||
const diffSnippetsCache = DiffSnippetsCache.getInstance();
|
||||
if (ideInfo?.ideType !== "vscode") {
|
||||
// Disabling for non-vscode IDEs for now
|
||||
// See https://github.com/continuedev/continue/issues/4130
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue