Some cleanup
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 c17b6e5 · Browse files · 7 files·+50 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+50 added−18 removed
- tests:+42 added−0 removed
- other:+8 added−18 removed
Provenance
- Commit
b3b43b9- Tree
c73f62f- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
c17b6e5
7 files changed+50 added−18 removed
modified:__tests__/base.js+42 added
1838 unmodified lines1839 // so we don't atm1840 expect(nextState).not.toBe(baseState)1841 })1842 1843+ it("cannot always detect noop assignments - 4", () => {1844+ const baseState = {}1845+ const [nextState, patches] = produceWithPatches(baseState, d => {1846+ d.x = 41847+ delete d.x1848+ })1849+ expect(nextState).toEqual({})1850+ expect(patches).toEqual([])1851+ // This differs between ES5 and proxy, and ES5 does it better :(1852+ if (useProxies) expect(nextState).not.toBe(baseState)1853+ else expect(nextState).toBe(baseState)1854+ })1855+1856 it("cannot produce undefined by returning undefined", () => {1857 const base = 31858 expect(produce(base, () => 4)).toBe(4)1859 expect(produce(base, () => null)).toBe(null)325 unmodified lines2185 expect(state.bar).toEqual({baz: 1})2186 })2187 })2188 2189+ describe("super class with getters", () => {2190+ class BaseState {2191+ [immerable] = true2192+ _bar = {baz: 1}2193+ foo2194+ get bar() {2195+ return this._bar2196+ }2197+ syncFoo() {2198+ const value = this.bar.baz2199+ this.foo = value2200+ this.bar.baz++2201+ }2202+ }2203+2204+ class State extends BaseState {}2205+2206+ const state = new State()2207+2208+ it("should use a method to assing a field using a getter that return a non primitive object", () => {2209+ const newState = produce(state, draft => {2210+ draft.syncFoo()2211+ })2212+ expect(newState.foo).toEqual(1)2213+ expect(newState.bar).toEqual({baz: 2})2214+ expect(state.bar).toEqual({baz: 1})2215+ })2216+ })2217+2218 describe("class with setters", () => {2219 class State {2220 [immerable] = true2221 _bar = 092 unmodified linesmodified:src/core/finalize.ts+1 added-3 removed
9 unmodified lines1011121314151415161766 unmodified lines8485868790888990919264 unmodified lines9 unmodified lines ImmerState, isDraft, SetState, set, is, get, ProxyTypeES5Object, ProxyTypeES5Array, ProxyTypeSet, getPlugin,66 unmodified lines state.scope_.unfinalizedDrafts_-- const result = // For ES5, create a good copy from the draft first, with added keys and without deleted keys. state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array ? (state.copy_ = shallowCopy(state.draft_, true)) ? (state.copy_ = shallowCopy(state.draft_)) : state.copy_ // finalize all children of the copy each(result as any, (key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path)64 unmodified linesmodified:src/core/immerClass.ts+1 added-1 removed
174 unmodified lines175 *176 * By default, feature detection is used, so calling this is rarely necessary.177 */178 setUseProxies(value: boolean) {179- if (!hasProxies) {179+ if (value && !hasProxies) {180 die(20)181 }182 this.useProxies_ = value183 }43 unmodified linesmodified:src/core/proxy.ts+2 added-3 removed
58 unmodified lines59 scope_: parent ? parent.scope_ : getCurrentScope()!,60 // True for both shallow and deep changes.61 modified_: false,62 // Used during finalization.63- // TODO: still needed?63 finalized_: false,64 // Track which properties have been assigned (true) or deleted (false).65 assigned_: {},66 // The parent draft state.81 unmodified lines148 if (peek(state.base_, prop) !== undefined || prop in state.base_) {149 state.assigned_[prop] = false150 prepareCopy(state)151 markChanged(state)153- } else if (state.assigned_[prop]) {152+ } else {153 // if an originally not assigned property was deleted154 delete state.assigned_[prop]155 }156 // @ts-ignore76 unmodified lines233 }234 235 export function prepareCopy(state: {base_: any; copy_: any}) {236 if (!state.copy_) {238- state.copy_ = shallowCopy(state.base_, true)237+ state.copy_ = shallowCopy(state.base_)238 }239 }modified:src/plugins/es5.ts-6 removed
25 unmodified lines26 scope: ImmerScope,27 result: any,28 isReplaced: boolean29 ) {30- scope.drafts_!.forEach((draft: any) => {31- ;(draft[DRAFT_STATE] as ES5State).finalizing_ = true32- })30 if (!isReplaced) {31 if (scope.patches_) {32 markChangesRecursively(scope.drafts_![0])33 }42 unmodified lines76 // TODO: reuse this object construction from proxy?77 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),78 scope_: parent ? parent.scope_ : getCurrentScope(),79 modified_: false,83- // TODO: still needed?84- finalizing_: false,80 finalized_: false,81 assigned_: {},82 parent_: parent,83 // base is the object we are drafting72 unmodified lines156 if (!state) return157 const {base_, draft_, assigned_, type_} = state158 if (type_ === ProxyTypeES5Object) {159 // Look for added keys.165- // TODO: looks quite duplicate to hasObjectChanges,160 // probably there is a faster way to detect changes, as sweep + recurse seems to do some161 // unnecessary work.162 // also: probably we can store the information we detect here, to speed up tree finalization!163 each(draft_, key => {98 unmodified linesmodified:src/utils/common.ts+4 added-4 removed
49 unmodified lines50 if (value && value[DRAFT_STATE]) {51 return value[DRAFT_STATE].base_ as any52 }53 // otherwise return undefined54+ // TODO: or die55 }56 57 /*#__PURE__*/58 export const ownKeys: (target: AnyObject) => PropertyKey[] =85 unmodified lines144 return state.copy_ || state.base_145 }146 147 /*#__PURE__*/147-export function shallowCopy(base: any, makeWritable = false) {148- // TODO: makeWritable is always true?148+export function shallowCopy(base: any) {149 if (Array.isArray(base)) return base.slice()150 const descriptors = Object.getOwnPropertyDescriptors(base)151 delete descriptors[DRAFT_STATE as any]152 for (let key in descriptors) {153 const desc = descriptors[key]154- if (makeWritable && desc.writable === false) {154+ if (desc.writable === false) {155 desc.writable = true156 desc.configurable = true157 }158 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing2 unmodified lines161 // TODO: document!162 if (desc.get)163 descriptors[key] = {164 configurable: true,165- writable: true, // TODO: or !!desc.set ?165+ writable: !!desc.set, // could live with 'true' as well here...166 enumerable: desc.enumerable,167 value: base[key]168 }169 }21 unmodified linesmodified:src/utils/plugins.ts-1 removed
65 unmodified lines66 67 /** ES5 Plugin */68 69 interface ES5BaseState extends ImmerBaseState {70- finalizing_: boolean70 assigned_: {[key: string]: any}71 parent_?: ImmerState72 revoked_: boolean73 }38 unmodified lines