UNPKG

@sussudio/platform

Version:

Internal APIs for VS Code's service injection the base services.

122 lines (121 loc) 4.11 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? (desc = Object.getOwnPropertyDescriptor(target, key)) : desc, d; if (typeof Reflect === 'object' && typeof Reflect.decorate === 'function') r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if ((d = decorators[i])) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); }; }; import { DeferredPromise } from '@sussudio/base/common/async.mjs'; import { CancellationTokenSource } from '@sussudio/base/common/cancellation.mjs'; import { Disposable, DisposableStore, toDisposable } from '@sussudio/base/common/lifecycle.mjs'; import { createDecorator } from '../../instantiation/common/instantiation.mjs'; export const IProgressService = createDecorator('progressService'); export const emptyProgressRunner = Object.freeze({ total() {}, worked() {}, done() {}, }); export class Progress { callback; static None = Object.freeze({ report() {} }); _value; get value() { return this._value; } constructor(callback) { this.callback = callback; } report(item) { this._value = item; this.callback(this._value); } } /** * RAII-style progress instance that allows imperative reporting and hides * once `dispose()` is called. */ let UnmanagedProgress = class UnmanagedProgress extends Disposable { deferred = new DeferredPromise(); reporter; lastStep; constructor(options, progressService) { super(); progressService.withProgress(options, (reporter) => { this.reporter = reporter; if (this.lastStep) { reporter.report(this.lastStep); } return this.deferred.p; }); this._register(toDisposable(() => this.deferred.complete())); } report(step) { if (this.reporter) { this.reporter.report(step); } else { this.lastStep = step; } } }; UnmanagedProgress = __decorate([__param(1, IProgressService)], UnmanagedProgress); export { UnmanagedProgress }; export class LongRunningOperation extends Disposable { progressIndicator; currentOperationId = 0; currentOperationDisposables = this._register(new DisposableStore()); currentProgressRunner; currentProgressTimeout; constructor(progressIndicator) { super(); this.progressIndicator = progressIndicator; } start(progressDelay) { // Stop any previous operation this.stop(); // Start new const newOperationId = ++this.currentOperationId; const newOperationToken = new CancellationTokenSource(); this.currentProgressTimeout = setTimeout(() => { if (newOperationId === this.currentOperationId) { this.currentProgressRunner = this.progressIndicator.show(true); } }, progressDelay); this.currentOperationDisposables.add(toDisposable(() => clearTimeout(this.currentProgressTimeout))); this.currentOperationDisposables.add(toDisposable(() => newOperationToken.cancel())); this.currentOperationDisposables.add( toDisposable(() => (this.currentProgressRunner ? this.currentProgressRunner.done() : undefined)), ); return { id: newOperationId, token: newOperationToken.token, stop: () => this.doStop(newOperationId), isCurrent: () => this.currentOperationId === newOperationId, }; } stop() { this.doStop(this.currentOperationId); } doStop(operationId) { if (this.currentOperationId === operationId) { this.currentOperationDisposables.clear(); } } } export const IEditorProgressService = createDecorator('editorProgressService');