This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#18 Implement debounce with proper async support
- Loading branch information
Showing
5 changed files
with
173 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import assert = require("assert"); | ||
import { FauxpilotCompletionProvider } from "../../FauxpilotCompletionProvider"; | ||
import { CancellationToken, InlineCompletionContext, InlineCompletionItem, InlineCompletionItemProvider, InlineCompletionList, Position, ProviderResult, Range, TextDocument, workspace } from 'vscode'; | ||
import { AxiosResponse } from 'axios'; | ||
import { CreateCompletionResponse } from "openai"; | ||
|
||
suite('provideInlineCompletionItems', () => { | ||
test('Normal completion', async () => { | ||
const provider = new FauxpilotCompletionProvider(testCompletion([{ text: "Example response" }])); | ||
const result = await provider.provideInlineCompletionItems( | ||
documentStub("Example prompt"), | ||
positionStub(), | ||
null as any, | ||
null as any | ||
); | ||
assert.equal((result as any)[0].insertText, "Example response"); | ||
}); | ||
test('Debounced completion', async () => { | ||
// Rewrite as before/after each | ||
let output: any[] = []; | ||
const originalLog = console.log; | ||
const originalDebug = console.debug; | ||
console.log = (message?: any, ...optional: any[]) => { | ||
output.push([message, ...optional]); | ||
originalLog(message, ...optional); | ||
}; | ||
console.debug = (message?: any, ...optional: any[]) => { | ||
output.push([message, ...optional]); | ||
originalDebug(message, ...optional); | ||
}; | ||
|
||
const provider = new FauxpilotCompletionProvider(testCompletion([{ text: "Example response" }])); | ||
(provider.provideInlineCompletionItems( | ||
documentStub("Example prompt 1"), | ||
positionStub(), | ||
null as any, | ||
null as any | ||
) as Promise<any>).then(console.debug); | ||
const result = await provider.provideInlineCompletionItems( | ||
documentStub("Example prompt 2"), | ||
positionStub(), | ||
null as any, | ||
null as any | ||
); | ||
|
||
console.debug = originalDebug; | ||
console.log = originalLog; | ||
|
||
assert.equal((result as any)[0].insertText, "Example response"); | ||
assert.deepEqual(output, [ | ||
[ | ||
"Requesting completion for prompt", | ||
"Example prompt 1" | ||
], | ||
[ | ||
"Requesting completion for prompt", | ||
"Example prompt 2" | ||
], | ||
[ | ||
"Resolved previous debounce with defaults" | ||
], | ||
[ | ||
[] | ||
], | ||
[ | ||
"Resolved debounce" | ||
], | ||
[ | ||
"Requesting completion after debounce period" | ||
], | ||
[ | ||
"Calling OpenAi", | ||
"Example prompt 2" | ||
], | ||
[ | ||
"Calling OpenAi with stop words = ", | ||
["\n"] | ||
] | ||
]); | ||
}); | ||
}); | ||
|
||
function positionStub(): Position { | ||
return { | ||
line: 0, | ||
character: 0 | ||
} as any; | ||
} | ||
|
||
function documentStub(out?: any): TextDocument { | ||
return { | ||
getText: () => out | ||
} as any; | ||
} | ||
|
||
function testCompletion(choices: { text: string }[]) { | ||
return { | ||
createCompletion: async (params: any): Promise<AxiosResponse<CreateCompletionResponse, any>> => { | ||
console.warn("DEBUG COMPLETION", params); | ||
const result: CreateCompletionResponse = { | ||
choices | ||
}; | ||
return { | ||
data: result | ||
} as AxiosResponse; | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function debounce<T>(fn: (ths: any, ...args: any[]) => Promise<T>, | ||
{ timeout = 300, defaultReturn }: { timeout: number, defaultReturn?: T }) { | ||
let timer: NodeJS.Timeout; | ||
let previousPromise: any; | ||
return async (...args: any[]) => { | ||
// Resolve any previous pending promises, so that we will never leave | ||
// them dangling | ||
// TODO: Extract debug logging wrapper | ||
previousPromise?.((() => { | ||
console.debug("Resolved previous debounce with defaults"); | ||
return defaultReturn; | ||
})()); | ||
clearTimeout(timer); | ||
return new Promise(resolve => { | ||
// Add previous promise, so that we can resolve it with empty upon the | ||
// next (debounced) call | ||
previousPromise = resolve; | ||
timer = setTimeout(() => { | ||
// TODO: Extract debug logging wrapper | ||
resolve((() => { | ||
console.debug("Resolved debounce"); | ||
// Because we are actually calling the API, we must resolved | ||
// all previous debounced calls with empty, so we ensure that | ||
// there is no dangling resolved promise that would be called | ||
// during the next debounced call | ||
previousPromise = undefined; | ||
// @ts-ignore | ||
return fn.apply(this, args); | ||
})()); | ||
}, timeout); | ||
}); | ||
}; | ||
} |