diff --git a/src/vs/workbench/services/extensions/node/lazyPromise.ts b/src/vs/workbench/services/extensions/node/lazyPromise.ts index 1d045268b71c5ae7b8ae9c8977a25732d3d8512f..da83c8f49645f558985741b928f04aec56966f8d 100644 --- a/src/vs/workbench/services/extensions/node/lazyPromise.ts +++ b/src/vs/workbench/services/extensions/node/lazyPromise.ts @@ -7,9 +7,7 @@ import { TPromise, ValueCallback, ErrorCallback } from 'vs/base/common/winjs.base'; import { onUnexpectedError } from 'vs/base/common/errors'; -export class LazyPromise implements TPromise { - - private _onCancel: () => void; +export class LazyPromise implements Thenable { private _actual: TPromise; private _actualOk: ValueCallback; @@ -21,10 +19,7 @@ export class LazyPromise implements TPromise { private _hasErr: boolean; private _err: any; - private _isCanceled: boolean; - - constructor(onCancel: () => void) { - this._onCancel = onCancel; + constructor() { this._actual = null; this._actualOk = null; this._actualErr = null; @@ -32,7 +27,6 @@ export class LazyPromise implements TPromise { this._value = null; this._hasErr = false; this._err = null; - this._isCanceled = false; } private _ensureActual(): TPromise { @@ -40,7 +34,7 @@ export class LazyPromise implements TPromise { this._actual = new TPromise((c, e) => { this._actualOk = c; this._actualErr = e; - }, this._onCancel); + }); if (this._hasValue) { this._actualOk(this._value); @@ -54,7 +48,7 @@ export class LazyPromise implements TPromise { } public resolveOk(value: any): void { - if (this._isCanceled || this._hasErr) { + if (this._hasValue || this._hasErr) { return; } @@ -67,7 +61,7 @@ export class LazyPromise implements TPromise { } public resolveErr(err: any): void { - if (this._isCanceled || this._hasValue) { + if (this._hasValue || this._hasErr) { return; } @@ -84,24 +78,6 @@ export class LazyPromise implements TPromise { } public then(success: any, error: any): any { - if (this._isCanceled) { - return; - } - return this._ensureActual().then(success, error); } - - public cancel(): void { - if (this._hasValue || this._hasErr) { - return; - } - - this._isCanceled = true; - - if (this._actual) { - this._actual.cancel(); - } else { - this._onCancel(); - } - } } diff --git a/src/vs/workbench/services/extensions/node/rpcProtocol.ts b/src/vs/workbench/services/extensions/node/rpcProtocol.ts index 8f18079f82de4b695d5941769ce7eea65bbc87d0..4ad109079ba69a186619532e63c714e34862a3b7 100644 --- a/src/vs/workbench/services/extensions/node/rpcProtocol.ts +++ b/src/vs/workbench/services/extensions/node/rpcProtocol.ts @@ -96,6 +96,8 @@ export interface IRPCProtocolLogger { logOutgoing(msgLength: number, req: number, initiator: RequestInitiator, str: string, data?: any): void; } +const noop = () => { }; + export class RPCProtocol implements IRPCProtocol { private readonly _protocol: IMessagePassingProtocol; @@ -249,7 +251,7 @@ export class RPCProtocol implements IRPCProtocol { } const callId = String(req); - let promise: TPromise; + let promise: Thenable; let cancel: () => void; if (usesCancellationToken) { const cancellationTokenSource = new CancellationTokenSource(); @@ -257,8 +259,9 @@ export class RPCProtocol implements IRPCProtocol { promise = this._invokeHandler(rpcId, method, args); cancel = () => cancellationTokenSource.cancel(); } else { + // cannot be cancelled promise = this._invokeHandler(rpcId, method, args); - cancel = () => promise.cancel(); + cancel = noop; } this._cancelInvokedHandlers[callId] = cancel; @@ -331,7 +334,7 @@ export class RPCProtocol implements IRPCProtocol { pendingReply.resolveErr(err); } - private _invokeHandler(rpcId: number, methodName: string, args: any[]): TPromise { + private _invokeHandler(rpcId: number, methodName: string, args: any[]): Thenable { try { return TPromise.as(this._doInvokeHandler(rpcId, methodName, args)); } catch (err) { @@ -351,7 +354,7 @@ export class RPCProtocol implements IRPCProtocol { return method.apply(actor, args); } - private _remoteCall(rpcId: number, methodName: string, args: any[]): TPromise { + private _remoteCall(rpcId: number, methodName: string, args: any[]): Thenable { if (this._isDisposed) { return TPromise.wrapError(errors.canceled()); } @@ -367,17 +370,16 @@ export class RPCProtocol implements IRPCProtocol { const req = ++this._lastMessageId; const callId = String(req); - const sendCancel = () => { - const msg = MessageIO.serializeCancel(req); - if (this._logger) { - this._logger.logOutgoing(msg.byteLength, req, RequestInitiator.LocalSide, `cancel`); - } - this._protocol.send(MessageIO.serializeCancel(req)); - }; - const result = new LazyPromise(sendCancel); + const result = new LazyPromise(); if (cancellationToken) { - cancellationToken.onCancellationRequested(sendCancel); + cancellationToken.onCancellationRequested(() => { + const msg = MessageIO.serializeCancel(req); + if (this._logger) { + this._logger.logOutgoing(msg.byteLength, req, RequestInitiator.LocalSide, `cancel`); + } + this._protocol.send(MessageIO.serializeCancel(req)); + }); } this._pendingRPCReplies[callId] = result; diff --git a/src/vs/workbench/services/extensions/test/node/rpcProtocol.test.ts b/src/vs/workbench/services/extensions/test/node/rpcProtocol.test.ts index f47224e36d2437ae40386198b8e9f130ed60efab..e6156147f3c60e99ee55d6271b89a343ea00d512 100644 --- a/src/vs/workbench/services/extensions/test/node/rpcProtocol.test.ts +++ b/src/vs/workbench/services/extensions/test/node/rpcProtocol.test.ts @@ -35,7 +35,7 @@ suite('RPCProtocol', () => { let delegate: (a1: any, a2: any) => any; let bProxy: BClass; class BClass { - $m(a1: any, a2: any): TPromise { + $m(a1: any, a2: any): Thenable { return TPromise.as(delegate.call(null, a1, a2)); } } @@ -108,15 +108,6 @@ suite('RPCProtocol', () => { }, done); }); - test('cancelling a call', function () { - delegate = (a1: number, a2: number) => a1 + a2; - let p = bProxy.$m(4, 1); - p.then((res: number) => { - assert.fail('should not receive result'); - }); - p.cancel(); - }); - test('cancelling a call via CancellationToken before', function (done) { delegate = (a1: number, a2: number) => a1 + a2; let p = bProxy.$m(4, CancellationToken.Cancelled);