Skip to content

Commit e4d5adb

Browse files
authored
activity: fix activity API is active although there is no any user interactions (microsoft#248031)
Fixes microsoft#237386
1 parent 62884eb commit e4d5adb

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/vs/workbench/services/progress/browser/progressService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class ProgressService extends Disposable implements IProgressService {
4747
const { location } = options;
4848

4949
const task = async (progress: IProgress<IProgressStep>) => {
50-
const activeLock = this.userActivityService.markActive({ whenHeldFor: 15_000 });
50+
const activeLock = this.userActivityService.markActive({ extendOnly: true, whenHeldFor: 15_000 });
5151
try {
5252
return await originalTask(progress);
5353
} finally {

src/vs/workbench/services/userActivity/common/userActivityService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import { userActivityRegistry } from './userActivityRegistry.js';
1212

1313
export interface IMarkActiveOptions {
1414
whenHeldFor?: number;
15+
/**
16+
* Only consider this progress if the state is already active. Used to avoid
17+
* background work from incorrectly marking the user as active (#237386)
18+
*/
19+
extendOnly?: boolean;
1520
}
1621

1722
/**
@@ -71,6 +76,10 @@ export class UserActivityService extends Disposable implements IUserActivityServ
7176

7277
/** @inheritdoc */
7378
markActive(opts?: IMarkActiveOptions): IDisposable {
79+
if (opts?.extendOnly && !this.isActive) {
80+
return Disposable.None;
81+
}
82+
7483
if (opts?.whenHeldFor) {
7584
const store = new DisposableStore();
7685
store.add(disposableTimeout(() => store.add(this.markActive()), opts.whenHeldFor));

src/vs/workbench/services/userActivity/test/common/userActivityService.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,29 @@ suite('UserActivityService', () => {
7171
clock.tick(duration + MARK_INACTIVE_DEBOUNCE);
7272
assert.strictEqual(userActivityService.isActive, false);
7373
});
74+
75+
test('markActive with extendOnly only extends if already active', () => {
76+
// Make user inactive
77+
userActivityService.markActive().dispose();
78+
clock.tick(MARK_INACTIVE_DEBOUNCE);
79+
assert.strictEqual(userActivityService.isActive, false);
80+
81+
// Should not activate if inactive and extendOnly is true
82+
const handle = userActivityService.markActive({ extendOnly: true });
83+
assert.strictEqual(userActivityService.isActive, false);
84+
handle.dispose();
85+
86+
// Activate normally
87+
const h1 = userActivityService.markActive();
88+
assert.strictEqual(userActivityService.isActive, true);
89+
90+
// Should extend activity if already active
91+
const h2 = userActivityService.markActive({ extendOnly: true });
92+
h1.dispose();
93+
// Still active because h2 is holding
94+
assert.strictEqual(userActivityService.isActive, true);
95+
h2.dispose();
96+
clock.tick(MARK_INACTIVE_DEBOUNCE);
97+
assert.strictEqual(userActivityService.isActive, false);
98+
});
7499
});

0 commit comments

Comments
 (0)