Skip to content
Myr

feat: add es5 support for Map/Set

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

authored by Andrey Boldyrev · committed by Andrey Boldyrev · parent 13eab80 · Browse files · 8 files+886 added−617 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+886 added−617 removed

  • tests:+597 added−548 removed
  • other:+289 added−69 removed

8 files+886 added−617 removed

modified:__tests__/__snapshots__/hooks.js.snap+106 added
1// Jest Snapshot v1, https://goo.gl/fbAQLP23exports[`hooks (es5) - onAssign() when draft is a Map assign 1`] = `4Array [5 Array [6 "a",7 10,8 ],9 Array [10 Object {11 "prop": "val1",12 },13 11,14 ],15]16`;1718exports[`hooks (es5) - onAssign() when draft is a Map nested assignments 1`] = `19Array [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`;4647exports[`hooks (es5) - onAssign() when draft is a Set assign 1`] = `48Array [49 Array [50 "a",51 Set {52 1,53 2,54 3,55 4,56 },57 ],58]59`;6061exports[`hooks (es5) - onAssign() when draft is a Set delete 1`] = `62Array [63 Array [64 "a",65 Set {66 2,67 3,68 },69 ],70]71`;7273exports[`hooks (es5) - onAssign() when draft is a Set nested assignments 1`] = `74Array [75 Array [76 "a",77 Set {78 "a",79 Set {80 1,81 2,82 },83 },84 ],85]86`;8788exports[`hooks (es5) - onAssign() when draft is an array assign 1`] = `89Array [90 Array [91 0,
100 unmodified lines
192 ],193]194`;195196exports[`hooks (es5) - onDelete() when draft is a Map - delete 1`] = `197Array [198 Array [199 "a",200 ],201 Array [202 Object {203 "prop": "val1",204 },205 ],206]207`;208209exports[`hooks (es5) - onDelete() when draft is a Map - nested deletions 1`] = `210Array [211 Array [212 "b",213 ],214]215`;216217exports[`hooks (es5) - onDelete() when draft is an array - length = 0 1`] = `218Array [219 Array [220 0,
289 unmodified lines
modified:__tests__/base.js+356 added-376 removed
297 unmodified lines
298 })299 }300 })301302 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 })310312 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 })320322 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 })342344 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 })364366 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 })386388 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 })402404 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 })438440 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 })453455 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 })464466 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 })476478 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 })484486 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 })495497 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 })507509 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 })518520 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 })530532 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 })547549 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 })567573 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 })587597 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 })606620 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 })625643 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 })647667 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 })655675 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 })667687 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 })678698 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 })690710 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 })701721 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 })713733 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 })726727 it("supports `immerable` symbol on constructor", () => {728 class One {}729 One[immerable] = true
982 unmodified lines
modified:__tests__/hooks.js+114 added-125 removed
118 unmodified lines
119 expect(calls).toEqual(["b", "d", "c", "x"])120 })121 })122123 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 })166171 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 })204205 describe("onDelete()", () => {206 useSharedTests(() => onDelete)
38 unmodified lines
245 expectCalls(onDelete)246 })247 })248254 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 })278287 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 })288289 describe("onCopy()", () => {290 let calls
12 unmodified lines
303 })304 expect(calls).toShallowEqual([base.a.b, base.a, base])305 })306316 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 })314325 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 })324325 function useSharedTests(getHook) {326 it("is called before the parent is frozen", () => {
34 unmodified lines
modified:__tests__/patch.js+21 added-47 removed
1 unmodified line
2import produce, {setUseProxies, applyPatches} from "../src/index"34jest.setTimeout(1000)56function runPatchTest(7 base,8 producer,9 patches,10 inversePathes,11 proxyOnly = false12) {6function runPatchTest(base, producer, patches, inversePathes) {7 function runPatchTestHelper() {8 let recordedPatches9 let recordedInversePatches10
22 unmodified lines
33 runPatchTestHelper()34 })3536 describe(`es5`, () => {43 if (!proxyOnly) {44 beforeAll(() => setUseProxies(false))45 runPatchTestHelper()37 beforeAll(() => setUseProxies(false))38 runPatchTestHelper()46 }39 })40}4142describe("applyPatches", () => {
72 unmodified lines
115 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})122123describe("simple assignment - 5", () => {
2 unmodified lines
126 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})133134describe("simple assignment - 6", () => {
7 unmodified lines
142 [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})149150describe("simple assignment - 7", () => {
11 unmodified lines
162 ],163 [164 {op: "replace", path: ["x", key1], value: 4},165 {op: "remove", path: ["x", key2]}177 ],178 true166 ]167 )168})169170describe("delete 1", () => {
12 unmodified lines
183 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})190191describe("delete 3", () => {
2 unmodified lines
194 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})201202describe("delete 5", () => {
8 unmodified lines
211 [{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})218219describe("delete 6", () => {
2 unmodified lines
222 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})229230describe("delete 7", () => {
2 unmodified lines
233 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})240241describe("renaming properties", () => {
24 unmodified lines
266 ],267 [268 {op: "remove", path: ["x"]},269 {op: "add", path: ["a"], value: new Map([["b", 1]])}287 ],288 true270 ]271 )272 })273274 describe("nested object (with changes)", () => {
35 unmodified lines
310 ],311 [312 {op: "remove", path: ["x"]},313 {op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}332 ],333 true314 ]315 )316 })317318 describe("deeply nested object (with changes)", () => {
43 unmodified lines
362 op: "add",363 path: ["a", "b"],364 value: new Map([["c", 1], ["d", 1]])365 }385 ],386 true366 ]367 )368 })369})370
187 unmodified lines
558 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})565566describe("sets - add, delete, add - 1", () => {
4 unmodified lines
571 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})578579describe("sets - add, delete, add - 2", () => {
4 unmodified lines
584 d.delete(2)585 d.add(2)586 },587 [],610 [],611 true588 []589 )590})591592describe("sets - mutate - 1", () => {
20 unmodified lines
613 {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})620621describe("simple replacement", () => {
51 unmodified lines
673 d.set("x", 4)674 d.set("x", 3)675 },676 [],701 [],702 true677 []678 )679})680681describe("same value replacement - 6", () => {
3 unmodified lines
685 d.delete("x")686 d.add("x")687 },688 [],714 [],715 true689 []690 )691})692693describe("simple delete", () => {
13 unmodified lines
modified:src/common.js+70 added-2 removed
modified:src/es5.js+213 added-9 removed
4 unmodified lines
5 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"17import {ImmerScope} from "./scope"1814// property descriptors are recycled to make sure we don't create a get and set closure per property,15// but share them all instead16const descriptors = {}1719export function willFinalize(scope, result, isReplaced) {20 scope.drafts.forEach(draft => {21 draft[DRAFT_STATE].finalizing = true22 })
12 unmodified lines
3536export 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 })3940 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 }4950 // See "proxy.js" for property documentation.51 const scope = parent ? parent.scope : ImmerScope.current52 const state = {53 scope,
3 unmodified lines
57 assigned: {},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 lines
133 }134 return shallowCopy(base)135}136137// property descriptors are recycled to make sure we don't create a get and set closure per property,138// but share them all instead139const descriptors = {}140141function proxyProperty(draft, prop, enumerable) {142 let desc = descriptors[prop]143 if (desc) {144 desc.enumerable = enumerable
11 unmodified lines
156 }157 Object.defineProperty(draft, prop, desc)158}159160function proxyMap(target) {161 Object.defineProperties(target, mapTraps)162163 if (typeof Symbol !== undefined) {164 Object.defineProperty(165 target,166 Symbol.iterator,167 proxyMethod(iterateMapValues)168 )169 }170}171172const mapTraps = finalizeTraps({173 size: state => latest(state).size,174 get: state => key => {175 const value = latest(state).get(key)176177 if (state.finalizing || state.finalized || !isDraftable(value)) {178 return value179 }180181 if (value !== state.base.get(key)) {182 return value183 }184 const draft = createProxy(value, state)185 prepareCopy(state)186 state.copy.set(key, draft)187 return draft188 },189 set: state => (key, value) => {190 if (latest(state).get(key) !== value) {191 prepareCopy(state)192 markChanged(state)193 state.copy.set(key, value)194 }195 return state.draft196 },197 delete: state => key => {198 prepareCopy(state)199 markChanged(state)200 state.copy.delete(key)201 return false202 },203 clear: state => () => {204 if (!state.copy) {205 prepareCopy(state)206 }207 markChanged(state)208 return state.copy.clear()209 },210 has: state => key => {211 return latest(state).has(key)212 },213 forEach: (state, key, reciever) => cb => {214 latest(state).forEach((value, key, map) => {215 cb(reciever.get(key), key, map)216 })217 },218 keys: state => () => {219 return latest(state).keys()220 },221 values: iterateMapValues,222 entries: iterateMapValues223})224225function proxySet(target) {226 Object.defineProperties(target, setTraps)227228 if (typeof Symbol !== undefined) {229 Object.defineProperty(230 target,231 Symbol.iterator,232 proxyMethod(iterateSetValues)233 )234 }235}236237const iterateSetValues = makeIterateSetValues(createProxy)238239const setTraps = finalizeTraps({240 size: state => {241 return latest(state).size242 },243 add: state => value => {244 if (!latest(state).has(value)) {245 markChanged(state)246 if (!state.copy) {247 prepareCopy(state)248 }249 state.copy.add(value)250 }251 return state.draft252 },253 delete: state => value => {254 markChanged(state)255 if (!state.copy) {256 prepareCopy(state)257 }258 return state.copy.delete(value)259 },260 has: state => key => {261 return latest(state).has(key)262 },263 clear: state => () => {264 markChanged(state)265 if (!state.copy) {266 prepareCopy(state)267 }268 return state.copy.clear()269 },270 keys: iterateSetValues,271 entries: iterateSetValues,272 values: iterateSetValues,273 forEach: state => (cb, thisArg) => {274 const iterator = iterateSetValues(state)()275 let result = iterator.next()276 while (!result.done) {277 cb.call(thisArg, result.value, result.value, state.draft)278 result = iterator.next()279 }280 }281})282283function finalizeTraps(traps) {284 return Object.keys(traps).reduce(function(acc, key) {285 const builder = key === "size" ? proxyAttr : proxyMethod286 acc[key] = builder(traps[key], key)287 return acc288 }, {})289}290291function proxyAttr(fn) {292 return {293 get() {294 const state = this[DRAFT_STATE]295 assertUnrevoked(state)296 return fn(state)297 }298 }299}300301function proxyMethod(trap, key) {302 return {303 get() {304 return (...args) => {305 const state = this[DRAFT_STATE]306 assertUnrevoked(state)307 return trap(state, key, state.draft)(...args)308 }309 }310 }311}312313function assertUnrevoked(state) {314 if (state.revoked === true)315 throw new Error(316 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
11 unmodified lines
328 const state = drafts[i][DRAFT_STATE]329 if (!state.modified) {330 if (Array.isArray(state.base)) {331 if (hasArrayChanges(state)) markChanged(state)332 } else if (isMap(state.base)) {333 if (hasMapChanges(state)) markChanged(state)334 } else if (isSet(state.base)) {166 } else if (hasObjectChanges(state)) markChanged(state)335 if (hasSetChanges(state)) markChanged(state)336 } else if (hasObjectChanges(state)) {337 markChanged(state)338 }339 }340 }341}342
82 unmodified lines
425 // For all other cases, we don't have to compare, as they would have been picked up by the index setters426 return false427}428429function hasMapChanges(state) {430 const {base, draft} = state431432 if (base.size !== draft.size) return true433434 // IE11 supports only forEach iteration435 let hasChanges = false436 draft.forEach(function(value, key) {437 if (!hasChanges) {438 hasChanges = isDraftable(value)439 ? value.modified440 : value !== base.get(key)441 }442 })443 return hasChanges444}445446function hasSetChanges(state) {447 const {base, draft} = state448449 if (base.size !== draft.size) return true450451 // IE11 supports only forEach iteration452 let hasChanges = false453 draft.forEach(function(value, key) {454 if (!hasChanges) {455 hasChanges = isDraftable(value) ? value.modified : !base.has(key)456 }457 })458 return hasChanges459}460461function createHiddenProperty(target, prop, value) {462 Object.defineProperty(target, prop, {463 value: value,464 enumerable: false,
3 unmodified lines
modified:src/proxy.js+5 added-57 removed
7 unmodified lines
8 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"20import {ImmerScope} from "./scope"2122// Do nothing before being finalized.
234 unmodified lines
257 entries: iterateMapValues,258 [Symbol.iterator]: iterateMapValues259})260258/** Map.prototype.values _-or-_ Map.prototype.entries */259function 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}274261const iterateSetValues = makeIterateSetValues(createProxy)262/**263 * Set drafts264 */265
29 unmodified lines
295 entries: iterateSetValues,296 [Symbol.iterator]: iterateSetValues297})298312function 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}326327function 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}342299/**300 * Helpers301 */302
36 unmodified lines
339 }340 }341}342387function makeIterable(next) {388 let self389 return (self = {390 [Symbol.iterator]: () => self,391 next392 })393}394343/** Create traps that all use the `Reflect` API on the `latest(state)` */344function makeReflectTraps(names) {345 return names.reduce((traps, name) => {346 traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)
17 unmodified lines
modified:.travis.yml+1 added-1 removed
1language: node_js2node_js:3 - "8"4 - "10"5 - "node"5 # - "node"6env:7 - NODE_ENV=TEST8cache:9 yarn: true
19 unmodified lines