File tree Expand file tree Collapse file tree 4 files changed +27
-9
lines changed
Expand file tree Collapse file tree 4 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -14,14 +14,15 @@ import { AuthUtil } from '../util/authUtil'
1414import { InsertedCode } from '../../codewhispererChat/controllers/chat/model'
1515import { codeWhispererClient } from '../client/codewhisperer'
1616import { 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 */
2223export 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 }
Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ import * as timeoutUtils from '../../../shared/utilities/timeoutUtils'
99import { installFakeClock , tickPromise } from '../../../test/testUtil'
1010import { 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 ( ) {
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments