mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Merge from master
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user