Code cleanup, bug fixes
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 b1c47c2 · Browse files · 10 files·+92 added−230 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+92 added−230 removed
- tests:+25 added−50 removed
- other:+67 added−180 removed
Provenance
- Commit
0592d3c- Tree
6c03e80- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
b1c47c2
10 files changed+92 added−230 removed
modified:__tests__/__snapshots__/base.js.snap-16 removed
20 unmodified lines21 exports[`base functionality - es5 (autofreeze) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;22 23 exports[`base functionality - es5 (autofreeze) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;24 25-exports[`base functionality - es5 (autofreeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;26-25 exports[`base functionality - es5 (autofreeze) throws when the draft is modified and another object is returned 1`] = `"[Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."`;26 27 exports[`base functionality - es5 (autofreeze)(patch listener) async recipe function works with rejected promises 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {\\"a\\":0,\\"b\\":1}"`;28 16 unmodified lines45 exports[`base functionality - es5 (autofreeze)(patch listener) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;46 47 exports[`base functionality - es5 (autofreeze)(patch listener) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;48 51-exports[`base functionality - es5 (autofreeze)(patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;52-49 exports[`base functionality - es5 (autofreeze)(patch listener) throws when the draft is modified and another object is returned 1`] = `"[Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."`;50 51 exports[`base functionality - es5 (no freeze) async recipe function works with rejected promises 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {\\"a\\":0,\\"b\\":1}"`;52 16 unmodified lines69 exports[`base functionality - es5 (no freeze) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;70 71 exports[`base functionality - es5 (no freeze) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;72 77-exports[`base functionality - es5 (no freeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;78-73 exports[`base functionality - es5 (no freeze) throws when the draft is modified and another object is returned 1`] = `"[Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."`;74 75 exports[`base functionality - es5 (patch listener) async recipe function works with rejected promises 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {\\"a\\":0,\\"b\\":1}"`;76 16 unmodified lines93 exports[`base functionality - es5 (patch listener) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;94 95 exports[`base functionality - es5 (patch listener) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;96 103-exports[`base functionality - es5 (patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;104-97 exports[`base functionality - es5 (patch listener) throws when the draft is modified and another object is returned 1`] = `"[Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."`;98 99 exports[`base functionality - proxy (autofreeze) array drafts throws when a non-numeric property is added 1`] = `"[Immer] Immer only supports setting array indices and the 'length' property"`;100 28 unmodified lines129 exports[`base functionality - proxy (autofreeze) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;130 131 exports[`base functionality - proxy (autofreeze) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;132 141-exports[`base functionality - proxy (autofreeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;142-133 exports[`base functionality - proxy (autofreeze) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;134 135 exports[`base functionality - proxy (autofreeze) throws when Object.setPrototypeOf() is used on a draft 1`] = `"[Immer] Object.setPrototypeOf() cannot be used on an Immer draft"`;136 32 unmodified lines169 exports[`base functionality - proxy (autofreeze)(patch listener) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;170 171 exports[`base functionality - proxy (autofreeze)(patch listener) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;172 183-exports[`base functionality - proxy (autofreeze)(patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;184-173 exports[`base functionality - proxy (autofreeze)(patch listener) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;174 175 exports[`base functionality - proxy (autofreeze)(patch listener) throws when Object.setPrototypeOf() is used on a draft 1`] = `"[Immer] Object.setPrototypeOf() cannot be used on an Immer draft"`;176 32 unmodified lines209 exports[`base functionality - proxy (no freeze) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;210 211 exports[`base functionality - proxy (no freeze) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;212 225-exports[`base functionality - proxy (no freeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;226-213 exports[`base functionality - proxy (no freeze) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;214 215 exports[`base functionality - proxy (no freeze) throws when Object.setPrototypeOf() is used on a draft 1`] = `"[Immer] Object.setPrototypeOf() cannot be used on an Immer draft"`;216 32 unmodified lines249 exports[`base functionality - proxy (patch listener) set drafts revokes sets 1`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;250 251 exports[`base functionality - proxy (patch listener) set drafts revokes sets 2`] = `"[Immer] Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {}"`;252 267-exports[`base functionality - proxy (patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;268-253 exports[`base functionality - proxy (patch listener) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;254 255 exports[`base functionality - proxy (patch listener) throws when Object.setPrototypeOf() is used on a draft 1`] = `"[Immer] Object.setPrototypeOf() cannot be used on an Immer draft"`;256 377 unmodified linesmodified:__tests__/base.js+23 added-32 removed
19 unmodified lines20 test("immer should have no dependencies", () => {21 expect(require("../package.json").dependencies).toBeUndefined()22 })23 24-// runBaseTest("proxy (no freeze)", true, false)25-// runBaseTest("proxy (autofreeze)", true, true)26-// runBaseTest("proxy (patch listener)", true, false, true)27-// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)24+runBaseTest("proxy (no freeze)", true, false)25+runBaseTest("proxy (autofreeze)", true, true)26+runBaseTest("proxy (patch listener)", true, false, true)27+runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)28 29 runBaseTest("es5 (no freeze)", false, false)30-// runBaseTest("es5 (autofreeze)", false, true)31-// runBaseTest("es5 (patch listener)", false, false, true)32-// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)30+runBaseTest("es5 (autofreeze)", false, true)31+runBaseTest("es5 (patch listener)", false, false, true)32+runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)33 34 function runBaseTest(name, useProxies, autoFreeze, useListener) {35 const listener = useListener ? function() {} : undefined36 const {produce, produceWithPatches} = createPatchedImmer({858 unmodified lines895 expect(nextState).not.toBe(baseState)896 expect(nextState.foo).toBeTruthy()897 })898 899- it.skip("preserves symbol properties", () => {899+ it("preserves symbol properties", () => {900 const test = Symbol("test")901 const baseState = {[test]: true}902 const nextState = produce(baseState, s => {903 expect(s[test]).toBeTruthy()29 unmodified lines933 expect(nextState.foo).toBeTruthy()934 expect(isEnumerable(nextState, "foo")).toBeFalsy()935 })936 937- it.skip("throws on computed properties", () => {938- const baseState = {}939- Object.defineProperty(baseState, "foo", {940- get: () => {},941- enumerable: true942- })943- expect(() => {944- produce(baseState, s => {945- // Proxies only throw once a change is made.946- if (useProxies) {947- s.modified = true948- }949- })950- }).toThrowErrorMatchingSnapshot()951- })952-937 it("can work with own computed props", () => {938 const baseState = {939 x: 1,940 get y() {455 unmodified lines1396 parent.b.x // Ensure proxy not revoked.1397 })1398 })1399 1416- it.skip("works with interweaved Immer instances", () => {1400+ it("works with interweaved Immer instances", () => {1401 const options = {useProxies, autoFreeze}1402 const one = createPatchedImmer(options)1403 const two = createPatchedImmer(options)1404 152 unmodified lines1557 expect(next2).toBe(a)1558 })1559 1560 autoFreeze &&1577- test.skip("issue #462 - frozen", () => {1561+ test("issue #462 - frozen", () => {1562 var origin = {1563 a: {1564 value: "no"1565 },1566 b: {1567 value: "no"1568 }1569 }1586- var im = produce(origin, origin => {1587- origin.a.value = "im"1570+ const next = produce(origin, draft => {1571+ draft.a.value = "im"1572 })1589- expect(() => (origin.b.value = "yes")).toThrow() // should throw!1590- // to prevent this...:1591- // expect(im.b.value).toBe('no');1573+ expect(() => {1574+ origin.b.value = "yes"1575+ }).toThrowError(1576+ "Cannot assign to read only property 'value' of object '#<Object>'"1577+ )1578+ expect(() => {1579+ next.b.value = "yes"1580+ }).toThrowError(1581+ "Cannot assign to read only property 'value' of object '#<Object>'"1582+ ) // should throw!1583 })1584 1585 autoFreeze &&1586 test("issue #469, state not frozen", () => {522 unmodified lines2109 testObjectType(name + " (frozen)", Object.freeze(copy))2110 }2111 function testObjectType(name, base) {2112 describe(name, () => {2122- it.skip("creates a draft", () => {2113+ it("creates a draft", () => {2114 produce(base, draft => {2115 expect(draft).not.toBe(base)2116 expect(shallowCopy(draft, true)).toEqual(base)2117 })131 unmodified linesmodified:__tests__/frozen.js+2 added-2 removed
28 unmodified lines29 expect(isFrozen(next)).toBeTruthy()30 expect(isFrozen(next.arr)).toBeTruthy()31 })32 33- it("never freezes reused state", () => {33+ it("freezes reused base state", () => {34 const base = {arr: [1], obj: {a: 1}}35 const next = produce(base, draft => {36 draft.arr.push(1)37 })38 expect(next.obj).toBe(base.obj)39- expect(isFrozen(next.obj)).toBeFalsy()39+ expect(isFrozen(next.obj)).toBeTruthy()40 })41 42 describe("the result is always auto-frozen when", () => {43 it("the root draft is mutated (and no error is thrown)", () => {173 unmodified linesmodified:src/core/finalize.ts+4 added-7 removed
17 unmodified lines181920212222232425262758 unmodified lines8687888989909192939438 unmodified lines13313413513613613713813913714113813914014114216 unmodified lines17 unmodified lines ProxyTypeSet, getPlugin, die, revokeScope, isFrozen isFrozen, shallowCopy} from "../internal"
export function processResult(result: any, scope: ImmerScope) { scope.unfinalizedDrafts_ = scope.drafts_.length58 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_ = getPlugin("ES5").createFinalCopy_(state)) ? (state.copy_ = shallowCopy(state.draft_, true)) : state.copy_ // finalize all children of the copy each(result as any, (key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path)38 unmodified lines if (isDraft(res)) { rootScope.canAutoFreeze_ = false } else return } // Unchanged draft properties are ignored. if (parentState && is(childValue, get(parentState!.base_, prop))) { return } // Search new objects for unfinalized drafts. Frozen objects should never contain drafts. if (isDraftable(childValue)) { if (isDraftable(childValue) && !Object.isFrozen(childValue)) { if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) { // optimization: if an object is not a draft, and we don't have to // deepfreeze everything, and we are sure that no drafts are left in the remaining object // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.16 unmodified linesmodified:src/core/immerClass.ts-11 removed
3 unmodified lines4 ImmerState,5 Drafted,6 isDraftable,7 processResult,8- NOTHING,8 Patch,9 Objectish,10 DRAFT_STATE,11 Draft,12 PatchListener,13 isDraft,14 isMap,15 isSet,17- markChangedProxy,16 createProxyProxy,19- freeze,17 getPlugin,18 die,19 hasProxies,20 isMinified,195 unmodified lines216 const scope = parent ? parent.scope_ : getCurrentScope()217 scope.drafts_.push(draft)218 return draft219 }223-224-export function markChanged(immer: Immer, state: ImmerState) {225- if (immer.useProxies_) {226- markChangedProxy(state)227- } else {228- getPlugin("ES5").markChangedES5_(state)229- }230-}modified:src/core/proxy.ts+33 added-38 removed
27 unmodified lines28 }29 30 export interface ProxyObjectState extends ProxyBaseState {31 type_: typeof ProxyTypeProxyObject32- base_: AnyObject32+ base_: any33 copy_: any34 draft_: Drafted<AnyObject, ProxyObjectState>35 }36 22 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?64 finalized_: false,65 // Track which properties have been assigned (true) or deleted (false).66 assigned_: {},67 // The parent draft state.30 unmodified lines98 99 /**100 * Object drafts101 */101-const objectTraps: ProxyHandler<ProxyState> = {102+export const objectTraps: ProxyHandler<ProxyState> = {103 get(state, prop) {104 if (prop === DRAFT_STATE) return state105 105- const value = latest(state)[prop]106+ const source = latest(state)107+ if (!has(source, prop)) {108+ // non-existing or non-own property...109+ return readPropFromProto(state, source, prop)110+ }111+ const value = source[prop]112 if (state.finalized_ || !isDraftable(value)) {113 return value114 }109-115 // Check for existing draft in modified state.111- // TODO: dedupe116 // Assigned values are never drafted. This catches any drafts we created, too.117 if (value === peek(state.base_, prop)) {118 prepareCopy(state)119 return (state.copy_![prop as any] = createProxy(11 unmodified lines131 },132 ownKeys(state) {133 return Reflect.ownKeys(latest(state))134 },131- // TODO: dedupe135 set(state, prop: string /* strictly not, but helps TS */, value) {136 state.assigned_[prop] = true137 if (!state.modified_) {135- if (is(value, peek(latest(state), prop))) return true138+ if (is(value, peek(latest(state), prop)) && value !== undefined)139+ return true140 prepareCopy(state)137- markChangedProxy(state)141+ markChanged(state)142 }143 // @ts-ignore144 state.copy_![prop] = value145 return true2 unmodified lines148 // The `undefined` check is a fast path for pre-existing keys.149 if (peek(state.base_, prop) !== undefined || prop in state.base_) {150 state.assigned_[prop] = false151 prepareCopy(state)148- markChangedProxy(state)152+ markChanged(state)153 } else if (state.assigned_[prop]) {154 // if an originally not assigned property was deleted155 delete state.assigned_[prop]156 }45 unmodified lines202 if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)203 return objectTraps.set!.call(this, state[0], prop, value, state[0])204 }205 202-/**203- * Map drafts204- */205-206 // Access a property without creating an Immer draft.207-// TODO: duplicate with ES5?208-function peek(draft: Drafted, prop: PropertyKey): any {207+function peek(draft: Drafted, prop: PropertyKey) {208 const state = draft[DRAFT_STATE]210- const desc = Reflect.getOwnPropertyDescriptor(211- state ? latest(state) : draft,212- prop213- )214- return desc && desc.value209+ const source = state ? latest(state) : draft210+ return source[prop]211 }212 217-// TODO: dedupe218-export function markChangedProxy(state: ImmerState) {213+function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {214+ // 'in' checks proto!215+ if (!(prop in source)) return undefined216+ let proto = Object.getPrototypeOf(source)217+ while (proto) {218+ const desc = Object.getOwnPropertyDescriptor(proto, prop)219+ // This is a very special case, if the prop is a getter defined by the220+ // prototype, we should invoke it with the draft as context!221+ if (desc) return `value` in desc ? desc.value : desc.get?.call(state.draft_)222+ proto = Object.getPrototypeOf(proto)223+ }224+ return undefined225+}226+227+export function markChanged(state: ImmerState) {228 if (!state.modified_) {229 state.modified_ = true221- // if (222- // state.type_ === ProxyTypeProxyObject ||223- // state.type_ === ProxyTypeProxyArray224- // ) {225- // prepareCopy(state);226- // const copy = (state.copy_ = shallowCopy(state.base_, true))227- // each(state.drafts_!, (key, value) => {228- // // @ts-ignore229- // copy[key] = value230- // })231- // state.drafts_ = undefined232- // }233-230 if (state.parent_) {235- markChangedProxy(state.parent_)231+ markChanged(state.parent_)232 }233 }234 }235 240-// TODO: duplicate with ES5241-function prepareCopy(state: ProxyState) {236+export function prepareCopy(state: {base_: any; copy_: any}) {237 if (!state.copy_) {238 state.copy_ = shallowCopy(state.base_, true)239 }240 }modified:src/plugins/es5.ts+20 added-112 removed
4 unmodified lines5 ES5ObjectState,6 each,7 has,8 isDraft,9- isDraftable,10- shallowCopy,9 latest,10 DRAFT_STATE,11 is,12 loadPlugin,13 ImmerScope,16- createProxy,14 ProxyTypeES5Array,15 ProxyTypeES5Object,19- AnyObject,16 getCurrentScope,21- die17+ die,18+ markChanged,19+ objectTraps20 } from "../internal"21 22 type ES5State = ES5ArrayState | ES5ObjectState23 23 unmodified lines47 }48 49 function createES5Draft(isArray: boolean, base: any) {50 // Create a new object / array, where each own property is trapped with an accessor51+ // TODO: extract all utils used in this fn?52 const descriptors = Object.getOwnPropertyDescriptors(base)53 // Descriptors we want to skip:54 if (isArray) delete descriptors.length55 delete descriptors[DRAFT_STATE as any]19 unmodified lines75 const isArray = Array.isArray(base)76 const draft = createES5Draft(isArray, base)77 78 const state: ES5ObjectState | ES5ArrayState = {79+ // TODO: reuse this object construction from proxy?80 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),81 scope_: parent ? parent.scope_ : getCurrentScope(),82 modified_: false,83+ // TODO: still needed?84 finalizing_: false,85 finalized_: false,86 assigned_: {},87 parent_: parent,13 unmodified lines101 })102 return draft103 }104 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_) {108- // state.finalizing_ = true // TODO: still needed?109- // If not, delete this block!110- const value = draft[prop]111- // state.finalizing_ = false112- return value113- }114- return draft[prop]115- }116-117- function get(state: ES5State, prop: string | number) {118- assertUnrevoked(state)119- const value = peek(latest(state), prop)120- if (state.finalizing_) return value121- // Create a draft if the value is unmodified.122- if (value === peek(state.base_, prop) && isDraftable(value)) {123- prepareCopy(state)124- // @ts-ignore125- return (state.copy_![prop] = createProxy(126- state.scope_.immer_,127- value,128- state129- ))130- }131- return value132- }133-134- function set(state: ES5State, prop: string | number, value: any) {135- assertUnrevoked(state)136- state.assigned_[prop] = true137- if (!state.modified_) {138- if (is(value, peek(latest(state), prop))) return139- markChangedES5_(state)140- prepareCopy(state)141- }142- // @ts-ignore143- state.copy_![prop] = value144- }145-146- function markChangedES5_(state: ImmerState) {147- if (!state.modified_) {148- state.modified_ = true149- if (state.parent_) markChangedES5_(state.parent_)150- }151- }152-153- function prepareCopy(state: ES5State) {154- if (state.copy_) return155- const stateofBase: ES5State | undefined = (state.base_ as any)[DRAFT_STATE]156-157- // make a copy, if the base is a draft itself, we take a copy of it's current state158- // stateofBase?.finalizing_ = true; // TODO: needed?159-160- // TODO: some code reuse?161- // Object.getOwnPropertyDescriptors162- state.copy_ = shallowCopy(state.base_, true)163- // stateofBase?.finalizing_ = false; // TODO: needed?164- }165-166- function createFinalCopy_(state: ES5ArrayState | ES5ObjectState): any {167- state.finalizing_ = true168- const res = shallowCopy(state.draft_, true)169- state.finalizing_ = false170- return res171-172- 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_)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 one185- 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-105 // property descriptors are recycled to make sure we don't create a get and set closure per property,106 // but share them all instead107 const descriptors: {[prop: string]: PropertyDescriptor} = {}108 8 unmodified lines117 descriptors[prop] = desc = {118 configurable: true,119 enumerable,120 get(this: any) {211- return get(this[DRAFT_STATE], prop)121+ const state = this[DRAFT_STATE]122+ assertUnrevoked(state)123+ // @ts-ignore124+ return objectTraps.get(state, prop)125 },126 set(this: any, value) {214- set(this[DRAFT_STATE], prop, value)127+ const state = this[DRAFT_STATE]128+ assertUnrevoked(state)129+ // @ts-ignore130+ objectTraps.set(state, prop, value)131 }132 }133 }134 return desc9 unmodified lines144 const state: ES5State = drafts[i][DRAFT_STATE]145 if (!state.modified_) {146 switch (state.type_) {147 case ProxyTypeES5Array:232- if (hasArrayChanges(state)) markChangedES5_(state)148+ if (hasArrayChanges(state)) markChanged(state)149 break150 case ProxyTypeES5Object:235- if (hasObjectChanges(state)) markChangedES5_(state)151+ if (hasObjectChanges(state)) markChanged(state)152 break153 }154 }155 }14 unmodified lines170 if ((key as any) === DRAFT_STATE) return171 // The `undefined` check is a fast path for pre-existing keys.172 if ((base_ as any)[key] === undefined && !has(base_, key)) {173 assigned_[key] = true258- markChangedES5_(state)174+ markChanged(state)175 } else if (!assigned_[key]) {176 // Only untouched properties trigger recursion.177 markChangesRecursively(draft_[key])178 }2 unmodified lines181 each(base_, key => {182 // The `undefined` check is a fast path for pre-existing keys.183 if (draft_[key] === undefined && !has(draft_, key)) {184 assigned_[key] = false269- markChangedES5_(state)185+ markChanged(state)186 }187 })188 } else if (type_ === ProxyTypeES5Array) {189 if (hasArrayChanges(state as ES5ArrayState)) {274- markChangedES5_(state)190+ markChanged(state)191 assigned_.length = true192 }193 194 if (draft_.length < base_.length) {60 unmodified lines255 // For all other cases, we don't have to compare, as they would have been picked up by the index setters256 return false257 }258 343- /*#__PURE__*/344- function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {345- const desc = Object.getOwnPropertyDescriptor(base, prop)346- return desc && desc.enumerable ? true : false347- }348-259 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {260 if (state.revoked_) die(3, JSON.stringify(latest(state)))261 }262 263 loadPlugin("ES5", {264 createES5Proxy_,355- markChangedES5_,356- willFinalizeES5_,265+ willFinalizeES5_357- createFinalCopy_266 })267 }modified:src/plugins/mapset.ts+6 added-6 removed
80 unmodified lines81 const state: MapState = this[DRAFT_STATE]82 assertUnrevoked(state)83 if (latest(state).get(key) !== value) {84 prepareMapCopy(state)85- markChanged(state.scope_.immer_, state)85+ markChanged(state)86 state.assigned_!.set(key, true)87 state.copy_!.set(key, value)88 state.assigned_!.set(key, true)89 }7 unmodified lines97 98 const state: MapState = this[DRAFT_STATE]99 assertUnrevoked(state)100 prepareMapCopy(state)101- markChanged(state.scope_.immer_, state)101+ markChanged(state)102 state.assigned_!.set(key, false)103 state.copy_!.delete(key)104 return true105 }1 unmodified line107 p.clear = function() {108 const state: MapState = this[DRAFT_STATE]109 assertUnrevoked(state)110 prepareMapCopy(state)111- markChanged(state.scope_.immer_, state)111+ markChanged(state)112 state.assigned_ = new Map()113 return state.copy_!.clear()114 }115 126 unmodified lines242 const state: SetState = this[DRAFT_STATE]243 assertUnrevoked(state)244 if (!this.has(value)) {245 prepareSetCopy(state)246- markChanged(state.scope_.immer_, state)246+ markChanged(state)247 state.copy_!.add(value)248 }249 return this250 }5 unmodified lines256 257 const state: SetState = this[DRAFT_STATE]258 assertUnrevoked(state)259 prepareSetCopy(state)260- markChanged(state.scope_.immer_, state)260+ markChanged(state)261 return (262 state.copy_!.delete(value) ||263 (state.drafts_.has(value)264 ? state.copy_!.delete(state.drafts_.get(value))4 unmodified lines269 p.clear = function() {270 const state: SetState = this[DRAFT_STATE]271 assertUnrevoked(state)272 prepareSetCopy(state)273- markChanged(state.scope_.immer_, state)273+ markChanged(state)274 return state.copy_!.clear()275 }276 277 p.values = function(): IterableIterator<any> {60 unmodified linesmodified:src/utils/plugins.ts+2 added-5 removed
3 unmodified lines4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8- AnyArray,8 AnyMap,9 AnySet,10 ProxyTypeES5Array,11 ProxyTypeES5Object,24 unmodified lines36 createES5Proxy_<T>(37 base: T,38 parent?: ImmerState39 ): Drafted<T, ES5ObjectState | ES5ArrayState>41- markChangedES5_(state: ImmerState): void42- createFinalCopy_(state: ES5ArrayState | ES5ObjectState): any40 }41 MapSet?: {42 proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T43 proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T38 unmodified lines82 83 export interface ES5ArrayState extends ES5BaseState {84 type_: typeof ProxyTypeES5Array85 draft_: Drafted<AnyObject, ES5ArrayState>89- base_: AnyArray90- copy_: AnyArray | null86+ base_: any87+ copy_: any88 }89 90 /** Map / Set plugin */91 21 unmodified linesmodified:jest.config.js+2 added-1 removed
3 unmodified lines4 __DEV__: true,5 "ts-jest": {6 tsConfig: {7 noUnusedLocals: false8- }8+ },9+ disableSourceMapSupport: true10 }11 },12 preset: "ts-jest/presets/js-with-ts",13 testEnvironment: "node",2 unmodified lines