Merge pull request #437 from runnez/es5-map-and-set-support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
es5 support for Map/Set
authored by Michel Weststrate · committed by GitHub · parents 13eab80, 0de782c · Browse files · 8 files·+890 added−619 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+890 added−619 removed
- tests:+598 added−549 removed
- other:+292 added−70 removed
Provenance
- Commit
85656cb- Tree
1b4a29a- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
13eab80- Parent 2
0de782c
8 files changed+890 added−619 removed
modified:__tests__/__snapshots__/hooks.js.snap+106 added
1 // Jest Snapshot v1, https://goo.gl/fbAQLP2 3+exports[`hooks (es5) - onAssign() when draft is a Map assign 1`] = `4+Array [5+ Array [6+ "a",7+ 10,8+ ],9+ Array [10+ Object {11+ "prop": "val1",12+ },13+ 11,14+ ],15+]16+`;17+18+exports[`hooks (es5) - onAssign() when draft is a Map nested assignments 1`] = `19+Array [20+ Array [21+ "b",22+ 2,23+ ],24+ Array [25+ Object {26+ "prop": "val1",27+ },28+ Map {29+ "b" => 2,30+ "d" => 1,31+ },32+ ],33+ Array [34+ "a",35+ Map {36+ Object {37+ "prop": "val1",38+ } => Map {39+ "b" => 2,40+ "d" => 1,41+ },42+ },43+ ],44+]45+`;46+47+exports[`hooks (es5) - onAssign() when draft is a Set assign 1`] = `48+Array [49+ Array [50+ "a",51+ Set {52+ 1,53+ 2,54+ 3,55+ 4,56+ },57+ ],58+]59+`;60+61+exports[`hooks (es5) - onAssign() when draft is a Set delete 1`] = `62+Array [63+ Array [64+ "a",65+ Set {66+ 2,67+ 3,68+ },69+ ],70+]71+`;72+73+exports[`hooks (es5) - onAssign() when draft is a Set nested assignments 1`] = `74+Array [75+ Array [76+ "a",77+ Set {78+ "a",79+ Set {80+ 1,81+ 2,82+ },83+ },84+ ],85+]86+`;87+88 exports[`hooks (es5) - onAssign() when draft is an array assign 1`] = `89 Array [90 Array [91 0,100 unmodified lines192 ],193 ]194 `;195 196+exports[`hooks (es5) - onDelete() when draft is a Map - delete 1`] = `197+Array [198+ Array [199+ "a",200+ ],201+ Array [202+ Object {203+ "prop": "val1",204+ },205+ ],206+]207+`;208+209+exports[`hooks (es5) - onDelete() when draft is a Map - nested deletions 1`] = `210+Array [211+ Array [212+ "b",213+ ],214+]215+`;216+217 exports[`hooks (es5) - onDelete() when draft is an array - length = 0 1`] = `218 Array [219 Array [220 0,289 unmodified linesmodified:__tests__/base.js+356 added-376 removed
297 unmodified lines298 })299 }300 })301 302- if (useProxies) {303- describe("map drafts", () => {304- it("supports key access", () => {305- const value = baseState.aMap.get("jedi")306- const nextState = produce(baseState, s => {307- expect(s.aMap.get("jedi")).toEqual(value)302+ describe("map drafts", () => {303+ it("supports key access", () => {304+ const value = baseState.aMap.get("jedi")305+ const nextState = produce(baseState, s => {306+ expect(s.aMap.get("jedi")).toEqual(value)308- })309- expect(nextState).toBe(baseState)307 })308+ expect(nextState).toBe(baseState)309+ })310 312- it("supports key access for non-primitive keys", () => {313- const key = {prop: "val"}314- const base = new Map([[key, {id: 1, a: 1}]])315- const value = base.get(key)316- const nextState = produce(base, s => {317- expect(s.get(key)).toEqual(value)318- })319- expect(nextState).toBe(base)311+ it("supports key access for non-primitive keys", () => {312+ const key = {prop: "val"}313+ const base = new Map([[key, {id: 1, a: 1}]])314+ const value = base.get(key)315+ const nextState = produce(base, s => {316+ expect(s.get(key)).toEqual(value)317 })318+ expect(nextState).toBe(base)319+ })320 322- it("supports iteration", () => {323- const base = new Map([324- ["first", {id: 1, a: 1}],325- ["second", {id: 2, a: 1}]326- ])327- const findById = (map, id) => {328- for (const [, item] of map) {329- if (item.id === id) return item330- }331- return null321+ it("supports iteration", () => {322+ const base = new Map([323+ ["first", {id: 1, a: 1}],324+ ["second", {id: 2, a: 1}]325+ ])326+ const findById = (map, id) => {327+ for (const [, item] of map) {328+ if (item.id === id) return item329 }333- const result = produce(base, draft => {334- const obj1 = findById(draft, 1)335- const obj2 = findById(draft, 2)336- obj1.a = 2337- obj2.a = 2338- })339- expect(result).not.toBe(base)340- expect(result.get("first").a).toEqual(2)341- expect(result.get("second").a).toEqual(2)330+ return null331+ }332+ const result = produce(base, draft => {333+ const obj1 = findById(draft, 1)334+ const obj2 = findById(draft, 2)335+ obj1.a = 2336+ obj2.a = 2337 })338+ expect(result).not.toBe(base)339+ expect(result.get("first").a).toEqual(2)340+ expect(result.get("second").a).toEqual(2)341+ })342 344- it("supports 'entries'", () => {345- const base = new Map([346- ["first", {id: 1, a: 1}],347- ["second", {id: 2, a: 1}]348- ])349- const findById = (map, id) => {350- for (const [, item] of map.entries()) {351- if (item.id === id) return item352- }353- return null343+ it("supports 'entries'", () => {344+ const base = new Map([345+ ["first", {id: 1, a: 1}],346+ ["second", {id: 2, a: 1}]347+ ])348+ const findById = (map, id) => {349+ for (const [, item] of map.entries()) {350+ if (item.id === id) return item351 }355- const result = produce(base, draft => {356- const obj1 = findById(draft, 1)357- const obj2 = findById(draft, 2)358- obj1.a = 2359- obj2.a = 2360- })361- expect(result).not.toBe(base)362- expect(result.get("first").a).toEqual(2)363- expect(result.get("second").a).toEqual(2)352+ return null353+ }354+ const result = produce(base, draft => {355+ const obj1 = findById(draft, 1)356+ const obj2 = findById(draft, 2)357+ obj1.a = 2358+ obj2.a = 2359 })360+ expect(result).not.toBe(base)361+ expect(result.get("first").a).toEqual(2)362+ expect(result.get("second").a).toEqual(2)363+ })364 366- it("supports 'values'", () => {367- const base = new Map([368- ["first", {id: 1, a: 1}],369- ["second", {id: 2, a: 1}]370- ])371- const findById = (map, id) => {372- for (const item of map.values()) {373- if (item.id === id) return item374- }375- return null365+ it("supports 'values'", () => {366+ const base = new Map([367+ ["first", {id: 1, a: 1}],368+ ["second", {id: 2, a: 1}]369+ ])370+ const findById = (map, id) => {371+ for (const item of map.values()) {372+ if (item.id === id) return item373 }377- const result = produce(base, draft => {378- const obj1 = findById(draft, 1)379- const obj2 = findById(draft, 2)380- obj1.a = 2381- obj2.a = 2382- })383- expect(result).not.toBe(base)384- expect(result.get("first").a).toEqual(2)385- expect(result.get("second").a).toEqual(2)374+ return null375+ }376+ const result = produce(base, draft => {377+ const obj1 = findById(draft, 1)378+ const obj2 = findById(draft, 2)379+ obj1.a = 2380+ obj2.a = 2381 })382+ expect(result).not.toBe(base)383+ expect(result.get("first").a).toEqual(2)384+ expect(result.get("second").a).toEqual(2)385+ })386 388- it("supports 'keys", () => {389- const base = new Map([390- ["first", Symbol()],391- ["second", Symbol()]387+ it("supports 'keys", () => {388+ const base = new Map([389+ ["first", Symbol()],390+ ["second", Symbol()]391+ ])392+ const result = produce(base, draft => {393+ expect([...draft.keys()]).toEqual(["first", "second"])394+ draft.set("third", Symbol())395+ expect([...draft.keys()]).toEqual([396+ "first",397+ "second",398+ "third"399 ])393- const result = produce(base, draft => {394- expect([...draft.keys()]).toEqual(["first", "second"])395- draft.set("third", Symbol())396- expect([...draft.keys()]).toEqual([397- "first",398- "second",399- "third"400- ])401- })400 })401+ })402 404- it("supports forEach", () => {405- const key1 = {prop: "val1"}406- const key2 = {prop: "val2"}407- const base = new Map([408- ["first", {id: 1, a: 1}],409- ["second", {id: 2, a: 1}],410- [key1, {id: 3, a: 1}],411- [key2, {id: 4, a: 1}]412- ])413- const result = produce(base, draft => {414- let sum1 = 0415- draft.forEach(({a}) => {416- sum1 += a417- })418- expect(sum1).toBe(4)419- let sum2 = 0420- draft.get("first").a = 10421- draft.get("second").a = 20422- draft.get(key1).a = 30423- draft.get(key2).a = 40424- draft.forEach(({a}) => {425- sum2 += a426- })427- expect(sum2).toBe(100)403+ it("supports forEach", () => {404+ const key1 = {prop: "val1"}405+ const key2 = {prop: "val2"}406+ const base = new Map([407+ ["first", {id: 1, a: 1}],408+ ["second", {id: 2, a: 1}],409+ [key1, {id: 3, a: 1}],410+ [key2, {id: 4, a: 1}]411+ ])412+ const result = produce(base, draft => {413+ let sum1 = 0414+ draft.forEach(({a}) => {415+ sum1 += a416 })429- expect(result).not.toBe(base)430- expect(base.get("first").a).toEqual(1)431- expect(base.get("second").a).toEqual(1)432- expect(base.get(key1).a).toEqual(1)433- expect(base.get(key2).a).toEqual(1)434- expect(result.get("first").a).toEqual(10)435- expect(result.get("second").a).toEqual(20)436- expect(result.get(key1).a).toEqual(30)437- expect(result.get(key2).a).toEqual(40)417+ expect(sum1).toBe(4)418+ let sum2 = 0419+ draft.get("first").a = 10420+ draft.get("second").a = 20421+ draft.get(key1).a = 30422+ draft.get(key2).a = 40423+ draft.forEach(({a}) => {424+ sum2 += a425+ })426+ expect(sum2).toBe(100)427 })428+ expect(result).not.toBe(base)429+ expect(base.get("first").a).toEqual(1)430+ expect(base.get("second").a).toEqual(1)431+ expect(base.get(key1).a).toEqual(1)432+ expect(base.get(key2).a).toEqual(1)433+ expect(result.get("first").a).toEqual(10)434+ expect(result.get("second").a).toEqual(20)435+ expect(result.get(key1).a).toEqual(30)436+ expect(result.get(key2).a).toEqual(40)437+ })438 440- it("supports forEach mutation", () => {441- const base = new Map([442- ["first", {id: 1, a: 1}],443- ["second", {id: 2, a: 1}]444- ])445- const result = produce(base, draft => {446- draft.forEach(item => {447- item.a = 100448- })439+ it("supports forEach mutation", () => {440+ const base = new Map([441+ ["first", {id: 1, a: 1}],442+ ["second", {id: 2, a: 1}]443+ ])444+ const result = produce(base, draft => {445+ draft.forEach(item => {446+ item.a = 100447 })450- expect(result).not.toBe(base)451- expect(result.get("first").a).toEqual(100)452- expect(result.get("second").a).toEqual(100)448 })449+ expect(result).not.toBe(base)450+ expect(result.get("first").a).toEqual(100)451+ expect(result.get("second").a).toEqual(100)452+ })453 455- it("can assign by key", () => {456- const nextState = produce(baseState, s => {457- // Map.prototype.set should return the Map itself458- const res = s.aMap.set("force", true)459- expect(res).toBe(s.aMap[DRAFT_STATE].draft)460- })461- expect(nextState).not.toBe(baseState)462- expect(nextState.aMap).not.toBe(baseState.aMap)463- expect(nextState.aMap.get("force")).toEqual(true)454+ it("can assign by key", () => {455+ const nextState = produce(baseState, s => {456+ // Map.prototype.set should return the Map itself457+ const res = s.aMap.set("force", true)458+ expect(res).toBe(s.aMap[DRAFT_STATE].draft)459 })460+ expect(nextState).not.toBe(baseState)461+ expect(nextState.aMap).not.toBe(baseState.aMap)462+ expect(nextState.aMap.get("force")).toEqual(true)463+ })464 466- it("can assign by a non-primitive key", () => {467- const key = {prop: "val"}468- const value = {id: 1, a: 1}469- const base = new Map([[key, value]])470- const nextState = produce(base, s => {471- s.set(key, true)472- })473- expect(nextState).not.toBe(base)474- expect(base.get(key)).toEqual(value)475- expect(nextState.get(key)).toEqual(true)465+ it("can assign by a non-primitive key", () => {466+ const key = {prop: "val"}467+ const value = {id: 1, a: 1}468+ const base = new Map([[key, value]])469+ const nextState = produce(base, s => {470+ s.set(key, true)471 })472+ expect(nextState).not.toBe(base)473+ expect(base.get(key)).toEqual(value)474+ expect(nextState.get(key)).toEqual(true)475+ })476 478- it("state stays the same if the the same item is assigned by key", () => {479- const nextState = produce(baseState, s => {480- s.aMap.set("jediTotal", 42)481- })482- expect(nextState).toBe(baseState)483- expect(nextState.aMap).toBe(baseState.aMap)477+ it("state stays the same if the the same item is assigned by key", () => {478+ const nextState = produce(baseState, s => {479+ s.aMap.set("jediTotal", 42)480 })481+ expect(nextState).toBe(baseState)482+ expect(nextState.aMap).toBe(baseState.aMap)483+ })484 486- it("returns 'size'", () => {487- const nextState = produce(baseState, s => {488- s.aMap.set("newKey", true)489- expect(s.aMap.size).toBe(baseState.aMap.size + 1)490- })491- expect(nextState).not.toBe(baseState)492- expect(nextState.aMap).not.toBe(baseState.aMap)493- expect(nextState.aMap.get("newKey")).toEqual(true)494- expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)485+ it("returns 'size'", () => {486+ const nextState = produce(baseState, s => {487+ s.aMap.set("newKey", true)488+ expect(s.aMap.size).toBe(baseState.aMap.size + 1)489 })490+ expect(nextState).not.toBe(baseState)491+ expect(nextState.aMap).not.toBe(baseState.aMap)492+ expect(nextState.aMap.get("newKey")).toEqual(true)493+ expect(nextState.aMap.size).toEqual(baseState.aMap.size + 1)494+ })495 497- it("can use 'delete' to remove items", () => {498- const nextState = produce(baseState, s => {499- expect(s.aMap.has("jedi")).toBe(true)500- s.aMap.delete("jedi")501- expect(s.aMap.has("jedi")).toBe(false)502- })503- expect(nextState.aMap).not.toBe(baseState.aMap)504- expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)505- expect(baseState.aMap.has("jedi")).toBe(true)506- expect(nextState.aMap.has("jedi")).toBe(false)496+ it("can use 'delete' to remove items", () => {497+ const nextState = produce(baseState, s => {498+ expect(s.aMap.has("jedi")).toBe(true)499+ s.aMap.delete("jedi")500+ expect(s.aMap.has("jedi")).toBe(false)501 })502+ expect(nextState.aMap).not.toBe(baseState.aMap)503+ expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)504+ expect(baseState.aMap.has("jedi")).toBe(true)505+ expect(nextState.aMap.has("jedi")).toBe(false)506+ })507 509- it("can use 'clear' to remove items", () => {510- const nextState = produce(baseState, s => {511- expect(s.aMap.size).not.toBe(0)512- s.aMap.clear()513- expect(s.aMap.size).toBe(0)514- })515- expect(nextState.aMap).not.toBe(baseState.aMap)516- expect(baseState.aMap.size).not.toBe(0)517- expect(nextState.aMap.size).toBe(0)508+ it("can use 'clear' to remove items", () => {509+ const nextState = produce(baseState, s => {510+ expect(s.aMap.size).not.toBe(0)511+ s.aMap.clear()512+ expect(s.aMap.size).toBe(0)513 })514+ expect(nextState.aMap).not.toBe(baseState.aMap)515+ expect(baseState.aMap.size).not.toBe(0)516+ expect(nextState.aMap.size).toBe(0)517+ })518 520- it("support 'has'", () => {521- const nextState = produce(baseState, s => {522- expect(s.aMap.has("newKey")).toBe(false)523- s.aMap.set("newKey", true)524- expect(s.aMap.has("newKey")).toBe(true)525- })526- expect(nextState).not.toBe(baseState)527- expect(nextState.aMap).not.toBe(baseState.aMap)528- expect(baseState.aMap.has("newKey")).toBe(false)529- expect(nextState.aMap.has("newKey")).toBe(true)519+ it("support 'has'", () => {520+ const nextState = produce(baseState, s => {521+ expect(s.aMap.has("newKey")).toBe(false)522+ s.aMap.set("newKey", true)523+ expect(s.aMap.has("newKey")).toBe(true)524 })525+ expect(nextState).not.toBe(baseState)526+ expect(nextState.aMap).not.toBe(baseState.aMap)527+ expect(baseState.aMap.has("newKey")).toBe(false)528+ expect(nextState.aMap.has("newKey")).toBe(true)529+ })530 532- it("supports nested maps", () => {533- const base = new Map([534- ["first", new Map([["second", {prop: "test"}]])]535- ])536- const result = produce(base, draft => {537- draft.get("first").get("second").prop = "test1"538- })539- expect(result).not.toBe(base)540- expect(result.get("first")).not.toBe(base.get("first"))541- expect(result.get("first").get("second")).not.toBe(542- base.get("first").get("second")543- )544- expect(base.get("first").get("second").prop).toBe("test")545- expect(result.get("first").get("second").prop).toBe("test1")531+ it("supports nested maps", () => {532+ const base = new Map([533+ ["first", new Map([["second", {prop: "test"}]])]534+ ])535+ const result = produce(base, draft => {536+ draft.get("first").get("second").prop = "test1"537 })538+ expect(result).not.toBe(base)539+ expect(result.get("first")).not.toBe(base.get("first"))540+ expect(result.get("first").get("second")).not.toBe(541+ base.get("first").get("second")542+ )543+ expect(base.get("first").get("second").prop).toBe("test")544+ expect(result.get("first").get("second").prop).toBe("test1")545 })546+ })547 549- describe("set drafts", () => {550- it("supports iteration", () => {551- const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])552- const findById = (set, id) => {553- for (const item of set) {554- if (item.id === id) return item555- }556- return null548+ describe("set drafts", () => {549+ it("supports iteration", () => {550+ const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])551+ const findById = (set, id) => {552+ for (const item of set) {553+ if (item.id === id) return item554 }558- const result = produce(base, draft => {559- const obj1 = findById(draft, 1)560- const obj2 = findById(draft, 2)561- obj1.a = 2562- obj2.a = 2563- })564- expect(result).not.toBe(base)565- expect(base).toEqual(566- new Set([{id: 1, a: 1}, {id: 2, a: 1}])567- )568- expect(result).toEqual(569- new Set([{id: 1, a: 2}, {id: 2, a: 2}])570- )555+ return null556+ }557+ const result = produce(base, draft => {558+ const obj1 = findById(draft, 1)559+ const obj2 = findById(draft, 2)560+ obj1.a = 2561+ obj2.a = 2562 })563+ expect(result).not.toBe(base)564+ expect(base).toEqual(new Set([{id: 1, a: 1}, {id: 2, a: 1}]))565+ expect(result).toEqual(new Set([{id: 1, a: 2}, {id: 2, a: 2}]))566+ })567 573- it("supports 'entries'", () => {574- const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])575- const findById = (set, id) => {576- for (const [item1, item2] of set.entries()) {577- expect(item1).toBe(item2)578- if (item1.id === id) return item1579- }580- return null568+ it("supports 'entries'", () => {569+ const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])570+ const findById = (set, id) => {571+ for (const [item1, item2] of set.entries()) {572+ expect(item1).toBe(item2)573+ if (item1.id === id) return item1574 }582- const result = produce(base, draft => {583- const obj1 = findById(draft, 1)584- const obj2 = findById(draft, 2)585- obj1.a = 2586- obj2.a = 2587- })588- expect(result).not.toBe(base)589- expect(base).toEqual(590- new Set([{id: 1, a: 1}, {id: 2, a: 1}])591- )592- expect(result).toEqual(593- new Set([{id: 1, a: 2}, {id: 2, a: 2}])594- )575+ return null576+ }577+ const result = produce(base, draft => {578+ const obj1 = findById(draft, 1)579+ const obj2 = findById(draft, 2)580+ obj1.a = 2581+ obj2.a = 2582 })583+ expect(result).not.toBe(base)584+ expect(base).toEqual(new Set([{id: 1, a: 1}, {id: 2, a: 1}]))585+ expect(result).toEqual(new Set([{id: 1, a: 2}, {id: 2, a: 2}]))586+ })587 597- it("supports 'values'", () => {598- const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])599- const findById = (set, id) => {600- for (const item of set.values()) {601- if (item.id === id) return item602- }603- return null588+ it("supports 'values'", () => {589+ const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])590+ const findById = (set, id) => {591+ for (const item of set.values()) {592+ if (item.id === id) return item593 }605- const result = produce(base, draft => {606- const obj1 = findById(draft, 1)607- const obj2 = findById(draft, 2)608- obj1.a = 2609- obj2.a = 2610- })611- expect(result).not.toBe(base)612- expect(base).toEqual(613- new Set([{id: 1, a: 1}, {id: 2, a: 1}])614- )615- expect(result).toEqual(616- new Set([{id: 1, a: 2}, {id: 2, a: 2}])617- )594+ return null595+ }596+ const result = produce(base, draft => {597+ const obj1 = findById(draft, 1)598+ const obj2 = findById(draft, 2)599+ obj1.a = 2600+ obj2.a = 2601 })602+ expect(result).not.toBe(base)603+ expect(base).toEqual(new Set([{id: 1, a: 1}, {id: 2, a: 1}]))604+ expect(result).toEqual(new Set([{id: 1, a: 2}, {id: 2, a: 2}]))605+ })606 620- it("supports 'keys'", () => {621- const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])622- const findById = (set, id) => {623- for (const item of set.keys()) {624- if (item.id === id) return item625- }626- return null607+ it("supports 'keys'", () => {608+ const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])609+ const findById = (set, id) => {610+ for (const item of set.keys()) {611+ if (item.id === id) return item612 }628- const result = produce(base, draft => {629- const obj1 = findById(draft, 1)630- const obj2 = findById(draft, 2)631- obj1.a = 2632- obj2.a = 2633- })634- expect(result).not.toBe(base)635- expect(base).toEqual(636- new Set([{id: 1, a: 1}, {id: 2, a: 1}])637- )638- expect(result).toEqual(639- new Set([{id: 1, a: 2}, {id: 2, a: 2}])640- )613+ return null614+ }615+ const result = produce(base, draft => {616+ const obj1 = findById(draft, 1)617+ const obj2 = findById(draft, 2)618+ obj1.a = 2619+ obj2.a = 2620 })621+ expect(result).not.toBe(base)622+ expect(base).toEqual(new Set([{id: 1, a: 1}, {id: 2, a: 1}]))623+ expect(result).toEqual(new Set([{id: 1, a: 2}, {id: 2, a: 2}]))624+ })625 643- it("supports forEach with mutation after reads", () => {644- const base = new Set([{id: 1, a: 1}, {id: 2, a: 2}])645- const result = produce(base, draft => {646- let sum1 = 0647- draft.forEach(({a}) => {648- sum1 += a649- })650- expect(sum1).toBe(3)651- let sum2 = 0652- draft.forEach(item => {653- item.a += 10654- sum2 += item.a655- })656- expect(sum2).toBe(23)626+ it("supports forEach with mutation after reads", () => {627+ const base = new Set([{id: 1, a: 1}, {id: 2, a: 2}])628+ const result = produce(base, draft => {629+ let sum1 = 0630+ draft.forEach(({a}) => {631+ sum1 += a632 })658- expect(result).not.toBe(base)659- expect(base).toEqual(660- new Set([{id: 1, a: 1}, {id: 2, a: 2}])661- )662- expect(result).toEqual(663- new Set([{id: 1, a: 11}, {id: 2, a: 12}])664- )633+ expect(sum1).toBe(3)634+ let sum2 = 0635+ draft.forEach(item => {636+ item.a += 10637+ sum2 += item.a638+ })639+ expect(sum2).toBe(23)640 })641+ expect(result).not.toBe(base)642+ expect(base).toEqual(new Set([{id: 1, a: 1}, {id: 2, a: 2}]))643+ expect(result).toEqual(644+ new Set([{id: 1, a: 11}, {id: 2, a: 12}])645+ )646+ })647 667- it("state stays the same if the same item is added", () => {668- const nextState = produce(baseState, s => {669- s.aSet.add("Luke")670- })671- expect(nextState).toBe(baseState)672- expect(nextState.aSet).toBe(baseState.aSet)648+ it("state stays the same if the same item is added", () => {649+ const nextState = produce(baseState, s => {650+ s.aSet.add("Luke")651 })652+ expect(nextState).toBe(baseState)653+ expect(nextState.aSet).toBe(baseState.aSet)654+ })655 675- it("can add new items", () => {676- const nextState = produce(baseState, s => {677- // Set.prototype.set should return the Set itself678- const res = s.aSet.add("force")679- expect(res).toBe(s.aSet[DRAFT_STATE].draft)680- })681- expect(nextState).not.toBe(baseState)682- expect(nextState.aSet).not.toBe(baseState.aSet)683- expect(baseState.aSet.has("force")).toBe(false)684- expect(nextState.aSet.has("force")).toBe(true)656+ it("can add new items", () => {657+ const nextState = produce(baseState, s => {658+ // Set.prototype.set should return the Set itself659+ const res = s.aSet.add("force")660+ expect(res).toBe(s.aSet[DRAFT_STATE].draft)661 })662+ expect(nextState).not.toBe(baseState)663+ expect(nextState.aSet).not.toBe(baseState.aSet)664+ expect(baseState.aSet.has("force")).toBe(false)665+ expect(nextState.aSet.has("force")).toBe(true)666+ })667 687- it("returns 'size'", () => {688- const nextState = produce(baseState, s => {689- s.aSet.add("newKey")690- expect(s.aSet.size).toBe(baseState.aSet.size + 1)691- })692- expect(nextState).not.toBe(baseState)693- expect(nextState.aSet).not.toBe(baseState.aSet)694- expect(nextState.aSet.has("newKey")).toBe(true)695- expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)668+ it("returns 'size'", () => {669+ const nextState = produce(baseState, s => {670+ s.aSet.add("newKey")671+ expect(s.aSet.size).toBe(baseState.aSet.size + 1)672 })673+ expect(nextState).not.toBe(baseState)674+ expect(nextState.aSet).not.toBe(baseState.aSet)675+ expect(nextState.aSet.has("newKey")).toBe(true)676+ expect(nextState.aSet.size).toEqual(baseState.aSet.size + 1)677+ })678 698- it("can use 'delete' to remove items", () => {699- const nextState = produce(baseState, s => {700- expect(s.aSet.has("Luke")).toBe(true)701- s.aSet.delete("Luke")702- expect(s.aSet.has("Luke")).toBe(false)703- })704- expect(nextState.aSet).not.toBe(baseState.aSet)705- expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)706- expect(baseState.aSet.has("Luke")).toBe(true)707- expect(nextState.aSet.has("Luke")).toBe(false)679+ it("can use 'delete' to remove items", () => {680+ const nextState = produce(baseState, s => {681+ expect(s.aSet.has("Luke")).toBe(true)682+ s.aSet.delete("Luke")683+ expect(s.aSet.has("Luke")).toBe(false)684 })685+ expect(nextState.aSet).not.toBe(baseState.aSet)686+ expect(nextState.aSet.size).toBe(baseState.aSet.size - 1)687+ expect(baseState.aSet.has("Luke")).toBe(true)688+ expect(nextState.aSet.has("Luke")).toBe(false)689+ })690 710- it("can use 'clear' to remove items", () => {711- const nextState = produce(baseState, s => {712- expect(s.aSet.size).not.toBe(0)713- s.aSet.clear()714- expect(s.aSet.size).toBe(0)715- })716- expect(nextState.aSet).not.toBe(baseState.aSet)717- expect(baseState.aSet.size).not.toBe(0)718- expect(nextState.aSet.size).toBe(0)691+ it("can use 'clear' to remove items", () => {692+ const nextState = produce(baseState, s => {693+ expect(s.aSet.size).not.toBe(0)694+ s.aSet.clear()695+ expect(s.aSet.size).toBe(0)696 })697+ expect(nextState.aSet).not.toBe(baseState.aSet)698+ expect(baseState.aSet.size).not.toBe(0)699+ expect(nextState.aSet.size).toBe(0)700+ })701 721- it("supports 'has'", () => {722- const nextState = produce(baseState, s => {723- expect(s.aSet.has("newKey")).toBe(false)724- s.aSet.add("newKey")725- expect(s.aSet.has("newKey")).toBe(true)726- })727- expect(nextState).not.toBe(baseState)728- expect(nextState.aSet).not.toBe(baseState.aSet)729- expect(baseState.aSet.has("newKey")).toBe(false)730- expect(nextState.aSet.has("newKey")).toBe(true)702+ it("supports 'has'", () => {703+ const nextState = produce(baseState, s => {704+ expect(s.aSet.has("newKey")).toBe(false)705+ s.aSet.add("newKey")706+ expect(s.aSet.has("newKey")).toBe(true)707 })708+ expect(nextState).not.toBe(baseState)709+ expect(nextState.aSet).not.toBe(baseState.aSet)710+ expect(baseState.aSet.has("newKey")).toBe(false)711+ expect(nextState.aSet.has("newKey")).toBe(true)712+ })713 733- it("supports nested sets", () => {734- const base = new Set([new Set(["Serenity"])])735- const result = produce(base, draft => {736- draft.forEach(nestedItem => nestedItem.add("Firefly"))737- })738- expect(result).not.toBe(base)739- expect(base).toEqual(new Set([new Set(["Serenity"])]))740- expect(result).toEqual(741- new Set([new Set(["Serenity", "Firefly"])])742- )714+ it("supports nested sets", () => {715+ const base = new Set([new Set(["Serenity"])])716+ const result = produce(base, draft => {717+ draft.forEach(nestedItem => nestedItem.add("Firefly"))718 })719+ expect(result).not.toBe(base)720+ expect(base).toEqual(new Set([new Set(["Serenity"])]))721+ expect(result).toEqual(722+ new Set([new Set(["Serenity", "Firefly"])])723+ )724 })745- }725+ })726 727 it("supports `immerable` symbol on constructor", () => {728 class One {}729 One[immerable] = true982 unmodified linesmodified:__tests__/hooks.js+114 added-125 removed
118 unmodified lines119 expect(calls).toEqual(["b", "d", "c", "x"])120 })121 })122 123- if (useProxies) {124- describe("when draft is a Map", () => {125- test("assign", () => {126- const key1 = {prop: "val1"}127- const key2 = {prop: "val2"}128- produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {129- s.set("a", 10)130- s.set(key1, 11)123+ describe("when draft is a Map", () => {124+ test("assign", () => {125+ const key1 = {prop: "val1"}126+ const key2 = {prop: "val2"}127+ produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {128+ s.set("a", 10)129+ s.set(key1, 11)131- })132- expectCalls(onAssign)130 })134- test("assign (no change)", () => {135- produce(new Map([["a", 0]]), s => {136- s.set("a", 0)137- })138- expect(onAssign).not.toBeCalled()131+ expectCalls(onAssign)132+ })133+ test("assign (no change)", () => {134+ produce(new Map([["a", 0]]), s => {135+ s.set("a", 0)136 })140- test("delete", () => {141- produce(new Map([["a", 0]]), s => {142- s.delete("a")143- })144- expect(onAssign).not.toBeCalled()137+ expect(onAssign).not.toBeCalled()138+ })139+ test("delete", () => {140+ produce(new Map([["a", 0]]), s => {141+ s.delete("a")142 })146- test("nested assignments", () => {147- const key1 = {prop: "val1"}148- produce(149- new Map([150- [151- "a",152- new Map([153- [154- key1,155- new Map([["b", 1], ["c", 1], ["d", 1]])156- ]157- ])158- ]159- ]),160- s => {161- const nested = s.get("a").get(key1)162- nested.set("b", 2)163- nested.delete("c")164- nested.set("d", 1) // no-op165- }166- )167- expectCalls(onAssign)168- })143+ expect(onAssign).not.toBeCalled()144 })145+ test("nested assignments", () => {146+ const key1 = {prop: "val1"}147+ produce(148+ new Map([149+ [150+ "a",151+ new Map([152+ [key1, new Map([["b", 1], ["c", 1], ["d", 1]])]153+ ])154+ ]155+ ]),156+ s => {157+ const nested = s.get("a").get(key1)158+ nested.set("b", 2)159+ nested.delete("c")160+ nested.set("d", 1) // no-op161+ }162+ )163+ expectCalls(onAssign)164+ })165+ })166 171- describe("when draft is a Set", () => {172- test("assign", () => {173- produce({a: new Set([1, 2, 3])}, s => {174- s.a.add(4)175- })176- expectCalls(onAssign)167+ describe("when draft is a Set", () => {168+ test("assign", () => {169+ produce({a: new Set([1, 2, 3])}, s => {170+ s.a.add(4)171 })178- test("assign (no change)", () => {179- produce({a: new Set([1, 2, 3])}, s => {180- s.a.add(3)181- })182- expect(onAssign).not.toBeCalled()172+ expectCalls(onAssign)173+ })174+ test("assign (no change)", () => {175+ produce({a: new Set([1, 2, 3])}, s => {176+ s.a.add(3)177 })184- // Any mutation of a Set results in a new assignment. Including deletes.185- test("delete", () => {186- produce({a: new Set([1, 2, 3])}, s => {187- s.a.delete(1)188- })189- expectCalls(onAssign)178+ expect(onAssign).not.toBeCalled()179+ })180+ // Any mutation of a Set results in a new assignment. Including deletes.181+ test("delete", () => {182+ produce({a: new Set([1, 2, 3])}, s => {183+ s.a.delete(1)184 })191- test("nested assignments", () => {192- const val1 = {prop: "val1"}193- produce({a: new Set(["a", new Set([val1, 1])])}, s => {194- let nested195- s.a.forEach(value => {196- if (isSet(value)) {197- nested = value198- }199- })200- nested.add(2)201- nested.delete(val1)202- nested.add(1) // no-op185+ expectCalls(onAssign)186+ })187+ test("nested assignments", () => {188+ const val1 = {prop: "val1"}189+ produce({a: new Set(["a", new Set([val1, 1])])}, s => {190+ let nested191+ s.a.forEach(value => {192+ if (isSet(value)) {193+ nested = value194+ }195 })204- expectCalls(onAssign)196+ nested.add(2)197+ nested.delete(val1)198+ nested.add(1) // no-op199 })200+ expectCalls(onAssign)201 })207- }202+ })203 })204 205 describe("onDelete()", () => {206 useSharedTests(() => onDelete)38 unmodified lines245 expectCalls(onDelete)246 })247 })248 254- if (useProxies) {255- describe("when draft is a Map -", () => {256- test("delete", () => {257- const key1 = {prop: "val1"}258- const key2 = {prop: "val2"}259- produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {260- s.delete("a")261- s.delete(key1)249+ describe("when draft is a Map -", () => {250+ test("delete", () => {251+ const key1 = {prop: "val1"}252+ const key2 = {prop: "val2"}253+ produce(new Map([["a", 0], [key1, 1], [key2, 2]]), s => {254+ s.delete("a")255+ s.delete(key1)262- })263- expectCalls(onDelete)256 })265- test("delete (no change)", () => {266- produce(new Map(), s => {267- s.delete("a")268- })269- expect(onDelete).not.toBeCalled()257+ expectCalls(onDelete)258+ })259+ test("delete (no change)", () => {260+ produce(new Map(), s => {261+ s.delete("a")262 })271- test("nested deletions", () => {272- const key1 = {prop: "val1"}273- produce(274- new Map([275- ["a", new Map([[key1, new Map([["b", 1]])]])]276- ]),277- s => {278- s.get("a")279- .get(key1)280- .delete("b")281- }282- )283- expectCalls(onDelete)284- })263+ expect(onDelete).not.toBeCalled()264 })265+ test("nested deletions", () => {266+ const key1 = {prop: "val1"}267+ produce(268+ new Map([["a", new Map([[key1, new Map([["b", 1]])]])]]),269+ s => {270+ s.get("a")271+ .get(key1)272+ .delete("b")273+ }274+ )275+ expectCalls(onDelete)276+ })277+ })278 287- describe("when draft is a Set -", () => {288- test("delete", () => {289- produce({a: new Set([1])}, s => {290- s.a.delete(1)291- })292- expect(onDelete).not.toBeCalled()279+ describe("when draft is a Set -", () => {280+ test("delete", () => {281+ produce({a: new Set([1])}, s => {282+ s.a.delete(1)283 })284+ expect(onDelete).not.toBeCalled()285 })295- }286+ })287 })288 289 describe("onCopy()", () => {290 let calls12 unmodified lines303 })304 expect(calls).toShallowEqual([base.a.b, base.a, base])305 })306 316- if (useProxies) {317- it("is called in the right order for Maps", () => {318- const base = new Map([["a", new Map([["b", 0]])]])319- produce(base, s => {320- s.get("a").delete("b")307+ it("is called in the right order for Maps", () => {308+ const base = new Map([["a", new Map([["b", 0]])]])309+ produce(base, s => {310+ s.get("a").delete("b")321- })322- expect(calls).toShallowEqual([base.get("a"), base])311 })312+ expect(calls).toShallowEqual([base.get("a"), base])313+ })314 325- it("is called in the right order for Sets", () => {326- const item1 = {a: 0}327- const base = new Set([item1])328- produce(base, s => {329- s.forEach(item => item.a++)330- })331- expect(calls).toShallowEqual([item1, base])315+ it("is called in the right order for Sets", () => {316+ const item1 = {a: 0}317+ const base = new Set([item1])318+ produce(base, s => {319+ s.forEach(item => item.a++)320 })333- }321+ expect(calls).toShallowEqual([item1, base])322+ })323 })324 325 function useSharedTests(getHook) {326 it("is called before the parent is frozen", () => {34 unmodified linesmodified:__tests__/patch.js+22 added-48 removed
1 unmodified line2 import produce, {setUseProxies, applyPatches} from "../src/index"3 4 jest.setTimeout(1000)5 6-function runPatchTest(7- base,8- producer,9- patches,10- inversePathes,11- proxyOnly = false12-) {6+function runPatchTest(base, producer, patches, inversePathes) {7 function runPatchTestHelper() {8 let recordedPatches9 let recordedInversePatches10 17 unmodified lines28 })29 }30 31 describe(`proxy`, () => {38- beforeAll(() => setUseProxies(true))32+ setUseProxies(true)33 runPatchTestHelper()34 })35 36 describe(`es5`, () => {43- if (!proxyOnly) {44- beforeAll(() => setUseProxies(false))45- runPatchTestHelper()46- }37+ setUseProxies(false)38+ runPatchTestHelper()39 })40 }41 42 describe("applyPatches", () => {72 unmodified lines115 d => {116 d.get("x").y++117 },118 [{op: "replace", path: ["x", "y"], value: 5}],127- [{op: "replace", path: ["x", "y"], value: 4}],128- true119+ [{op: "replace", path: ["x", "y"], value: 4}]120 )121 })122 123 describe("simple assignment - 5", () => {2 unmodified lines126 d => {127 d.x.set("y", 5)128 },129 [{op: "replace", path: ["x", "y"], value: 5}],139- [{op: "replace", path: ["x", "y"], value: 4}],140- true130+ [{op: "replace", path: ["x", "y"], value: 4}]131 )132 })133 134 describe("simple assignment - 6", () => {7 unmodified lines142 [143 {op: "replace", path: ["x"], value: 2},144 {op: "add", path: ["y"], value: 3}145 ],156- [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}],157- true146+ [{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}]147 )148 })149 150 describe("simple assignment - 7", () => {11 unmodified lines162 ],163 [164 {op: "replace", path: ["x", key1], value: 4},165 {op: "remove", path: ["x", key2]}177- ],178- true166+ ]167 )168 })169 170 describe("delete 1", () => {12 unmodified lines183 d => {184 d.delete("x")185 },186 [{op: "remove", path: ["x"]}],199- [{op: "add", path: ["x"], value: 1}],200- true187+ [{op: "add", path: ["x"], value: 1}]188 )189 })190 191 describe("delete 3", () => {2 unmodified lines194 d => {195 d.x.delete("y")196 },197 [{op: "remove", path: ["x", "y"]}],211- [{op: "add", path: ["x", "y"], value: 1}],212- true198+ [{op: "add", path: ["x", "y"], value: 1}]199 )200 })201 202 describe("delete 5", () => {8 unmodified lines211 [{op: "remove", path: ["x", key1]}, {op: "remove", path: ["x", key2]}],212 [213 {op: "add", path: ["x", key1], value: 1},214 {op: "add", path: ["x", key2], value: 2}229- ],230- true215+ ]216 )217 })218 219 describe("delete 6", () => {2 unmodified lines222 d => {223 d.delete("x")224 },225 [{op: "remove", path: [0], value: "x"}],241- [{op: "add", path: [0], value: "x"}],242- true226+ [{op: "add", path: [0], value: "x"}]227 )228 })229 230 describe("delete 7", () => {2 unmodified lines233 d => {234 d.x.delete("y")235 },236 [{op: "remove", path: ["x", 0], value: "y"}],253- [{op: "add", path: ["x", 0], value: "y"}],254- true237+ [{op: "add", path: ["x", 0], value: "y"}]238 )239 })240 241 describe("renaming properties", () => {24 unmodified lines266 ],267 [268 {op: "remove", path: ["x"]},269 {op: "add", path: ["a"], value: new Map([["b", 1]])}287- ],288- true270+ ]271 )272 })273 274 describe("nested object (with changes)", () => {35 unmodified lines310 ],311 [312 {op: "remove", path: ["x"]},313 {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}332- ],333- true314+ ]315 )316 })317 318 describe("deeply nested object (with changes)", () => {43 unmodified lines362 op: "add",363 path: ["a", "b"],364 value: new Map([["c", 1], ["d", 1]])365 }385- ],386- true366+ ]367 )368 })369 })370 187 unmodified lines558 d => {559 d.add(2)560 },561 [{op: "add", path: [1], value: 2}],582- [{op: "remove", path: [1], value: 2}],583- true562+ [{op: "remove", path: [1], value: 2}]563 )564 })565 566 describe("sets - add, delete, add - 1", () => {4 unmodified lines571 d.delete(2)572 d.add(2)573 },574 [{op: "add", path: [1], value: 2}],596- [{op: "remove", path: [1], value: 2}],597- true575+ [{op: "remove", path: [1], value: 2}]576 )577 })578 579 describe("sets - add, delete, add - 2", () => {4 unmodified lines584 d.delete(2)585 d.add(2)586 },587 [],610- [],611- true588+ []589 )590 })591 592 describe("sets - mutate - 1", () => {20 unmodified lines613 {op: "remove", path: [1], value: {id: 2, val: "you"}},614 {op: "remove", path: [0], value: {id: 1, val: "rock"}},615 {op: "add", path: [1], value: {id: 2, val: "will"}},616 {op: "add", path: [0], value: {id: 1, val: "We"}}640- ],641- true617+ ]618 )619 })620 621 describe("simple replacement", () => {51 unmodified lines673 d.set("x", 4)674 d.set("x", 3)675 },676 [],701- [],702- true677+ []678 )679 })680 681 describe("same value replacement - 6", () => {3 unmodified lines685 d.delete("x")686 d.add("x")687 },688 [],714- [],715- true689+ []690 )691 })692 693 describe("simple delete", () => {13 unmodified linesmodified:src/common.js+70 added-2 removed
129 unmodified linesExpand all130131132133134135136135137138139140141142139143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208129 unmodified linesExpand all return x !== x && y !== y }}
const hasMap = typeof Map !== "undefined"
export function isMap(target) { return target instanceof Map return hasMap && target instanceof Map}
const hasSet = typeof Set !== "undefined"
export function isSet(target) { return target instanceof Set return hasSet && target instanceof Set}
export function makeIterable(next) { let self return (self = { [Symbol.iterator]: () => self, next })}
/** Map.prototype.values _-or-_ Map.prototype.entries */export function iterateMapValues(state, prop, receiver) { const isEntries = prop !== "values" return () => { const iterator = latest(state)[Symbol.iterator]() return makeIterable(() => { const result = iterator.next() if (!result.done) { const [key] = result.value const value = receiver.get(key) result.value = isEntries ? [key, value] : value } return result }) }}
export function makeIterateSetValues(createProxy) { function iterateSetValues(state, prop) { const isEntries = prop === "entries" return () => { const iterator = latest(state)[Symbol.iterator]() return makeIterable(() => { const result = iterator.next() if (!result.done) { const value = wrapSetValue(state, result.value) result.value = isEntries ? [value, value] : value } return result }) } }
function wrapSetValue(state, value) { const key = original(value) || value let draft = state.drafts.get(key) if (!draft) { if (state.finalized || !isDraftable(value) || state.finalizing) { return value } draft = createProxy(value, state) state.drafts.set(key, draft) if (state.modified) { state.copy.add(draft) } } return draft }
return iterateSetValues}
function latest(state) { return state.copy || state.base}modified:src/es5.js+216 added-10 removed
4 unmodified lines5 is,6 isDraft,7 isDraftable,8 isEnumerable,9+ isMap,10+ isSet,11 shallowCopy,10- DRAFT_STATE12+ DRAFT_STATE,13+ iterateMapValues,14+ makeIterable,15+ makeIterateSetValues16 } from "./common"17 import {ImmerScope} from "./scope"18 14-// property descriptors are recycled to make sure we don't create a get and set closure per property,15-// but share them all instead16-const descriptors = {}17-19 export function willFinalize(scope, result, isReplaced) {20 scope.drafts.forEach(draft => {21 draft[DRAFT_STATE].finalizing = true22 })12 unmodified lines35 36 export function createProxy(base, parent) {37 const isArray = Array.isArray(base)38 const draft = clonePotentialDraft(base)38- each(draft, prop => {39- proxyProperty(draft, prop, isArray || isEnumerable(base, prop))40- })39 40+ if (isMap(base)) {41+ proxyMap(draft)42+ } else if (isSet(base)) {43+ proxySet(draft)44+ } else {45+ each(draft, prop => {46+ proxyProperty(draft, prop, isArray || isEnumerable(base, prop))47+ })48+ }49+50 // See "proxy.js" for property documentation.51 const scope = parent ? parent.scope : ImmerScope.current52 const state = {53 scope,54 modified: false,55 finalizing: false, // es5 only56 finalized: false,49- assigned: {},57+ assigned: isMap(base) ? new Map() : {},58 parent,59 base,60 draft,61+ drafts: isSet(base) ? new Map() : null,62 copy: null,63 revoke,64 revoked: false // es5 only65 }67 unmodified lines133 }134 return shallowCopy(base)135 }136 137+// property descriptors are recycled to make sure we don't create a get and set closure per property,138+// but share them all instead139+const descriptors = {}140+141 function proxyProperty(draft, prop, enumerable) {142 let desc = descriptors[prop]143 if (desc) {144 desc.enumerable = enumerable11 unmodified lines156 }157 Object.defineProperty(draft, prop, desc)158 }159 160+function proxyMap(target) {161+ Object.defineProperties(target, mapTraps)162+163+ if (typeof Symbol !== undefined) {164+ Object.defineProperty(165+ target,166+ Symbol.iterator,167+ proxyMethod(iterateMapValues)168+ )169+ }170+}171+172+const mapTraps = finalizeTraps({173+ size: state => latest(state).size,174+ has: state => key => latest(state).has(key),175+ set: state => (key, value) => {176+ if (latest(state).get(key) !== value) {177+ prepareCopy(state)178+ markChanged(state)179+ state.assigned.set(key, true)180+ state.copy.set(key, value)181+ }182+ return state.draft183+ },184+ delete: state => key => {185+ prepareCopy(state)186+ markChanged(state)187+ state.assigned.set(key, false)188+ state.copy.delete(key)189+ return false190+ },191+ clear: state => () => {192+ if (!state.copy) {193+ prepareCopy(state)194+ }195+ markChanged(state)196+ state.assigned = new Map()197+ for (const key of latest(state).keys()) {198+ state.assigned.set(key, false)199+ }200+ return state.copy.clear()201+ },202+ forEach: (state, key, reciever) => cb => {203+ latest(state).forEach((value, key, map) => {204+ cb(reciever.get(key), key, map)205+ })206+ },207+ get: state => key => {208+ const value = latest(state).get(key)209+210+ if (state.finalizing || state.finalized || !isDraftable(value)) {211+ return value212+ }213+214+ if (value !== state.base.get(key)) {215+ return value216+ }217+ const draft = createProxy(value, state)218+ prepareCopy(state)219+ state.copy.set(key, draft)220+ return draft221+ },222+ keys: state => () => latest(state).keys(),223+ values: iterateMapValues,224+ entries: iterateMapValues225+})226+227+function proxySet(target) {228+ Object.defineProperties(target, setTraps)229+230+ if (typeof Symbol !== undefined) {231+ Object.defineProperty(232+ target,233+ Symbol.iterator,234+ proxyMethod(iterateSetValues)235+ )236+ }237+}238+239+const iterateSetValues = makeIterateSetValues(createProxy)240+241+const setTraps = finalizeTraps({242+ size: state => {243+ return latest(state).size244+ },245+ add: state => value => {246+ if (!latest(state).has(value)) {247+ markChanged(state)248+ if (!state.copy) {249+ prepareCopy(state)250+ }251+ state.copy.add(value)252+ }253+ return state.draft254+ },255+ delete: state => value => {256+ markChanged(state)257+ if (!state.copy) {258+ prepareCopy(state)259+ }260+ return state.copy.delete(value)261+ },262+ has: state => key => {263+ return latest(state).has(key)264+ },265+ clear: state => () => {266+ markChanged(state)267+ if (!state.copy) {268+ prepareCopy(state)269+ }270+ return state.copy.clear()271+ },272+ keys: iterateSetValues,273+ entries: iterateSetValues,274+ values: iterateSetValues,275+ forEach: state => (cb, thisArg) => {276+ const iterator = iterateSetValues(state)()277+ let result = iterator.next()278+ while (!result.done) {279+ cb.call(thisArg, result.value, result.value, state.draft)280+ result = iterator.next()281+ }282+ }283+})284+285+function finalizeTraps(traps) {286+ return Object.keys(traps).reduce(function(acc, key) {287+ const builder = key === "size" ? proxyAttr : proxyMethod288+ acc[key] = builder(traps[key], key)289+ return acc290+ }, {})291+}292+293+function proxyAttr(fn) {294+ return {295+ get() {296+ const state = this[DRAFT_STATE]297+ assertUnrevoked(state)298+ return fn(state)299+ }300+ }301+}302+303+function proxyMethod(trap, key) {304+ return {305+ get() {306+ return (...args) => {307+ const state = this[DRAFT_STATE]308+ assertUnrevoked(state)309+ return trap(state, key, state.draft)(...args)310+ }311+ }312+ }313+}314+315 function assertUnrevoked(state) {316 if (state.revoked === true)317 throw new Error(318 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +11 unmodified lines330 const state = drafts[i][DRAFT_STATE]331 if (!state.modified) {332 if (Array.isArray(state.base)) {333 if (hasArrayChanges(state)) markChanged(state)334+ } else if (isMap(state.base)) {335+ if (hasMapChanges(state)) markChanged(state)336+ } else if (isSet(state.base)) {166- } else if (hasObjectChanges(state)) markChanged(state)337+ if (hasSetChanges(state)) markChanged(state)338+ } else if (hasObjectChanges(state)) {339+ markChanged(state)340+ }341 }342 }343 }344 82 unmodified lines427 // For all other cases, we don't have to compare, as they would have been picked up by the index setters428 return false429 }430 431+function hasMapChanges(state) {432+ const {base, draft} = state433+434+ if (base.size !== draft.size) return true435+436+ // IE11 supports only forEach iteration437+ let hasChanges = false438+ draft.forEach(function(value, key) {439+ if (!hasChanges) {440+ hasChanges = isDraftable(value)441+ ? value.modified442+ : value !== base.get(key)443+ }444+ })445+ return hasChanges446+}447+448+function hasSetChanges(state) {449+ const {base, draft} = state450+451+ if (base.size !== draft.size) return true452+453+ // IE11 supports only forEach iteration454+ let hasChanges = false455+ draft.forEach(function(value, key) {456+ if (!hasChanges) {457+ hasChanges = isDraftable(value) ? value.modified : !base.has(key)458+ }459+ })460+ return hasChanges461+}462+463 function createHiddenProperty(target, prop, value) {464 Object.defineProperty(target, prop, {465 value: value,466 enumerable: false,3 unmodified linesmodified:src/proxy.js+5 added-57 removed
7 unmodified lines8 isDraft,9 isMap,10 isSet,11 shallowCopy,12+ makeIterable,13 DRAFT_STATE,14 assignMap,15 assignSet,15- original16+ original,17+ iterateMapValues,18+ makeIterateSetValues19 } from "./common"20 import {ImmerScope} from "./scope"21 22 // Do nothing before being finalized.234 unmodified lines257 entries: iterateMapValues,258 [Symbol.iterator]: iterateMapValues259 })260 258-/** Map.prototype.values _-or-_ Map.prototype.entries */259-function iterateMapValues(state, prop, receiver) {260- const isEntries = prop !== "values"261- return () => {262- const iterator = latest(state)[Symbol.iterator]()263- return makeIterable(() => {264- const result = iterator.next()265- if (!result.done) {266- const [key] = result.value267- const value = receiver.get(key)268- result.value = isEntries ? [key, value] : value269- }270- return result271- })272- }273-}274-261+const iterateSetValues = makeIterateSetValues(createProxy)262 /**263 * Set drafts264 */265 29 unmodified lines295 entries: iterateSetValues,296 [Symbol.iterator]: iterateSetValues297 })298 312-function iterateSetValues(state, prop) {313- const isEntries = prop === "entries"314- return () => {315- const iterator = latest(state)[Symbol.iterator]()316- return makeIterable(() => {317- const result = iterator.next()318- if (!result.done) {319- const value = wrapSetValue(state, result.value)320- result.value = isEntries ? [value, value] : value321- }322- return result323- })324- }325-}326-327-function wrapSetValue(state, value) {328- const key = original(value) || value329- let draft = state.drafts.get(key)330- if (!draft) {331- if (state.finalized || !isDraftable(value)) {332- return value333- }334- draft = createProxy(value, state)335- state.drafts.set(key, draft)336- if (state.modified) {337- state.copy.add(draft)338- }339- }340- return draft341-}342-299 /**300 * Helpers301 */302 36 unmodified lines339 }340 }341 }342 387-function makeIterable(next) {388- let self389- return (self = {390- [Symbol.iterator]: () => self,391- next392- })393-}394-343 /** Create traps that all use the `Reflect` API on the `latest(state)` */344 function makeReflectTraps(names) {345 return names.reduce((traps, name) => {346 traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)17 unmodified linesmodified:.travis.yml+1 added-1 removed
1 language: node_js2 node_js:3 - "8"4 - "10"5- - "node"5+ # - "node"6 env:7 - NODE_ENV=TEST8 cache:9 yarn: true19 unmodified lines