diff --git a/src/vs/base/common/async.ts b/src/vs/base/common/async.ts index 9eff176c713..90e4c4ffdd6 100644 --- a/src/vs/base/common/async.ts +++ b/src/vs/base/common/async.ts @@ -349,13 +349,14 @@ export function always(promise: TPromise, f: Function): TPromise { * Runs the provided list of promise factories in sequential order. The returned * promise will complete to an array of results from each promise. */ -export function sequence(promiseFactories: ITask>[]): TPromise { + +export function sequence(promiseFactories: ITask>[]): TPromise { const results: T[] = []; // reverse since we start with last element using pop() promiseFactories = promiseFactories.reverse(); - function next(): Promise { + function next(): Thenable { if (promiseFactories.length) { return promiseFactories.pop()(); } @@ -363,7 +364,7 @@ export function sequence(promiseFactories: ITask>[]): TPromise { if (result !== undefined && result !== null) { results.push(result); } @@ -692,4 +693,4 @@ export class ThrottledEmitter extends Emitter { this.hasLastEvent = false; this.lastEvent = void 0; } -} \ No newline at end of file +} diff --git a/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts b/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts index 5c6e9b6f321..90ee7c35ba9 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadSaveParticipant.ts @@ -5,7 +5,6 @@ 'use strict'; -import { TPromise } from 'vs/base/common/winjs.base'; import { sequence } from 'vs/base/common/async'; import * as strings from 'vs/base/common/strings'; import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService'; @@ -175,7 +174,7 @@ class FormatOnSaveParticipant implements ISaveParticipant { // Nothing } - participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): TPromise { + participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): Promise { const model = editorModel.textEditorModel; if (env.reason === SaveReason.AUTO @@ -186,7 +185,7 @@ class FormatOnSaveParticipant implements ISaveParticipant { const versionNow = model.getVersionId(); const { tabSize, insertSpaces } = model.getOptions(); - return new TPromise((resolve, reject) => { + return new Promise((resolve, reject) => { setTimeout(reject, 750); getDocumentFormattingEdits(model, { tabSize, insertSpaces }) .then(edits => this._editorWorkerService.computeMoreMinimalEdits(model.uri, edits)) @@ -241,13 +240,13 @@ class ExtHostSaveParticipant implements ISaveParticipant { this._proxy = extHostContext.get(ExtHostContext.ExtHostDocumentSaveParticipant); } - participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): TPromise { - return new TPromise((resolve, reject) => { + participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): Promise { + return new Promise((resolve, reject) => { setTimeout(reject, 1750); this._proxy.$participateInSave(editorModel.getResource(), env.reason).then(values => { for (const success of values) { if (!success) { - return TPromise.wrapError(new Error('listener failed')); + return Promise.reject(new Error('listener failed')); } } return undefined; @@ -286,9 +285,9 @@ export class SaveParticipant implements ISaveParticipant { TextFileEditorModel.setSaveParticipant(undefined); } - participate(model: ITextFileEditorModel, env: { reason: SaveReason }): TPromise { + participate(model: ITextFileEditorModel, env: { reason: SaveReason }): Thenable { const promiseFactory = this._saveParticipants.map(p => () => { - return TPromise.as(p.participate(model, env)); + return Promise.resolve(p.participate(model, env)); }); return sequence(promiseFactory).then(() => { }); diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 729372242e2..9dbf881de70 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -462,7 +462,7 @@ export interface ExtHostDocumentsShape { } export interface ExtHostDocumentSaveParticipantShape { - $participateInSave(resource: URI, reason: SaveReason): TPromise; + $participateInSave(resource: URI, reason: SaveReason): Thenable; } export interface ITextEditorAddData { diff --git a/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts b/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts index 607c8c27b4d..479ac970cd4 100644 --- a/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts +++ b/src/vs/workbench/api/node/extHostDocumentSaveParticipant.ts @@ -8,7 +8,6 @@ import Event from 'vs/base/common/event'; import URI from 'vs/base/common/uri'; import { sequence, always } from 'vs/base/common/async'; import { illegalState } from 'vs/base/common/errors'; -import { TPromise } from 'vs/base/common/winjs.base'; import { ExtHostDocumentSaveParticipantShape, MainThreadEditorsShape, IWorkspaceResourceEdit } from 'vs/workbench/api/node/extHost.protocol'; import { TextEdit } from 'vs/workbench/api/node/extHostTypes'; import { fromRange, TextDocumentSaveReason, EndOfLine } from 'vs/workbench/api/node/extHostTypeConverters'; @@ -46,7 +45,7 @@ export class ExtHostDocumentSaveParticipant implements ExtHostDocumentSavePartic }; } - $participateInSave(resource: URI, reason: SaveReason): TPromise { + $participateInSave(resource: URI, reason: SaveReason): Thenable { const entries = this._callbacks.toArray(); let didTimeout = false; @@ -68,11 +67,11 @@ export class ExtHostDocumentSaveParticipant implements ExtHostDocumentSavePartic return always(promise, () => clearTimeout(didTimeoutHandle)); } - private _deliverEventAsyncAndBlameBadListeners(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): TPromise { + private _deliverEventAsyncAndBlameBadListeners(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): Promise { const errors = this._badListeners.get(listener); if (errors > this._thresholds.errors) { // bad listener - ignore - return TPromise.wrap(false); + return Promise.resolve(false); } return this._deliverEventAsync(listener, thisArg, stubEvent).then(() => { @@ -93,9 +92,9 @@ export class ExtHostDocumentSaveParticipant implements ExtHostDocumentSavePartic }); } - private _deliverEventAsync(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): TPromise { + private _deliverEventAsync(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): Promise { - const promises: TPromise[] = []; + const promises: Promise[] = []; const { document, reason } = stubEvent; const { version } = document; @@ -107,7 +106,7 @@ export class ExtHostDocumentSaveParticipant implements ExtHostDocumentSavePartic if (Object.isFrozen(promises)) { throw illegalState('waitUntil can not be called async'); } - promises.push(TPromise.wrap(p)); + promises.push(Promise.resolve(p)); } }); @@ -115,16 +114,23 @@ export class ExtHostDocumentSaveParticipant implements ExtHostDocumentSavePartic // fire event listener.apply(thisArg, [event]); } catch (err) { - return TPromise.wrapError(err); + return Promise.reject(err); } // freeze promises after event call Object.freeze(promises); - return new TPromise((resolve, reject) => { + return new Promise((resolve, reject) => { // join on all listener promises, reject after timeout const handle = setTimeout(() => reject(new Error('timeout')), this._thresholds.timeout); - return always(TPromise.join(promises), () => clearTimeout(handle)).then(resolve, reject); + + return Promise.all(promises).then(edits => { + clearTimeout(handle); + resolve(edits); + }).catch(err => { + clearTimeout(handle); + reject(err); + }); }).then(values => { @@ -156,7 +162,7 @@ export class ExtHostDocumentSaveParticipant implements ExtHostDocumentSavePartic } // TODO@joh bubble this to listener? - return TPromise.wrapError(new Error('concurrent_edits')); + return Promise.reject(new Error('concurrent_edits')); }); } } diff --git a/src/vs/workbench/test/electron-browser/api/extHostDocumentSaveParticipant.test.ts b/src/vs/workbench/test/electron-browser/api/extHostDocumentSaveParticipant.test.ts index 06167349726..d937c8af433 100644 --- a/src/vs/workbench/test/electron-browser/api/extHostDocumentSaveParticipant.test.ts +++ b/src/vs/workbench/test/electron-browser/api/extHostDocumentSaveParticipant.test.ts @@ -128,7 +128,7 @@ suite('ExtHostDocumentSaveParticipant', () => { }); }); - test('event delivery, ignore bad listeners', () => { + test('event delivery, ignore bad listeners', async () => { const participant = new ExtHostDocumentSaveParticipant(documents, mainThreadEditors, { timeout: 5, errors: 1 }); let callCount = 0; @@ -137,16 +137,13 @@ suite('ExtHostDocumentSaveParticipant', () => { throw new Error('boom'); }); - return TPromise.join([ - participant.$participateInSave(resource, SaveReason.EXPLICIT), - participant.$participateInSave(resource, SaveReason.EXPLICIT), - participant.$participateInSave(resource, SaveReason.EXPLICIT), - participant.$participateInSave(resource, SaveReason.EXPLICIT) + await participant.$participateInSave(resource, SaveReason.EXPLICIT); + await participant.$participateInSave(resource, SaveReason.EXPLICIT); + await participant.$participateInSave(resource, SaveReason.EXPLICIT); + await participant.$participateInSave(resource, SaveReason.EXPLICIT); - ]).then(values => { - sub.dispose(); - assert.equal(callCount, 2); - }); + sub.dispose(); + assert.equal(callCount, 2); }); test('event delivery, overall timeout', () => {