Add resourceMap helper for markdown extension (#151471)

This change introduces a `ResoruceMap` map type that is essentially `Map<vscode.Uri, T>`

It also fixes a potential race condition with `MdWorkspaceCache` where two quick calls would both trigger init
This commit is contained in:
Matt Bierner
2022-06-07 18:00:10 -07:00
committed by GitHub
parent 5bbab47c7c
commit 60a68d666d
4 changed files with 90 additions and 21 deletions

View File

@@ -0,0 +1,62 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
export class ResourceMap<T> {
private readonly map = new Map<string, { readonly uri: vscode.Uri; readonly value: T }>();
public set(uri: vscode.Uri, value: T): this {
this.map.set(this.toKey(uri), { uri, value });
return this;
}
public get(resource: vscode.Uri): T | undefined {
return this.map.get(this.toKey(resource))?.value;
}
public has(resource: vscode.Uri): boolean {
return this.map.has(this.toKey(resource));
}
public get size(): number {
return this.map.size;
}
public clear(): void {
this.map.clear();
}
public delete(resource: vscode.Uri): boolean {
return this.map.delete(this.toKey(resource));
}
public *values(): IterableIterator<T> {
for (const entry of this.map.values()) {
yield entry.value;
}
}
public *keys(): IterableIterator<vscode.Uri> {
for (const entry of this.map.values()) {
yield entry.uri;
}
}
public *entries(): IterableIterator<[vscode.Uri, T]> {
for (const entry of this.map.values()) {
yield [entry.uri, entry.value];
}
}
public [Symbol.iterator](): IterableIterator<[vscode.Uri, T]> {
return this.entries();
}
private toKey(resource: vscode.Uri) {
return resource.toString();
}
}