From 480272dc7989404049f7bb2d3a4d30313f3d6822 Mon Sep 17 00:00:00 2001 From: Dirk Baeumer Date: Mon, 11 May 2020 11:28:20 +0200 Subject: [PATCH] Add map state test to forEach --- src/vs/base/common/map.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/vs/base/common/map.ts b/src/vs/base/common/map.ts index 962df44c0de..8df6ef68bea 100644 --- a/src/vs/base/common/map.ts +++ b/src/vs/base/common/map.ts @@ -580,7 +580,9 @@ export const enum Touch { AsNew = 2 } -export class LinkedMap { +export class LinkedMap implements Map { + + readonly [Symbol.toStringTag] = 'LinkedMap'; private _map: Map>; private _head: Item | undefined; @@ -636,7 +638,7 @@ export class LinkedMap { return item.value; } - set(key: K, value: V, touch: Touch = Touch.None): void { + set(key: K, value: V, touch: Touch = Touch.None): this { let item = this._map.get(key); if (item) { item.value = value; @@ -662,6 +664,7 @@ export class LinkedMap { this._map.set(key, item); this._size++; } + return this; } delete(key: K): boolean { @@ -694,6 +697,7 @@ export class LinkedMap { } forEach(callbackfn: (value: V, key: K, map: LinkedMap) => void, thisArg?: any): void { + const state = this._state; let current = this._head; while (current) { if (thisArg) { @@ -701,6 +705,9 @@ export class LinkedMap { } else { callbackfn(current.value, current.key, this); } + if (this._state !== state) { + throw new Error(`LinkedMap got modified during iteration.`); + } current = current.next; } } @@ -987,9 +994,10 @@ export class LRUCache extends LinkedMap { return super.get(key, Touch.None); } - set(key: K, value: V): void { + set(key: K, value: V): this { super.set(key, value, Touch.AsNew); this.checkTrim(); + return this; } private checkTrim() { -- GitLab