Skip to content
Myr

finished accessor props for ES5

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 d0f3a71 · Browse files · 3 files+178 added−37 removed
This commit

State

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

Diff stats

3 files+178 added−37 removed

  • tests:+115 added−0 removed
  • other:+63 added−37 removed

3 files+178 added−37 removed

modified:__tests__/base.js+115 added
948 unmodified lines
949 })950 }).toThrowErrorMatchingSnapshot()951 })952953 it("can work with own computed props", () => {954 const baseState = {955 x: 1,956 get y() {957 return this.x958 },959 set y(v) {960 this.x = v961 }962 }963964 const nextState = produce(baseState, d => {965 expect(d.y).toBe(1)966 d.x = 2967 expect(d.x).toBe(2)968 expect(d.y).toBe(1) // this has been copied!969 d.y = 3970 expect(d.x).toBe(2)971 })972 expect(baseState.x).toBe(1)973 expect(baseState.y).toBe(1)974975 expect(nextState.x).toBe(2)976 expect(nextState.y).toBe(3)977 if (!autoFreeze) {978 nextState.y = 4 // decoupled now!979 expect(nextState.y).toBe(4)980 expect(nextState.x).toBe(2)981 expect(Object.getOwnPropertyDescriptor(nextState, "y").value).toBe(4)982 }983 })984985 it("can work with class with computed props", () => {986 class State {987 [immerable] = true988989 x = 1990991 set y(v) {992 this.x = v993 }994995 get y() {996 return this.x997 }998 }9991000 const baseState = new State()10011002 const nextState = produce(baseState, d => {1003 expect(d.y).toBe(1)1004 d.y = 21005 expect(d.x).toBe(2)1006 expect(d.y).toBe(2)1007 expect(Object.getOwnPropertyDescriptor(d, "y")).toBeUndefined()1008 })1009 expect(baseState.x).toBe(1)1010 expect(baseState.y).toBe(1)10111012 expect(nextState.x).toBe(2)1013 expect(nextState.y).toBe(2)1014 expect(Object.getOwnPropertyDescriptor(nextState, "y")).toBeUndefined()1015 })10161017 it("allows inherited computed properties", () => {1018 const proto = {}1019 Object.defineProperty(proto, "foo", {1020 get() {
1126 unmodified lines
2147 expect(result[0]).toBe(random)2148 })2149 })2150 }21512152 describe("class with getters", () => {2153 class State {2154 [immerable] = true2155 _bar = {baz: 1}2156 foo2157 get bar() {2158 return this._bar2159 }2160 syncFoo() {2161 const value = this.bar.baz2162 this.foo = value2163 this.bar.baz++2164 }2165 }2166 const state = new State()21672168 it("should use a method to assing a field using a getter that return a non primitive object", () => {2169 const newState = produce(state, draft => {2170 draft.syncFoo()2171 })2172 expect(newState.foo).toEqual(1)2173 expect(newState.bar).toEqual({baz: 2})2174 expect(state.bar).toEqual({baz: 1})2175 })2176 })21772178 describe("class with setters", () => {2179 class State {2180 [immerable] = true2181 _bar = 02182 get bar() {2183 return this._bar2184 }2185 set bar(x) {2186 this._bar = x2187 }2188 }2189 const state = new State()21902191 it("should define a field with a setter", () => {2192 const newState3 = produce(state, d => {2193 d.bar = 12194 expect(d._bar).toEqual(1)2195 })2196 expect(newState3._bar).toEqual(1)2197 expect(newState3.bar).toEqual(1)2198 expect(state._bar).toEqual(0)2199 expect(state.bar).toEqual(0)2200 })2201 })2202}22032204function testLiteralTypes(produce) {2205 class Foo {}
52 unmodified lines
modified:src/plugins/es5.ts+44 added-33 removed
46 unmodified lines
47 markChangesSweep(scope.drafts_)48 }49 }5051 function createES5Proxy_<T>(52 base: T,53 parent?: ImmerState54 ): Drafted<T, ES5ObjectState | ES5ArrayState> {55 const isArray = Array.isArray(base)56 const baseDescriptors = Object.getOwnPropertyDescriptors(base)51 function createES5Draft(isArray: boolean, base: any) {52 // Create a new object / array, where each own property is trapped with an accessor53 const descriptors = Object.getOwnPropertyDescriptors(base)54 // Descriptors we want to skip:58 if (isArray) delete baseDescriptors.length59 delete baseDescriptors[DRAFT_STATE as any]60 let draft: any61 const newDescriptors = {}6263 for (let key in baseDescriptors) {64 // @ts-ignore65 newDescriptors[key] = proxyProperty(55 if (isArray) delete descriptors.length56 delete descriptors[DRAFT_STATE as any]57 for (let key in descriptors) {58 descriptors[key] = proxyProperty(59 key,67 // @ts-ignore68 isArray || baseDescriptors[key].enumerable60 isArray || !!descriptors[key].enumerable61 )62 }63 if (isArray) {72 // @ts-ignore73 draft = new Array(base.length)74 // @ts-ignore75 Object.defineProperties(draft, newDescriptors)64 const draft = new Array(base.length)65 Object.defineProperties(draft, descriptors)66 return draft67 } else {77 draft = Object.create(Object.getPrototypeOf(base), newDescriptors)68 return Object.create(Object.getPrototypeOf(base), descriptors)69 }70 }7172 function createES5Proxy_<T>(73 base: T,74 parent?: ImmerState75 ): Drafted<T, ES5ObjectState | ES5ArrayState> {76 const isArray = Array.isArray(base)77 const draft = createES5Draft(isArray, base)7879 const state: ES5ObjectState | ES5ArrayState = {80 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),81 scope_: parent ? parent.scope_ : getCurrentScope(),82 modified_: false,83 finalizing_: false,84 finalized_: false,85 assigned_: {},86 parent_: parent,87 // base is the object we are drafting88 base_: base,89 // draft is the draft object itself, that traps all reads and reads from either the base (if unmodified) or copy (if modified)90 draft_: draft,91 copy_: null,92 revoked_: false,93 isManual_: false
10 unmodified lines
104 // Access a property without creating an Immer draft.105 function peek(draft: Drafted, prop: PropertyKey) {106 const state: ES5State = draft[DRAFT_STATE]107 if (state && !state.finalizing_) {107 state.finalizing_ = true108 // state.finalizing_ = true // TODO: still needed?109 // If not, delete this block!110 const value = draft[prop]109 state.finalizing_ = false111 // state.finalizing_ = false112 return value113 }114 return draft[prop]115 }
36 unmodified lines
152153 function prepareCopy(state: ES5State) {154 if (state.copy_) return155 const stateofBase: ES5State | undefined = (state.base_ as any)[DRAFT_STATE]156157 // make a copy, if the base is a draft itself, we take a copy of it's current state155 state.copy_ = shallowCopy(156 stateofBase ? latest(stateofBase) : state.base_,157 true158 )158 // stateofBase?.finalizing_ = true; // TODO: needed?159160 // TODO: some code reuse?161 // Object.getOwnPropertyDescriptors162 state.copy_ = shallowCopy(state.base_, true)163 // stateofBase?.finalizing_ = false; // TODO: needed?164 }165166 function createFinalCopy_(state: ES5ArrayState | ES5ObjectState): any {167 state.finalizing_ = true168 const res = shallowCopy(state.draft_, true)169 state.finalizing_ = false170 return res171172 if (state.type_ === ProxyTypeES5Array) return state.draft_?.slice()173 // We create a final copy, based on the keys present in the draft.174 // However, since most draft keys are wrapped getters, so lets take the descriptor of the copy175 // so that any getters can be preserved (if present)176 const final = Object.create(Object.getPrototypeOf(state.draft_))177 // TODO: extract utils for getPrototypeOf / getOwnPropertyDescriptors178 const draftDescriptors = Object.getOwnPropertyDescriptors(state.draft_)169 const copyDescriptors = Object.getOwnPropertyDescriptors(latest(state))179 // const copyDescriptors = Object.getOwnPropertyDescriptors(latest(state))180 for (let key in draftDescriptors) {181 // @ts-ignore182 if (key === DRAFT_STATE) return183 const desc = draftDescriptors[key]184 // if there is a value in the descriptor, it was deleted and readded, so respect the draft one175 Object.defineProperty(176 final,177 key,178 "value" in desc ? desc : copyDescriptors[key] || desc179 )185 Object.defineProperty(final, key, {186 writable: true,187 enumerable: desc.enumerable,188 configurable: true,189 value: state.draft_[key]190 })191 }192 return final193 }194
165 unmodified lines
modified:src/utils/common.ts+19 added-4 removed