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
Provenance
- Commit
12c38c6- Tree
2bc3b96- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
28ce953
5 files changed+148 added−39 removed
modified:__tests__/base.js+118 added-18 removed
186 unmodified lines187 expect(nextState).toBe(baseState)188 })189 190 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 lines388 expect(result.get("second").a).toEqual(2)389 })390 391 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 lines548 expect(d.delete("y")).toBe(false)549 })550 expect(next).toBe(base)551 })552+553+ 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 })566 567 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 lines582 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 })599 600 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 item16 unmodified lines615 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 })632 633 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 lines647 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 })664 665 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 lines679 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 })696 697 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 += a6 unmodified lines712 })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 })729 730 it("state stays the same if the same item is added", () => {731 const nextState = produce(baseState, s => {83 unmodified lines815 expect(d.has(obj)).toBe(false)816 })817 expect(next).toEqual(new Set([3]))818 })819+820+ 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 })833 834 it("supports `immerable` symbol on constructor", () => {835 class One {}880 unmodified lines1716 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}1725 1726 function first(set) {1727 return Array.from(set.values())[0]166 unmodified linesmodified:src/es5.ts+3 added-7 removed
20 unmodified lines21 ImmerBaseState,22 AnyArray,23 ProxyType24 } from "./types"25+import {MapState} from "./map"26+import {SetState} from "./set"27 28 interface ES5BaseState extends ImmerBaseState {29 finalizing: boolean30 assigned: {[key: string]: any}31 parent?: ImmerState30- revoke(): void32 revoked: boolean33 }34 35 export interface ES5ObjectState extends ES5BaseState {54 unmodified lines90 parent,91 base,92 draft,93 copy: null,93- revoke,94 revoked: false,95 isManual: false96 }97 98 createHiddenProperty(draft, DRAFT_STATE, state)99 return draft100 }101 102-function revoke(this: ES5BaseState) {103- this.revoked = true104-}105-102 // Access a property without creating an Immer draft.103 function peek(draft: Drafted, prop: PropertyKey) {104 const state = draft[DRAFT_STATE]105 if (state && !state.finalizing) {78 unmodified lines184 }185 Object.defineProperty(draft, prop, desc)186 }187 192-export function assertUnrevoked(state: ES5State) {188+export 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 linesmodified:src/map.ts+8 added-5 removed
12345678910111212131415161714 unmodified lines32333435353637383637383940415 unmodified lines4748495051525354558 unmodified lines6465666768697071727374757677787980818210 unmodified lines9394959697989910010161 unmodified linesimport {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"
import {ImmerScope} from "./scope"import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"import {assertUnrevoked} from "./es5"
export interface MapState extends ImmerBaseState { type: ProxyType.Map finalizing: boolean copy: AnyMap | undefined assigned: Map<any, boolean> | undefined base: AnyMap revoke(): void revoked: boolean draft: Drafted<AnyMap, MapState>}
// Make sure DraftMap declarion doesn't die if Map is not avialable...14 unmodified lines copy: undefined, assigned: undefined, base: target, draft: this, revoke() { // TODO: make sure this marks the Map as revoked, and assert everywhere }, isManual: false isManual: false, revoked: false } }
get size(): number {5 unmodified lines }
set(key: K, value: V): this { const state = this[DRAFT_STATE] assertUnrevoked(state) if (latest(state).get(key) !== value) { prepareCopy(state) state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly state.assigned!.set(key, true)8 unmodified lines return false }
const state = this[DRAFT_STATE] assertUnrevoked(state) prepareCopy(state) state.scope.immer.markChanged(state) state.assigned!.set(key, false) state.copy!.delete(key) return true }
clear() { const state = this[DRAFT_STATE] assertUnrevoked(state) prepareCopy(state) state.scope.immer.markChanged(state) state.assigned = new Map() for (const key of latest(state).keys()) {10 unmodified lines }
get(key: K): V { const state = this[DRAFT_STATE] assertUnrevoked(state) const value = latest(state).get(key) if (state.finalizing || state.finalized || !isDraftable(value)) { return value }61 unmodified linesmodified:src/scope.ts+8 added-3 removed
1 import {DRAFT_STATE} from "./common"2-import {Patch, PatchListener, Drafted} from "./types"2+import {Patch, PatchListener, Drafted, ProxyType} from "./types"3 import {Immer} from "./immer"4 5 /** Each scope represents a `produce` call. */6 export class ImmerScope {45 unmodified lines52 }53 }54 55 function 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
1 import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"2 3 import {ImmerScope} from "./scope"4 import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5+import {assertUnrevoked} from "./es5"6 7 export 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 }16 17 // Make sure DraftSet declarion doesn't die if Map is not avialable...14 unmodified lines32 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 lines43 }44 45 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 lines56 }57 58 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 lines68 return this69 }70 71 delete(value: V): boolean {71- const state = this[DRAFT_STATE]72 if (!this.has(value)) {73 return false74 }75 76+ 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 lines86 }87 88 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 }95 96 values(): IterableIterator<V> {97 const state = this[DRAFT_STATE]98+ assertUnrevoked(state)99 prepareCopy(state)100 return state.copy!.values()101 }102 103 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