Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/base/pausableThrottledWorker.ts
13388 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
import { IThrottledWorkerOptions, ThrottledWorker } from '../../src/util/vs/base/common/async';
6
7
/**
8
* A ThrottledWorker that supports pausing and resuming work processing.
9
* When paused, work items will still be buffered but not processed until resumed.
10
* Work that was in progress when paused will be completed before pausing takes effect.
11
*/
12
export class PausableThrottledWorker<T> extends ThrottledWorker<T> {
13
private _paused: boolean = false;
14
private _pausedWork: T[] = [];
15
16
constructor(options: IThrottledWorkerOptions, handler: (units: T[]) => void) {
17
super(options, (units: T[]) => {
18
if (this._paused) {
19
// If paused, store the work for later
20
this._pausedWork.push(...units);
21
} else {
22
handler(units);
23
}
24
});
25
}
26
27
/**
28
* Whether the worker is currently paused
29
*/
30
isPaused(): boolean {
31
return this._paused;
32
}
33
34
/**
35
* Pause processing of work items. Any work items received while paused
36
* will be buffered until resume() is called.
37
*/
38
pause(): void {
39
this._paused = true;
40
}
41
42
/**
43
* Resume processing of work items, including any that were buffered
44
* while the worker was paused.
45
*/
46
resume(): void {
47
this._paused = false;
48
49
// Process any work that was buffered while paused
50
if (this._pausedWork.length > 0) {
51
const work = this._pausedWork;
52
this._pausedWork = [];
53
this.work(work);
54
}
55
}
56
57
override dispose(): void {
58
this._pausedWork = [];
59
super.dispose();
60
}
61
}
62