Skip to content
Myr

Made sure Map and Set proxies are revoked, and less closures are created

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

authored by Michel Weststrate · committed by Michel Weststrate · parent 28ce953 · Browse files · 5 files+148 added−39 removed
This commit

State

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

Diff stats

5 files+148 added−39 removed

  • tests:+118 added−18 removed
  • other:+30 added−21 removed

5 files+148 added−39 removed

modified:__tests__/base.js+118 added-18 removed
186 unmodified lines
187 expect(nextState).toBe(baseState)188 })189190 it("supports iteration", () => {191 const base = [{id: 1, a: 1}, {id: 2, a: 1}]191 const base = [192 {id: 1, a: 1},193 {id: 2, a: 1}194 ]195 const findById = (collection, id) => {196 for (const item of collection) {197 if (item.id === id) return item198 }
189 unmodified lines
388 expect(result.get("second").a).toEqual(2)389 })390391 it("supports 'keys", () => {389 const base = new Map([["first", Symbol()], ["second", Symbol()]])392 const base = new Map([393 ["first", Symbol()],394 ["second", Symbol()]395 ])396 const result = produce(base, draft => {397 expect([...draft.keys()]).toEqual(["first", "second"])398 draft.set("third", Symbol())399 expect([...draft.keys()]).toEqual(["first", "second", "third"])
148 unmodified lines
548 expect(d.delete("y")).toBe(false)549 })550 expect(next).toBe(base)551 })552553 it("revokes map proxies", () => {554 let m555 produce(baseState, s => {556 m = s.aMap557 })558 expect(() => m.get("x")).toThrow(559 "Cannot use a proxy that has been revoked"560 )561 expect(() => m.set("x", 3)).toThrow(562 "Cannot use a proxy that has been revoked"563 )564 })565 })566567 describe("set drafts", () => {568 it("supports iteration", () => {550 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])569 const base = new Set([570 {id: 1, a: 1},571 {id: 2, a: 1}572 ])573 const findById = (set, id) => {574 for (const item of set) {575 if (item.id === id) return item576 }
5 unmodified lines
582 obj1.a = 2583 obj2.a = 2584 })585 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}]))586 expect(base).toEqual(587 new Set([588 {id: 1, a: 1},589 {id: 2, a: 1}590 ])591 )592 expect(result).toEqual(593 new Set([594 {id: 1, a: 2},595 {id: 2, a: 2}596 ])597 )598 })599600 it("supports 'entries'", () => {569 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])601 const base = new Set([602 {id: 1, a: 1},603 {id: 2, a: 1}604 ])605 const findById = (set, id) => {606 for (const [item1, item2] of set.entries()) {607 expect(item1).toBe(item2)608 if (item1.id === id) return item1
6 unmodified lines
615 obj1.a = 2616 obj2.a = 2617 })618 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}]))619 expect(base).toEqual(620 new Set([621 {id: 1, a: 1},622 {id: 2, a: 1}623 ])624 )625 expect(result).toEqual(626 new Set([627 {id: 1, a: 2},628 {id: 2, a: 2}629 ])630 )631 })632633 it("supports 'values'", () => {589 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])634 const base = new Set([635 {id: 1, a: 1},636 {id: 2, a: 1}637 ])638 const findById = (set, id) => {639 for (const item of set.values()) {640 if (item.id === id) return item641 }
5 unmodified lines
647 obj1.a = 2648 obj2.a = 2649 })650 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}]))651 expect(base).toEqual(652 new Set([653 {id: 1, a: 1},654 {id: 2, a: 1}655 ])656 )657 expect(result).toEqual(658 new Set([659 {id: 1, a: 2},660 {id: 2, a: 2}661 ])662 )663 })664665 it("supports 'keys'", () => {608 const base = new Set([{id: 1, a: 1}, {id: 2, a: 1}])666 const base = new Set([667 {id: 1, a: 1},668 {id: 2, a: 1}669 ])670 const findById = (set, id) => {671 for (const item of set.keys()) {672 if (item.id === id) return item673 }
5 unmodified lines
679 obj1.a = 2680 obj2.a = 2681 })682 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}]))683 expect(base).toEqual(684 new Set([685 {id: 1, a: 1},686 {id: 2, a: 1}687 ])688 )689 expect(result).toEqual(690 new Set([691 {id: 1, a: 2},692 {id: 2, a: 2}693 ])694 )695 })696697 it("supports forEach with mutation after reads", () => {627 const base = new Set([{id: 1, a: 1}, {id: 2, a: 2}])698 const base = new Set([699 {id: 1, a: 1},700 {id: 2, a: 2}701 ])702 const result = produce(base, draft => {703 let sum1 = 0704 draft.forEach(({a}) => {705 sum1 += a
6 unmodified lines
712 })713 expect(sum2).toBe(23)714 })715 expect(result).not.toBe(base)642 expect(base).toEqual(new Set([{id: 1, a: 1}, {id: 2, a: 2}]))643 expect(result).toEqual(new Set([{id: 1, a: 11}, {id: 2, a: 12}]))716 expect(base).toEqual(717 new Set([718 {id: 1, a: 1},719 {id: 2, a: 2}720 ])721 )722 expect(result).toEqual(723 new Set([724 {id: 1, a: 11},725 {id: 2, a: 12}726 ])727 )728 })729730 it("state stays the same if the same item is added", () => {731 const nextState = produce(baseState, s => {
83 unmodified lines
815 expect(d.has(obj)).toBe(false)816 })817 expect(next).toEqual(new Set([3]))818 })819820 it("revokes sets", () => {821 let m822 produce(baseState, s => {823 m = s.aSet824 })825 expect(() => m.has("x")).toThrow(826 "Cannot use a proxy that has been revoked"827 )828 expect(() => m.add("x")).toThrow(829 "Cannot use a proxy that has been revoked"830 )831 })832 })833834 it("supports `immerable` symbol on constructor", () => {835 class One {}
880 unmodified lines
1716 const b = {b: 2}1717 const c = {c: 3}1718 const set1 = new Set([a, b])1719 const set2 = new Set([c])1623 const map = new Map([["set1", set1], ["set2", set2]])1720 const map = new Map([1721 ["set1", set1],1722 ["set2", set2]1723 ])1724 const base = {map}17251726 function first(set) {1727 return Array.from(set.values())[0]
166 unmodified lines
modified:src/es5.ts+3 added-7 removed
20 unmodified lines
21 ImmerBaseState,22 AnyArray,23 ProxyType24} from "./types"25import {MapState} from "./map"26import {SetState} from "./set"2728interface ES5BaseState extends ImmerBaseState {29 finalizing: boolean30 assigned: {[key: string]: any}31 parent?: ImmerState30 revoke(): void32 revoked: boolean33}3435export interface ES5ObjectState extends ES5BaseState {
54 unmodified lines
90 parent,91 base,92 draft,93 copy: null,93 revoke,94 revoked: false,95 isManual: false96 }9798 createHiddenProperty(draft, DRAFT_STATE, state)99 return draft100}101102function revoke(this: ES5BaseState) {103 this.revoked = true104}105102// Access a property without creating an Immer draft.103function peek(draft: Drafted, prop: PropertyKey) {104 const state = draft[DRAFT_STATE]105 if (state && !state.finalizing) {
78 unmodified lines
184 }185 Object.defineProperty(draft, prop, desc)186}187192export function assertUnrevoked(state: ES5State) {188export function assertUnrevoked(state: ES5State | MapState | SetState) {189 if (state.revoked === true)190 throw new Error(191 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +192 JSON.stringify(latest(state))
114 unmodified lines
modified:src/map.ts+8 added-5 removed
modified:src/scope.ts+8 added-3 removed
1import {DRAFT_STATE} from "./common"2import {Patch, PatchListener, Drafted} from "./types"2import {Patch, PatchListener, Drafted, ProxyType} from "./types"3import {Immer} from "./immer"45/** Each scope represents a `produce` call. */6export class ImmerScope {
45 unmodified lines
52 }53}5455function revoke(draft: Drafted) {56 // TODO: switch per type and remove from data structures57 draft[DRAFT_STATE].revoke()56 const state = draft[DRAFT_STATE]57 if (58 state.type === ProxyType.ProxyObject ||59 state.type === ProxyType.ProxyArray60 )61 state.revoke()62 else state.revoked = true63}
modified:src/set.ts+11 added-6 removed
1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"23import {ImmerScope} from "./scope"4import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5import {assertUnrevoked} from "./es5"67export interface SetState extends ImmerBaseState {8 type: ProxyType.Set8 finalizing: boolean9 finalizing: boolean // TODO: kill?10 copy: AnySet | undefined11 base: AnySet12 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set12 revoke(): void13 revoked: boolean14 draft: Drafted<AnySet, SetState>15}1617// Make sure DraftSet declarion doesn't die if Map is not avialable...
14 unmodified lines
32 copy: undefined,33 base: target,34 draft: this,35 drafts: new Map(),35 revoke() {36 // TODO: make sure this marks the Map as revoked, and assert everywhere37 },36 revoked: false,37 isManual: false38 }39 }40
2 unmodified lines
43 }4445 has(value: V): boolean {46 const state = this[DRAFT_STATE]47 assertUnrevoked(state)48 // bit of trickery here, to be able to recognize both the value, and the draft of its value49 if (!state.copy) {50 return state.base.has(value)51 }
4 unmodified lines
56 }5758 add(value: V): this {59 const state = this[DRAFT_STATE]60 assertUnrevoked(state)61 if (state.copy) {62 state.copy.add(value)63 } else if (!state.base.has(value)) {64 prepareCopy(state)
3 unmodified lines
68 return this69 }7071 delete(value: V): boolean {71 const state = this[DRAFT_STATE]72 if (!this.has(value)) {73 return false74 }7576 const state = this[DRAFT_STATE]77 assertUnrevoked(state)78 prepareCopy(state)79 state.scope.immer.markChanged(state)80 return (81 state.copy!.delete(value) ||
4 unmodified lines
86 }8788 clear() {89 const state = this[DRAFT_STATE]90 assertUnrevoked(state)91 prepareCopy(state)92 state.scope.immer.markChanged(state)93 return state.copy!.clear()94 }9596 values(): IterableIterator<V> {97 const state = this[DRAFT_STATE]98 assertUnrevoked(state)99 prepareCopy(state)100 return state.copy!.values()101 }102103 entries(): IterableIterator<[V, V]> {104 const state = this[DRAFT_STATE]105 assertUnrevoked(state)106 prepareCopy(state)107 return state.copy!.entries()108 }109
37 unmodified lines