Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/inlineEdits/common/delay.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
7
export class DelaySession {
8
private extraDebounce = 0;
9
10
constructor(
11
private baseDebounceTime: number,
12
private expectedTotalTime: number | undefined,
13
private readonly providerInvocationTime: number = Date.now(),
14
) {
15
}
16
17
public setExtraDebounce(extraDebounce: number): void {
18
this.extraDebounce = extraDebounce;
19
}
20
21
public setBaseDebounceTime(baseDebounceTime: number): void {
22
this.baseDebounceTime = baseDebounceTime;
23
}
24
25
public setExpectedTotalTime(expectedTotalTime: number): void {
26
this.expectedTotalTime = expectedTotalTime;
27
}
28
29
getDebounceTime() {
30
const expectedDebounceTime = this.expectedTotalTime === undefined
31
? this.baseDebounceTime
32
: Math.min(this.baseDebounceTime, this.expectedTotalTime);
33
34
const expectedDebounceTimeWithExtras = expectedDebounceTime + this.extraDebounce;
35
36
const timeAlreadySpent = Date.now() - this.providerInvocationTime;
37
const actualDebounceTime = Math.max(0, expectedDebounceTimeWithExtras - timeAlreadySpent);
38
39
return actualDebounceTime;
40
}
41
42
getArtificialDelay() {
43
if (this.expectedTotalTime === undefined) {
44
return 0;
45
}
46
47
const timeAlreadySpent = Date.now() - this.providerInvocationTime;
48
const delay = Math.max(0, this.expectedTotalTime - timeAlreadySpent);
49
return delay;
50
}
51
}
52
53