src/vs/base/common/cache.ts
153 LOC · 130 covered · 23 uncovered · 19 ranges · 16824 concepts · 8 introducers · 10556 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
strings.ts ×101
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken, CancellationTokenSource } from './cancellation.js';
import { IDisposable } from './lifecycle.js';
export interface CacheResult<T> extends IDisposable {
promise: Promise<T>;
}
export class Cache<T> {
private result: CacheResult<T> | null = null;
constructor(private task: (ct: CancellationToken) => Promise<T>) { }
get(): CacheResult<T> {
return this.result;
}
const cts = new CancellationTokenSource();
const promise = this.task(cts.token);
this.result = {
promise,
dispose: () => {
cts.cancel();
cts.dispose();
}
return this.result;
}
export function identity<T>(t: T): T {
}
interface ICacheOptions<TArg> {
/**
* The cache key is used to identify the cache entry.
* Strict equality is used to compare cache keys.
*/
getCacheKey: (arg: TArg) => unknown;
}
/**
* Uses a LRU cache to make a given parametrized function cached.
* Caches just the last key/value.
*/
export class LRUCachedFunction<TArg, TComputed> {
private lastCache: TComputed | undefined = undefined;
private lastArgKey: unknown | undefined = undefined;
private readonly _fn: (arg: TArg) => TComputed;
private readonly _computeKey: (arg: TArg) => unknown;
constructor(fn: (arg: TArg) => TComputed);
constructor(options: ICacheOptions<TArg>, fn: (arg: TArg) => TComputed);
constructor(arg1: ICacheOptions<TArg> | ((arg: TArg) => TComputed), arg2?: (arg: TArg) => TComputed) {
if (typeof arg1 === 'function') {
this._fn = arg1;
this._computeKey = identity;
} else {
this._fn = arg2!;
this._computeKey = arg1.getCacheKey;
}
public get(arg: TArg): TComputed {
if (this.lastArgKey !== key) {
this.lastArgKey = key;
this.lastCache = this._fn(arg);
}
return this.lastCache!;
}
/**
* Uses an unbounded cache to memoize the results of the given function.
*/
export class CachedFunction<TArg, TComputed> {
private readonly _map = new Map<TArg, TComputed>();
private readonly _map2 = new Map<unknown, TComputed>();
public get cachedValues(): ReadonlyMap<TArg, TComputed> {
return this._map;
}
private readonly _fn: (arg: TArg) => TComputed;
private readonly _computeKey: (arg: TArg) => unknown;
constructor(fn: (arg: TArg) => TComputed);
constructor(options: ICacheOptions<TArg>, fn: (arg: TArg) => TComputed);
constructor(arg1: ICacheOptions<TArg> | ((arg: TArg) => TComputed), arg2?: (arg: TArg) => TComputed) {
this._fn = arg1;
this._computeKey = identity;
} else {
this._fn = arg2!;
this._computeKey = arg1.getCacheKey;
}
public get(arg: TArg): TComputed {
if (this._map2.has(key)) {
}
const value = this._fn(arg);
this._map.set(arg, value);
this._map2.set(key, value);
return value;
}
/**
* Uses an unbounded cache to memoize the results of the given function.
*/
export class WeakCachedFunction<TArg, TComputed> {
private readonly _map = new WeakMap<WeakKey, TComputed>();
private readonly _fn: (arg: TArg) => TComputed;
private readonly _computeKey: (arg: TArg) => unknown;
constructor(fn: (arg: TArg) => TComputed);
constructor(options: ICacheOptions<TArg>, fn: (arg: TArg) => TComputed);
constructor(arg1: ICacheOptions<TArg> | ((arg: TArg) => TComputed), arg2?: (arg: TArg) => TComputed) {
if (typeof arg1 === 'function') {
this._fn = arg1;
this._computeKey = identity;
} else {
this._fn = arg2!;
this._computeKey = arg1.getCacheKey;
}
}
public get(arg: TArg): TComputed {
const key = this._computeKey(arg) as WeakKey;
if (this._map.has(key)) {
return this._map.get(key)!;
}
const value = this._fn(arg);
this._map.set(key, value);
return value;
}