Merge from master

This commit is contained in:
Raj Musuku
2019-02-21 17:56:04 -08:00
parent 5a146e34fa
commit 666ae11639
11482 changed files with 119352 additions and 255574 deletions

View File

@@ -3,27 +3,37 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { always } from 'vs/base/common/async';
import { TPromise } from 'vs/base/common/winjs.base';
export interface CacheResult<T> {
promise: Promise<T>;
dispose(): void;
}
export default class Cache<T> {
export class Cache<T> {
private promise: TPromise<T> = null;
constructor(private task: () => TPromise<T>) { }
private result: CacheResult<T> | null = null;
constructor(private task: (ct: CancellationToken) => Promise<T>) { }
get(): TPromise<T> {
if (this.promise) {
return this.promise;
get(): CacheResult<T> {
if (this.result) {
return this.result;
}
const promise = this.task();
const cts = new CancellationTokenSource();
const promise = this.task(cts.token);
always(promise, () => cts.dispose());
this.promise = new TPromise<T>((c, e) => promise.done(c, e), () => {
this.promise = null;
promise.cancel();
});
this.result = {
promise,
dispose: () => {
this.result = null;
cts.cancel();
cts.dispose();
}
};
return this.promise;
return this.result;
}
}