Fix vim plugin: cancel pending completion request when make a new completion request. (#114)

add-tracing
Zhiming Ma 2023-04-21 13:05:52 +08:00 committed by GitHub
parent b8730685e0
commit 3dedd49afc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -176,6 +176,7 @@ function! s:GetCompletion(id)
\ prompt: s:GetPrompt(),
\ language: s:GetLanguage(),
\ }],
\ cancelPendingRequest: v:true,
\ }, #{
\ callback: function('s:HandleCompletion', [a:id]),
\ })

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
import { TabbyClient } from "./TabbyClient";
import { getFunction } from "./utils";
import { CancelablePromise, ApiError } from "./generated";
import { CancelablePromise, CancelError, ApiError } from "./generated";
const tabby = TabbyClient.getInstance();
interface VimRequest {
@ -8,6 +8,7 @@ interface VimRequest {
1: {
func: string;
args?: any;
cancelPendingRequest?: boolean; // cancel pending request that called the same API
};
}
@ -16,6 +17,8 @@ interface VimResponse {
1: any; // Response data
}
const pendingPromise : { [key: string]: CancelablePromise<any> } = {};
process.stdin.on("data", async (data) => {
try {
const req: VimRequest = JSON.parse(data.toString());
@ -26,16 +29,24 @@ process.stdin.on("data", async (data) => {
const result = func(...args);
if (result instanceof CancelablePromise && req[1].func.startsWith("api.")) {
// Async API calls
if (req[1].cancelPendingRequest && pendingPromise[req[1].func]) {
pendingPromise[req[1].func].cancel();
}
pendingPromise[req[1].func] = result;
resp[1] = await result
.then((response: any) => {
tabby.changeStatus("ready");
return response;
})
.catch((e: CancelError) => {
return null;
})
.catch((e: ApiError) => {
process.stderr.write(JSON.stringify(e, Object.getOwnPropertyNames(e)) + "\n");
tabby.changeStatus("disconnected");
return null;
});
pendingPromise[req[1].func] = null;
} else if (result instanceof Promise) {
// Async calls (non-API)
resp[1] = await result;