/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ 'use strict'; import { Progress, CancellationToken } from 'vscode'; import { MainThreadProgressShape } from './extHost.protocol'; import { IExtensionDescription } from 'vs/platform/extensions/common/extensions'; export class ExtHostProgress { private _proxy: MainThreadProgressShape; private _handles: number = 0; constructor(proxy: MainThreadProgressShape) { this._proxy = proxy; } withWindowProgress(extension: IExtensionDescription, title: string, task: (progress: Progress, token: CancellationToken) => Thenable): Thenable { const handle = this._handles++; this._proxy.$startWindow(handle, title); return this._withProgress(handle, task); } withScmProgress(extension: IExtensionDescription, task: (progress: Progress) => Thenable): Thenable { const handle = this._handles++; this._proxy.$startScm(handle); return this._withProgress(handle, task); } private _withProgress(handle: number, task: (progress: Progress, token: CancellationToken) => Thenable): Thenable { const progress = { report: (message: string) => { this._proxy.$progressReport(handle, message); } }; let p: Thenable; try { p = task(progress, null); } catch (err) { this._proxy.$progressEnd(handle); throw err; } return p.then(result => { this._proxy.$progressEnd(handle); return result; }, err => { this._proxy.$progressEnd(handle); throw err; }); } }