Skip to content
Myr

fix: Preserve insertion order of Sets, fixes #819 (#976)

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

* Proposed fix for #819 to preserve Set insertion order * Proposed fix for #819 (B) We are already cloning and iterating over the Set. If we go a step further and clear it before iteration, and re-add all children during iteration, we can preserve insertion order.

authored by jdip · committed by GitHub · parent b9eae1d · Browse files · 3 files+49 added−8 removed
This commit

State

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

Diff stats

3 files+49 added−8 removed

  • tests:+34 added−0 removed
  • other:+15 added−8 removed

3 files+49 added−8 removed

modified:__tests__/map-set.js+34 added
294 unmodified lines
295 })296 expect(mapType1).toBe(mapType2)297 })298 })299 describe("set issues " + name, () => {300 test("#819.A - maintains order when adding", () => {301 const objs = [302 "a",303 {304 id: "b"305 }306 ]307308 const set = new Set([objs[0]])309 const newSet = produce(set, draft => {310 draft.add(objs[1])311 })312313 // passes314 expect(Array.from(newSet)).toEqual([objs[0], objs[1]])315 })316317 test("#819.B - maintains order when adding", () => {318 const objs = [319 {320 id: "a"321 },322 "b"323 ]324325 const set = new Set([objs[0]])326 const newSet = produce(set, draft => {327 draft.add(objs[1])328 })329330 expect(Array.from(newSet)).toEqual([objs[0], objs[1]])331 })332 })333}
modified:src/core/finalize.ts+15 added-7 removed
modified:src/utils/common.ts-1 removed
130 unmodified lines
131export function set(thing: any, propOrOldValue: PropertyKey, value: any) {132 const t = getArchtype(thing)133 if (t === Archtype.Map) thing.set(propOrOldValue, value)134 else if (t === Archtype.Set) {135 thing.delete(propOrOldValue)135 thing.add(value)136 } else thing[propOrOldValue] = value137}138
78 unmodified lines