Skip to content
Myr

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Marcel Jackwerth · committed by GitHub · parent 89acf94 · Browse files · 2 files+58 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+58 added−6 removed

  • tests:+30 added−0 removed
  • other:+28 added−6 removed

2 files+58 added−6 removed

modified:__tests__/map-set.js+30 added
329 unmodified lines
330 })331332 expect(Array.from(newSet)).toEqual([items[0], items[1]])333 })334335 it("IterableIterator should return itself", () => {336 const baseState = {337 map: new Map([1, 2, 3].map(i => [i, `Item ${i}`]))338 }339340 produce(baseState, draft => {341 const it = draft.map.values()342 expect(it.next()).toEqual({value: "Item 1", done: false})343344 const itOfIt = it[Symbol.iterator]()345 expect(itOfIt.next()).toEqual({value: "Item 2", done: false})346 expect(itOfIt).toBe(it)347348 const it2 = draft.map.values()349 expect(it2.next()).toEqual({value: "Item 1", done: false})350 })351 })352353 it("is compatible with ES2025 features", () => {354 const baseState = {355 map: new Map([1, 2, 3].map(i => [i, `Item ${i}`]))356 }357358 const result = produce(baseState, draft => {359 draft.map = new Map(draft.map.entries().filter(([k]) => k % 2 === 1))360 })361362 expect(result.map.keys().toArray()).toEqual([1, 3])363 })364 })365 describe("set issues " + name, () => {366 test("#819.A - maintains order when adding", () => {367 const objs = [
147 unmodified lines
modified:src/plugins/mapset.ts+28 added-6 removed
18 unmodified lines
19 PluginMapSet,20 handleCrossReference21} from "../internal"2223declare global {24 // `Iterator.from` was added in ES2025.25 var Iterator:26 | undefined27 | {28 from<T, TReturn>(iterable: Iterator<T, TReturn>): IterableIterator<T>29 }30}3132export function enableMapSet() {33 class DraftMap extends Map {34 [DRAFT_STATE]: MapState35
98 unmodified lines
134 }135136 values(): IterableIterator<any> {137 const iterator = this.keys()129 return {130 [Symbol.iterator]: () => this.values(),138 return iteratorFrom({139 next: () => {140 const r = iterator.next()141 /* istanbul ignore next */142 if (r.done) return r
2 unmodified lines
145 done: false,146 value147 }148 }141 } as any149 })150 }151152 entries(): IterableIterator<[any, any]> {153 const iterator = this.keys()146 return {147 [Symbol.iterator]: () => this.entries(),154 return iteratorFrom({155 next: () => {156 const r = iterator.next()157 /* istanbul ignore next */158 if (r.done) return r
2 unmodified lines
161 done: false,162 value: [r.value, value]163 }164 }158 } as any165 })166 }167168 [Symbol.iterator]() {169 return this.entries()170 }171 }172173 function iteratorFrom<T, TReturn>(174 iterable: Iterator<T, TReturn>175 ): IterableIterator<T> {176 if (typeof Iterator !== "undefined") {177 return Iterator.from(iterable)178 }179180 const iterator: IterableIterator<T> = {181 ...iterable,182 [Symbol.iterator]: () => iterator183 }184185 return iterator186 }187188 function proxyMap_<T extends AnyMap>(189 target: T,190 parent?: ImmerState191 ): [T, MapState] {
163 unmodified lines