@@ -580,7 +580,9 @@ export const enum Touch {
580580 AsNew = 2
581581}
582582
583- export class LinkedMap < K , V > {
583+ export class LinkedMap < K , V > implements Map < K , V > {
584+
585+ readonly [ Symbol . toStringTag ] = 'LinkedMap' ;
584586
585587 private _map : Map < K , Item < K , V > > ;
586588 private _head : Item < K , V > | undefined ;
@@ -636,7 +638,7 @@ export class LinkedMap<K, V> {
636638 return item . value ;
637639 }
638640
639- set ( key : K , value : V , touch : Touch = Touch . None ) : void {
641+ set ( key : K , value : V , touch : Touch = Touch . None ) : this {
640642 let item = this . _map . get ( key ) ;
641643 if ( item ) {
642644 item . value = value ;
@@ -662,6 +664,7 @@ export class LinkedMap<K, V> {
662664 this . _map . set ( key , item ) ;
663665 this . _size ++ ;
664666 }
667+ return this ;
665668 }
666669
667670 delete ( key : K ) : boolean {
@@ -694,13 +697,17 @@ export class LinkedMap<K, V> {
694697 }
695698
696699 forEach ( callbackfn : ( value : V , key : K , map : LinkedMap < K , V > ) => void , thisArg ?: any ) : void {
700+ const state = this . _state ;
697701 let current = this . _head ;
698702 while ( current ) {
699703 if ( thisArg ) {
700704 callbackfn . bind ( thisArg ) ( current . value , current . key , this ) ;
701705 } else {
702706 callbackfn ( current . value , current . key , this ) ;
703707 }
708+ if ( this . _state !== state ) {
709+ throw new Error ( `LinkedMap got modified during iteration.` ) ;
710+ }
704711 current = current . next ;
705712 }
706713 }
@@ -987,9 +994,10 @@ export class LRUCache<K, V> extends LinkedMap<K, V> {
987994 return super . get ( key , Touch . None ) ;
988995 }
989996
990- set ( key : K , value : V ) : void {
997+ set ( key : K , value : V ) : this {
991998 super . set ( key , value , Touch . AsNew ) ;
992999 this . checkTrim ( ) ;
1000+ return this ;
9931001 }
9941002
9951003 private checkTrim ( ) {
0 commit comments