mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-08 17:19:48 +01:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. 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';
|
|
|
|
export interface CacheResult<T> {
|
|
promise: Thenable<T>;
|
|
dispose(): void;
|
|
}
|
|
|
|
export class Cache<T> {
|
|
|
|
private result: CacheResult<T> = null;
|
|
constructor(private task: (ct: CancellationToken) => Thenable<T>) { }
|
|
|
|
get(): CacheResult<T> {
|
|
if (this.result) {
|
|
return this.result;
|
|
}
|
|
|
|
const cts = new CancellationTokenSource();
|
|
const promise = this.task(cts.token);
|
|
always(promise, () => cts.dispose());
|
|
|
|
this.result = {
|
|
promise,
|
|
dispose: () => {
|
|
this.result = null;
|
|
cts.cancel();
|
|
cts.dispose();
|
|
}
|
|
};
|
|
|
|
return this.result;
|
|
}
|
|
}
|