@@ -51,10 +51,8 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
5151 }
5252
5353 // **** ALL GETTERS ****
54- entries ( ) {
55- consumeTag ( this . #collection) ;
56-
57- return this . #vals. entries ( ) ;
54+ entries ( ) : MapIterator < [ K , V ] > {
55+ return this [ Symbol . iterator ] ( ) ;
5856 }
5957
6058 getOrInsert ( key : K , defaultValue : V ) : V {
@@ -82,15 +80,23 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
8280 }
8381
8482 values ( ) {
85- consumeTag ( this . #collection ) ;
83+ let iterator = this [ Symbol . iterator ] ( ) ;
8684
87- return this . #vals. values ( ) ;
85+ return {
86+ next ( ) {
87+ let { value, done } = iterator . next ( ) ;
88+ return { value : done ? ( undefined as V ) : value ?. [ 1 ] , done } ;
89+ } ,
90+ [ Symbol . iterator ] ( ) {
91+ return this ;
92+ } ,
93+ } as MapIterator < V > ;
8894 }
8995
9096 forEach ( fn : ( value : V , key : K , map : Map < K , V > ) => void ) : void {
91- consumeTag ( this . #collection ) ;
92-
93- this . #vals . forEach ( fn ) ;
97+ for ( let [ key , value ] of this ) {
98+ fn ( value , key , this ) ;
99+ }
94100 }
95101
96102 get size ( ) : number {
@@ -101,8 +107,10 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
101107
102108 /**
103109 * When iterating:
104- * - we entangle with the collection (as we iterate over the whole thing
110+ * - we entangle with the collection (as we iterate over the whole thing)
111+ * via keys() → consumeTag(#collection)
105112 * - for each individual item, we entangle with the item as well
113+ * via get() → consumeTag(#storageFor(key))
106114 */
107115 [ Symbol . iterator ] ( ) {
108116 let keys = this . keys ( ) ;
@@ -121,6 +129,9 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
121129 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
122130 return { value : [ currentKey , self . get ( currentKey ! ) ] , done : false } ;
123131 } ,
132+ [ Symbol . iterator ] ( ) {
133+ return this ;
134+ } ,
124135 } as MapIterator < [ K , V ] > ;
125136 }
126137
@@ -129,10 +140,10 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
129140 }
130141
131142 set ( key : K , value : V ) : this {
132- let existing = this . #vals. get ( key ) ;
143+ let hasExisting = this . #vals. has ( key ) ;
133144
134- if ( existing ) {
135- let isUnchanged = this . #options. equals ( existing , value ) ;
145+ if ( hasExisting ) {
146+ let isUnchanged = this . #options. equals ( this . #vals . get ( key ) as V , value ) ;
136147
137148 if ( isUnchanged ) {
138149 return this ;
@@ -141,7 +152,7 @@ class TrackedMap<K = unknown, V = unknown> implements Map<K, V> {
141152
142153 this . #dirtyStorageFor( key ) ;
143154
144- if ( ! existing ) {
155+ if ( ! hasExisting ) {
145156 DIRTY_TAG ( this . #collection) ;
146157 }
147158
0 commit comments