提交 e1869c57 编写于 作者: J Johannes Rieken

make ResourceMap a real ES6 map, #93368

fyi @bpasero
上级 effb1787
......@@ -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;
......@@ -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 {
......@@ -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>();
......
......@@ -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);
......
......@@ -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;
......
......@@ -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 {
......
......@@ -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
......
......@@ -563,7 +563,7 @@ export class FolderMatch extends Disposable {
}
matches(): FileMatch[] {
return this._fileMatches.values();
return [...this._fileMatches.values()];
}
isEmpty(): boolean {
......@@ -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();
}
......
......@@ -95,7 +95,7 @@ export class BackupFilesModel implements IBackupFilesModel {
}
get(): URI[] {
return this.cache.keys();
return [...this.cache.keys()];
}
remove(resource: URI): void {
......
......@@ -87,7 +87,7 @@ export class ConflictDetector {
}
list(): URI[] {
return this._conflicts.keys();
return [...this._conflicts.keys()];
}
hasConflicts(): boolean {
......
......@@ -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);
......
......@@ -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) => {
......@@ -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()])]
};
}
......
......@@ -68,7 +68,7 @@ export class TextFileEditorModelManager extends Disposable implements ITextFileE
})();
get models(): TextFileEditorModel[] {
return this.mapResourceToModel.values();
return [...this.mapResourceToModel.values()];
}
constructor(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册