Skip to content
Myr

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

7 files+50 added−18 removed

modified:__tests__/base.js+42 added
1838 unmodified lines
1839 // so we don't atm1840 expect(nextState).not.toBe(baseState)1841 })18421843 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 })18551856 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 lines
2185 expect(state.bar).toEqual({baz: 1})2186 })2187 })21882189 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 }22032204 class State extends BaseState {}22052206 const state = new State()22072208 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 })22172218 describe("class with setters", () => {2219 class State {2220 [immerable] = true2221 _bar = 0
92 unmodified lines
modified:src/core/finalize.ts+1 added-3 removed
modified:src/core/immerClass.ts+1 added-1 removed
174 unmodified lines
175 *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 lines
modified:src/core/proxy.ts+2 added-3 removed
58 unmodified lines
59 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 lines
148 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-ignore
76 unmodified lines
233}234235export 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 lines
26 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 lines
76 // 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 drafting
72 unmodified lines
156 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 lines
modified:src/utils/common.ts+4 added-4 removed
49 unmodified lines
50 if (value && value[DRAFT_STATE]) {51 return value[DRAFT_STATE].base_ as any52 }53 // otherwise return undefined54 // TODO: or die55}5657/*#__PURE__*/58export const ownKeys: (target: AnyObject) => PropertyKey[] =
85 unmodified lines
144 return state.copy_ || state.base_145}146147/*#__PURE__*/147export function shallowCopy(base: any, makeWritable = false) {148 // TODO: makeWritable is always true?148export 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 dealing
2 unmodified lines
161 // 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 lines
modified:src/utils/plugins.ts-1 removed
65 unmodified lines
6667/** ES5 Plugin */6869interface ES5BaseState extends ImmerBaseState {70 finalizing_: boolean70 assigned_: {[key: string]: any}71 parent_?: ImmerState72 revoked_: boolean73}
38 unmodified lines