Path: blob/main/extensions/copilot/test/base/pausableThrottledWorker.ts
13388 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import { IThrottledWorkerOptions, ThrottledWorker } from '../../src/util/vs/base/common/async';56/**7* A ThrottledWorker that supports pausing and resuming work processing.8* When paused, work items will still be buffered but not processed until resumed.9* Work that was in progress when paused will be completed before pausing takes effect.10*/11export class PausableThrottledWorker<T> extends ThrottledWorker<T> {12private _paused: boolean = false;13private _pausedWork: T[] = [];1415constructor(options: IThrottledWorkerOptions, handler: (units: T[]) => void) {16super(options, (units: T[]) => {17if (this._paused) {18// If paused, store the work for later19this._pausedWork.push(...units);20} else {21handler(units);22}23});24}2526/**27* Whether the worker is currently paused28*/29isPaused(): boolean {30return this._paused;31}3233/**34* Pause processing of work items. Any work items received while paused35* will be buffered until resume() is called.36*/37pause(): void {38this._paused = true;39}4041/**42* Resume processing of work items, including any that were buffered43* while the worker was paused.44*/45resume(): void {46this._paused = false;4748// Process any work that was buffered while paused49if (this._pausedWork.length > 0) {50const work = this._pausedWork;51this._pausedWork = [];52this.work(work);53}54}5556override dispose(): void {57this._pausedWork = [];58super.dispose();59}60}6162