ipc: native uninitializedPromise
This commit is contained in:
parent
76c5590855
commit
61f7b687ad
2 changed files with 26 additions and 17 deletions
|
@ -296,12 +296,11 @@ export function fromPromise<T =any>(promise: Thenable<T>): Event<T> {
|
|||
}
|
||||
|
||||
export function toPromise<T>(event: Event<T>): Thenable<T> {
|
||||
return new TPromise(complete => {
|
||||
const sub = event(e => {
|
||||
sub.dispose();
|
||||
complete(e);
|
||||
});
|
||||
});
|
||||
return new TPromise(c => once(event)(c));
|
||||
}
|
||||
|
||||
export function toNativePromise<T>(event: Event<T>): Thenable<T> {
|
||||
return new Promise(c => once(event)(c));
|
||||
}
|
||||
|
||||
export function once<T>(event: Event<T>): Event<T> {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
|
||||
import { Event, Emitter, once, filterEvent, toPromise, Relay } from 'vs/base/common/event';
|
||||
import { Event, Emitter, once, filterEvent, toNativePromise, Relay } from 'vs/base/common/event';
|
||||
import { always, CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
|
||||
|
||||
export enum RequestType {
|
||||
|
@ -295,10 +295,13 @@ export class ChannelClient implements IChannelClient, IDisposable {
|
|||
const type = RequestType.Promise;
|
||||
const request: IRawRequest = { id, type, channelName, name, arg };
|
||||
|
||||
const activeRequest = this.whenInitialized().then(() => {
|
||||
const id = request.id;
|
||||
let uninitializedPromise: CancelablePromise<void> | null = null;
|
||||
|
||||
const result = new TPromise((c, e) => {
|
||||
uninitializedPromise = createCancelablePromise(_ => this.whenInitialized());
|
||||
uninitializedPromise.then(() => {
|
||||
uninitializedPromise = null;
|
||||
|
||||
return new TPromise((c, e) => {
|
||||
const handler: IHandler = response => {
|
||||
switch (response.type) {
|
||||
case ResponseType.PromiseSuccess:
|
||||
|
@ -323,14 +326,21 @@ export class ChannelClient implements IChannelClient, IDisposable {
|
|||
|
||||
this.handlers.set(id, handler);
|
||||
this.sendRequest(request);
|
||||
}, () => this.sendRequest({ id, type: RequestType.PromiseCancel }));
|
||||
});
|
||||
}, () => {
|
||||
if (uninitializedPromise) {
|
||||
uninitializedPromise.cancel();
|
||||
uninitializedPromise = null;
|
||||
} else {
|
||||
this.sendRequest({ id, type: RequestType.PromiseCancel });
|
||||
}
|
||||
});
|
||||
|
||||
const disposable = toDisposable(() => activeRequest.cancel());
|
||||
const disposable = toDisposable(() => result.cancel());
|
||||
this.activeRequests.add(disposable);
|
||||
always(activeRequest, () => this.activeRequests.delete(disposable));
|
||||
always(result, () => this.activeRequests.delete(disposable));
|
||||
|
||||
return activeRequest;
|
||||
return result;
|
||||
}
|
||||
|
||||
private requestEvent(channelName: string, name: string, arg: any): Event<any> {
|
||||
|
@ -416,11 +426,11 @@ export class ChannelClient implements IChannelClient, IDisposable {
|
|||
}
|
||||
}
|
||||
|
||||
private whenInitialized(): TPromise<void> {
|
||||
private whenInitialized(): Thenable<void> {
|
||||
if (this.state === State.Idle) {
|
||||
return TPromise.as(null);
|
||||
return Promise.resolve(null);
|
||||
} else {
|
||||
return TPromise.wrap(toPromise(this.onDidInitialize));
|
||||
return toNativePromise(this.onDidInitialize);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue