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
Provenance
- Commit
858d036- Tree
6cfcfc6- Author
Marcel Jackwerth <marceljackwerth@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
89acf94
2 files changed+58 added−6 removed
modified:__tests__/map-set.js+30 added
329 unmodified lines330 })331 332 expect(Array.from(newSet)).toEqual([items[0], items[1]])333 })334+335+ it("IterableIterator should return itself", () => {336+ const baseState = {337+ map: new Map([1, 2, 3].map(i => [i, `Item ${i}`]))338+ }339+340+ produce(baseState, draft => {341+ const it = draft.map.values()342+ expect(it.next()).toEqual({value: "Item 1", done: false})343+344+ const itOfIt = it[Symbol.iterator]()345+ expect(itOfIt.next()).toEqual({value: "Item 2", done: false})346+ expect(itOfIt).toBe(it)347+348+ const it2 = draft.map.values()349+ expect(it2.next()).toEqual({value: "Item 1", done: false})350+ })351+ })352+353+ it("is compatible with ES2025 features", () => {354+ const baseState = {355+ map: new Map([1, 2, 3].map(i => [i, `Item ${i}`]))356+ }357+358+ const result = produce(baseState, draft => {359+ draft.map = new Map(draft.map.entries().filter(([k]) => k % 2 === 1))360+ })361+362+ 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 linesmodified:src/plugins/mapset.ts+28 added-6 removed
18 unmodified lines19 PluginMapSet,20 handleCrossReference21 } from "../internal"22 23+declare 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+}31+32 export function enableMapSet() {33 class DraftMap extends Map {34 [DRAFT_STATE]: MapState35 98 unmodified lines134 }135 136 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 r2 unmodified lines145 done: false,146 value147 }148 }141- } as any149+ })150 }151 152 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 r2 unmodified lines161 done: false,162 value: [r.value, value]163 }164 }158- } as any165+ })166 }167 168 [Symbol.iterator]() {169 return this.entries()170 }171 }172 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]: () => iterator183+ }184+185+ return iterator186+ }187+188 function proxyMap_<T extends AnyMap>(189 target: T,190 parent?: ImmerState191 ): [T, MapState] {163 unmodified lines