Skip to content

Commit

Permalink
make ResourceMap a real ES6 map, #93368
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed May 5, 2020
1 parent effb178 commit e1869c5
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 30 deletions.
46 changes: 31 additions & 15 deletions src/vs/base/common/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ export class TernarySearchTree<K, V> {
}
}

export class ResourceMap<T> {
export class ResourceMap<T> implements Map<URI, T> {

readonly [Symbol.toStringTag] = 'ResourceMap';

protected readonly map: Map<string, T>;
protected readonly ignoreCase?: boolean;
Expand All @@ -491,8 +493,9 @@ export class ResourceMap<T> {
this.ignoreCase = false; // in the future this should be an uri-comparator
}

set(resource: URI, value: T): void {
set(resource: URI, value: T): this {
this.map.set(this.toKey(resource), value);
return this;
}

get(resource: URI): T | undefined {
Expand All @@ -515,33 +518,46 @@ export class ResourceMap<T> {
return this.map.delete(this.toKey(resource));
}

forEach(clb: (value: T, key: URI) => void): void {
this.map.forEach((value, index) => clb(value, URI.parse(index)));
forEach(clb: (value: T, key: URI, map: Map<URI, T>) => void, thisArg?: any): void {
if (typeof thisArg !== 'undefined') {
clb = clb.bind(thisArg);
}
for (let [index, value] of this.map) {
clb(value, URI.parse(index), <any>this);
}
}

values(): T[] {
return values(this.map);
values(): IterableIterator<T> {
return this.map.values();
}

private toKey(resource: URI): string {
let key = resource.toString();
if (this.ignoreCase) {
key = key.toLowerCase();
*keys(): IterableIterator<URI> {
for (let key of this.map.keys()) {
yield URI.parse(key);
}

return key;
}

keys(): URI[] {
return keys(this.map).map(k => URI.parse(k));
*entries(): IterableIterator<[URI, T]> {
for (let tuple of this.map.entries()) {
yield [URI.parse(tuple[0]), tuple[1]];
}
}

*[Symbol.iterator](): Iterator<[URI, T]> {
*[Symbol.iterator](): IterableIterator<[URI, T]> {
for (let item of this.map) {
yield [URI.parse(item[0]), item[1]];
}
}

private toKey(resource: URI): string {
let key = resource.toString();
if (this.ignoreCase) {
key = key.toLowerCase();
}

return key;
}

clone(): ResourceMap<T> {
const resourceMap = new ResourceMap<T>();

Expand Down
9 changes: 6 additions & 3 deletions src/vs/base/test/common/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,18 +656,21 @@ suite('Map', () => {

assert.equal(map.size, 0);

map.set(resource1, 1);
let res = map.set(resource1, 1);
assert.ok(res === map);
map.set(resource2, '2');
map.set(resource3, true);

const values = map.values();
const values = [...map.values()];
assert.equal(values[0], 1);
assert.equal(values[1], '2');
assert.equal(values[2], true);

let counter = 0;
map.forEach(value => {
map.forEach((value, key, mapObj) => {
assert.equal(value, values[counter++]);
assert.ok(URI.isUri(key));
assert.ok(map === mapObj);
});

const obj = Object.create(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export class Configuration {
overrides: this._workspaceConfiguration.overrides,
keys: this._workspaceConfiguration.keys
},
folders: this._folderConfigurations.keys().reduce<[UriComponents, IConfigurationModel][]>((result, folder) => {
folders: [...this._folderConfigurations.keys()].reduce<[UriComponents, IConfigurationModel][]>((result, folder) => {
const { contents, overrides, keys } = this._folderConfigurations.get(folder)!;
result.push([folder, { contents, overrides, keys }]);
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/markers/browser/markers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class MarkersWorkbenchService extends Disposable implements IMarkersWorkb
resourcesMap = resourcesMap ? resourcesMap : new ResourceMap<URI>();
resources.forEach(resource => resourcesMap!.set(resource, resource));
return resourcesMap;
}, 0)(resourcesMap => this.onMarkerChanged(resourcesMap.values())));
}, 0)(resourcesMap => this.onMarkerChanged([...resourcesMap.values()])));
}

private onMarkerChanged(resources: URI[]): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class NotebookEditorModelManager extends Disposable implements INotebookE
// private readonly modelLoadQueue = this._register(new ResourceQueue());

get models(): NotebookEditorModel[] {
return this.mapResourceToModel.values();
return [...this.mapResourceToModel.values()];
}
constructor(
@IInstantiationService readonly instantiationService: IInstantiationService
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/contrib/search/common/searchModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export class FolderMatch extends Disposable {
}

matches(): FileMatch[] {
return this._fileMatches.values();
return [...this._fileMatches.values()];
}

isEmpty(): boolean {
Expand Down Expand Up @@ -621,8 +621,8 @@ export class FolderMatch extends Disposable {
}

private disposeMatches(): void {
this._fileMatches.values().forEach((fileMatch: FileMatch) => fileMatch.dispose());
this._unDisposedFileMatches.values().forEach((fileMatch: FileMatch) => fileMatch.dispose());
[...this._fileMatches.values()].forEach((fileMatch: FileMatch) => fileMatch.dispose());
[...this._unDisposedFileMatches.values()].forEach((fileMatch: FileMatch) => fileMatch.dispose());
this._fileMatches.clear();
this._unDisposedFileMatches.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class BackupFilesModel implements IBackupFilesModel {
}

get(): URI[] {
return this.cache.keys();
return [...this.cache.keys()];
}

remove(resource: URI): void {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/services/bulkEdit/browser/conflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class ConflictDetector {
}

list(): URI[] {
return this._conflicts.keys();
return [...this._conflicts.keys()];
}

hasConflicts(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/services/editor/browser/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
}

// Handle no longer visible out of workspace resources
this.activeOutOfWorkspaceWatchers.keys().forEach(resource => {
[...this.activeOutOfWorkspaceWatchers.keys()].forEach(resource => {
if (!visibleOutOfWorkspaceResources.get(resource)) {
dispose(this.activeOutOfWorkspaceWatchers.get(resource));
this.activeOutOfWorkspaceWatchers.delete(resource);
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/services/search/common/searchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SearchService extends Disposable implements ISearchService {
const localResults = this.getLocalResults(query);

if (onProgress) {
arrays.coalesce(localResults.results.values()).forEach(onProgress);
arrays.coalesce([...localResults.results.values()]).forEach(onProgress);
}

const onProviderProgress = (progress: ISearchProgressItem) => {
Expand All @@ -99,7 +99,7 @@ export class SearchService extends Disposable implements ISearchService {
...{
limitHit: otherResults.limitHit || localResults.limitHit
},
results: [...otherResults.results, ...arrays.coalesce(localResults.results.values())]
results: [...otherResults.results, ...arrays.coalesce([...localResults.results.values()])]
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class TextFileEditorModelManager extends Disposable implements ITextFileE
})();

get models(): TextFileEditorModel[] {
return this.mapResourceToModel.values();
return [...this.mapResourceToModel.values()];
}

constructor(
Expand Down

0 comments on commit e1869c5

Please sign in to comment.