提交 5e840a6d 编写于 作者: J Johannes Rieken

eng - no TPromise in save participants

上级 d221a282
......@@ -349,13 +349,14 @@ export function always<T>(promise: TPromise<T>, f: Function): TPromise<T> {
* 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<T>(promiseFactories: ITask<TPromise<T>>[]): TPromise<T[]> {
export function sequence<T>(promiseFactories: ITask<Thenable<T>>[]): TPromise<T[]> {
const results: T[] = [];
// reverse since we start with last element using pop()
promiseFactories = promiseFactories.reverse();
function next(): Promise {
function next(): Thenable<any> {
if (promiseFactories.length) {
return promiseFactories.pop()();
}
......@@ -363,7 +364,7 @@ export function sequence<T>(promiseFactories: ITask<TPromise<T>>[]): TPromise<T[
return null;
}
function thenHandler(result: any): Promise {
function thenHandler(result: any): Thenable<any> {
if (result !== undefined && result !== null) {
results.push(result);
}
......@@ -692,4 +693,4 @@ export class ThrottledEmitter<T> extends Emitter<T> {
this.hasLastEvent = false;
this.lastEvent = void 0;
}
}
\ No newline at end of file
}
......@@ -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<void> {
participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): Promise<void> {
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<ISingleEditOperation[]>((resolve, reject) => {
return new Promise<ISingleEditOperation[]>((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<void> {
return new TPromise<any>((resolve, reject) => {
participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): Promise<void> {
return new Promise<any>((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<void> {
participate(model: ITextFileEditorModel, env: { reason: SaveReason }): Thenable<void> {
const promiseFactory = this._saveParticipants.map(p => () => {
return TPromise.as(p.participate(model, env));
return Promise.resolve(p.participate(model, env));
});
return sequence(promiseFactory).then(() => { });
......
......@@ -462,7 +462,7 @@ export interface ExtHostDocumentsShape {
}
export interface ExtHostDocumentSaveParticipantShape {
$participateInSave(resource: URI, reason: SaveReason): TPromise<boolean[]>;
$participateInSave(resource: URI, reason: SaveReason): Thenable<boolean[]>;
}
export interface ITextEditorAddData {
......
......@@ -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<boolean[]> {
$participateInSave(resource: URI, reason: SaveReason): Thenable<boolean[]> {
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<any> {
private _deliverEventAsyncAndBlameBadListeners(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): Promise<any> {
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<any> {
private _deliverEventAsync(listener: Function, thisArg: any, stubEvent: vscode.TextDocumentWillSaveEvent): Promise<any> {
const promises: TPromise<vscode.TextEdit[]>[] = [];
const promises: Promise<vscode.TextEdit[]>[] = [];
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<vscode.TextEdit[][]>((resolve, reject) => {
return new Promise<vscode.TextEdit[][]>((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'));
});
}
}
......@@ -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', () => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册