Skip to content

Commit fadc65c

Browse files
web: minor Timer/Timeout change for CW (aws#5258)
* web: change timeout for web compatiblility Problem: Timer in this class does not work in web mode since it only works with the Node timeout class. Solution: Use the generalized Timeout class. Signed-off-by: Nikolas Komonen <[email protected]> * test: copy timeout tests to web tests This is copying the node timeout tests to web mode. Since they behave the same, no modifications needed to be done to the tests. - Also fix an issue where some code was not being run in web mode during refresh. Signed-off-by: Nikolas Komonen <[email protected]> --------- Signed-off-by: Nikolas Komonen <[email protected]>
1 parent 198d2c9 commit fadc65c

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

packages/core/src/codewhisperer/tracker/codewhispererTracker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import { AuthUtil } from '../util/authUtil'
1414
import { InsertedCode } from '../../codewhispererChat/controllers/chat/model'
1515
import { codeWhispererClient } from '../client/codewhisperer'
1616
import { logSendTelemetryEventFailure } from '../../codewhispererChat/controllers/chat/telemetryHelper'
17+
import { Timeout } from '../../shared/utilities/timeoutUtils'
1718

1819
/**
1920
* This singleton class is mainly used for calculating the percentage of user modification.
2021
* The current calculation method is (Levenshtein edit distance / acceptedSuggestion.length).
2122
*/
2223
export class CodeWhispererTracker {
2324
private _eventQueue: (AcceptedSuggestionEntry | InsertedCode)[]
24-
private _timer?: NodeJS.Timer
25+
private _timer?: Timeout
2526
private static instance: CodeWhispererTracker
2627

2728
/**
@@ -158,21 +159,22 @@ export class CodeWhispererTracker {
158159

159160
public async startTimer() {
160161
if (!this._timer) {
161-
this._timer = setTimeout(async () => {
162+
this._timer = new Timeout(CodeWhispererTracker.defaultCheckPeriodMillis)
163+
this._timer.onCompletion(async () => {
162164
try {
163165
await this.flush()
164166
} finally {
165167
if (this._timer !== undefined) {
166168
this._timer!.refresh()
167169
}
168170
}
169-
}, CodeWhispererTracker.defaultCheckPeriodMillis)
171+
})
170172
}
171173
}
172174

173175
public closeTimer() {
174176
if (this._timer !== undefined) {
175-
clearTimeout(this._timer)
177+
this._timer.cancel()
176178
this._timer = undefined
177179
}
178180
}

packages/core/src/shared/utilities/timeoutUtils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ export class Timeout {
101101
globals.clock.clearTimeout(this._timerTimeout)
102102
this._timerTimeout = this.createTimeout()
103103
} else {
104-
// These will not align, but we don't have visibility into a NodeJS.Timeout
105-
// so remainingtime will be approximate. Timers are approximate anyway and are
106-
// not highly accurate in when they fire.
107-
this._endTime = globals.clock.Date.now() + this._timeoutLength
104+
// This is a node timeout instance, which has refresh built in
108105
this._timerTimeout = this._timerTimeout.refresh()
109106
}
107+
108+
// These will not align, but we don't have visibility into a NodeJS.Timeout
109+
// so remainingtime will be approximate. Timers are approximate anyway and are
110+
// not highly accurate in when they fire.
111+
this._endTime = globals.clock.Date.now() + this._timeoutLength
110112
}
111113

112114
/**

packages/core/src/test/shared/utilities/timeoutUtils.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import * as timeoutUtils from '../../../shared/utilities/timeoutUtils'
99
import { installFakeClock, tickPromise } from '../../../test/testUtil'
1010
import { sleep } from '../../../shared/utilities/timeoutUtils'
1111

12-
describe('timeoutUtils', async function () {
12+
// We export this describe() so it can be used in the web tests as well
13+
export const timeoutUtilsDescribe = describe('timeoutUtils', async function () {
1314
let clock: FakeTimers.InstalledClock
1415

1516
before(function () {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* IMPORTANT: We are importing the same test from the node timeoutUtils
8+
* since the behavior should be the exact same.
9+
*
10+
* Any web specific tests should be made within their own `describe()`.
11+
*/
12+
import { timeoutUtilsDescribe } from '../../../test/shared/utilities/timeoutUtils.test'
13+
timeoutUtilsDescribe

0 commit comments

Comments
 (0)