Skip to content

Commit

Permalink
fix: improve copilot debounce logic (#2249)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick authored Sep 6, 2024
1 parent 56aab3b commit 176c1dd
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions frontend/src/core/codemirror/copilot/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,23 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
params: CopilotGetCompletionsParams,
version: number,
): Promise<CopilotGetCompletionsResult> {
// Start a loading indicator
setGitHubCopilotLoadingVersion(version);
const response = await this._request("getCompletions", {
doc: {
...params.doc,
version: version,
},
});
// Stop the loading indicator (only if the version hasn't changed)
clearGitHubCopilotLoadingVersion(version);

// If the document version has changed since the request was made, return an empty response
if (version !== this.documentVersion) {
return { completions: [] };
}

return response;
}

// Even though the copilot extension has a debounce,
// there are multiple requests sent at the same time
// when multiple Codemirror instances are mounted at the same time.
// So we only need to debounce around 10ms.
private debouncedGetCompletionInternal = debounce(
this.getCompletionInternal.bind(this),
300,
10,
);

async getCompletion(
Expand All @@ -178,7 +170,17 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
return { completions: [] };
}

// Start a loading indicator
setGitHubCopilotLoadingVersion(version);
const response = await this.debouncedGetCompletionInternal(params, version);
// Stop the loading indicator (only if the version hasn't changed)
clearGitHubCopilotLoadingVersion(version);

// If the document version has changed since the request was made, return an empty response
if (version !== this.documentVersion) {
return { completions: [] };
}

return response || { completions: [] };
}
}

0 comments on commit 176c1dd

Please sign in to comment.