@@ -20,6 +20,15 @@ import {
2020 handleCrossReference
2121} from "../internal"
2222
23+ declare global {
24+ // `Iterator.from` was added in ES2025.
25+ var Iterator :
26+ | undefined
27+ | {
28+ from < T , TReturn > ( iterable : Iterator < T , TReturn > ) : IterableIterator < T >
29+ }
30+ }
31+
2332export function enableMapSet ( ) {
2433 class DraftMap extends Map {
2534 [ DRAFT_STATE ] : MapState
@@ -126,8 +135,7 @@ export function enableMapSet() {
126135
127136 values ( ) : IterableIterator < any > {
128137 const iterator = this . keys ( )
129- return {
130- [ Symbol . iterator ] : ( ) => this . values ( ) ,
138+ return iteratorFrom ( {
131139 next : ( ) => {
132140 const r = iterator . next ( )
133141 /* istanbul ignore next */
@@ -138,13 +146,12 @@ export function enableMapSet() {
138146 value
139147 }
140148 }
141- } as any
149+ } )
142150 }
143151
144152 entries ( ) : IterableIterator < [ any , any ] > {
145153 const iterator = this . keys ( )
146- return {
147- [ Symbol . iterator ] : ( ) => this . entries ( ) ,
154+ return iteratorFrom ( {
148155 next : ( ) => {
149156 const r = iterator . next ( )
150157 /* istanbul ignore next */
@@ -155,14 +162,29 @@ export function enableMapSet() {
155162 value : [ r . value , value ]
156163 }
157164 }
158- } as any
165+ } )
159166 }
160167
161168 [ Symbol . iterator ] ( ) {
162169 return this . entries ( )
163170 }
164171 }
165172
173+ function iteratorFrom < T , TReturn > (
174+ iterable : Iterator < T , TReturn >
175+ ) : IterableIterator < T > {
176+ if ( typeof Iterator !== "undefined" ) {
177+ return Iterator . from ( iterable )
178+ }
179+
180+ const iterator : IterableIterator < T > = {
181+ ...iterable ,
182+ [ Symbol . iterator ] : ( ) => iterator
183+ }
184+
185+ return iterator
186+ }
187+
166188 function proxyMap_ < T extends AnyMap > (
167189 target : T ,
168190 parent ?: ImmerState
0 commit comments