Skip to content
Myr

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

10 files+92 added−230 removed

modified:__tests__/__snapshots__/base.js.snap-16 removed
20 unmodified lines
21exports[`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? {}"`;2223exports[`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? {}"`;2425exports[`base functionality - es5 (autofreeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;2625exports[`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."`;2627exports[`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 lines
45exports[`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? {}"`;4647exports[`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? {}"`;4851exports[`base functionality - es5 (autofreeze)(patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;5249exports[`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."`;5051exports[`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 lines
69exports[`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? {}"`;7071exports[`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? {}"`;7277exports[`base functionality - es5 (no freeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;7873exports[`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."`;7475exports[`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 lines
93exports[`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? {}"`;9495exports[`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? {}"`;96103exports[`base functionality - es5 (patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;10497exports[`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."`;9899exports[`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 lines
129exports[`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? {}"`;130131exports[`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? {}"`;132141exports[`base functionality - proxy (autofreeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;142133exports[`base functionality - proxy (autofreeze) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;134135exports[`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 lines
169exports[`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? {}"`;170171exports[`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? {}"`;172183exports[`base functionality - proxy (autofreeze)(patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;184173exports[`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"`;174175exports[`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 lines
209exports[`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? {}"`;210211exports[`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? {}"`;212225exports[`base functionality - proxy (no freeze) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;226213exports[`base functionality - proxy (no freeze) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;214215exports[`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 lines
249exports[`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? {}"`;250251exports[`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? {}"`;252267exports[`base functionality - proxy (patch listener) throws on computed properties 1`] = `"[Immer] Immer drafts cannot have computed properties"`;268253exports[`base functionality - proxy (patch listener) throws when Object.defineProperty() is used on drafts 1`] = `"[Immer] Object.defineProperty() cannot be used on an Immer draft"`;254255exports[`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 lines
modified:__tests__/base.js+23 added-32 removed
19 unmodified lines
20test("immer should have no dependencies", () => {21 expect(require("../package.json").dependencies).toBeUndefined()22})2324// 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)24runBaseTest("proxy (no freeze)", true, false)25runBaseTest("proxy (autofreeze)", true, true)26runBaseTest("proxy (patch listener)", true, false, true)27runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)2829runBaseTest("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)30runBaseTest("es5 (autofreeze)", false, true)31runBaseTest("es5 (patch listener)", false, false, true)32runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)3334function runBaseTest(name, useProxies, autoFreeze, useListener) {35 const listener = useListener ? function() {} : undefined36 const {produce, produceWithPatches} = createPatchedImmer({
858 unmodified lines
895 expect(nextState).not.toBe(baseState)896 expect(nextState.foo).toBeTruthy()897 })898899 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 lines
933 expect(nextState.foo).toBeTruthy()934 expect(isEnumerable(nextState, "foo")).toBeFalsy()935 })936937 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 })952937 it("can work with own computed props", () => {938 const baseState = {939 x: 1,940 get y() {
455 unmodified lines
1396 parent.b.x // Ensure proxy not revoked.1397 })1398 })13991416 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 lines
1557 expect(next2).toBe(a)1558 })15591560 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 })15841585 autoFreeze &&1586 test("issue #469, state not frozen", () => {
522 unmodified lines
2109 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 lines
modified:__tests__/frozen.js+2 added-2 removed
28 unmodified lines
29 expect(isFrozen(next)).toBeTruthy()30 expect(isFrozen(next.arr)).toBeTruthy()31 })3233 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 })4142 describe("the result is always auto-frozen when", () => {43 it("the root draft is mutated (and no error is thrown)", () => {
173 unmodified lines
modified:src/core/finalize.ts+4 added-7 removed
modified:src/core/immerClass.ts-11 removed
3 unmodified lines
4 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 lines
216 const scope = parent ? parent.scope_ : getCurrentScope()217 scope.drafts_.push(draft)218 return draft219}223224export 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 lines
28}2930export interface ProxyObjectState extends ProxyBaseState {31 type_: typeof ProxyTypeProxyObject32 base_: AnyObject32 base_: any33 copy_: any34 draft_: Drafted<AnyObject, ProxyObjectState>35}36
22 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?64 finalized_: false,65 // Track which properties have been assigned (true) or deleted (false).66 assigned_: {},67 // The parent draft state.
30 unmodified lines
9899/**100 * Object drafts101 */101const objectTraps: ProxyHandler<ProxyState> = {102export const objectTraps: ProxyHandler<ProxyState> = {103 get(state, prop) {104 if (prop === DRAFT_STATE) return state105105 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 }109115 // 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 lines
131 },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 true
2 unmodified lines
148 // 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 lines
202 if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)203 return objectTraps.set!.call(this, state[0], prop, value, state[0])204}205202/**203 * Map drafts204 */205206// Access a property without creating an Immer draft.207// TODO: duplicate with ES5?208function peek(draft: Drafted, prop: PropertyKey): any {207function 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}212217// TODO: dedupe218export function markChangedProxy(state: ImmerState) {213function 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}226227export 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 // }233230 if (state.parent_) {235 markChangedProxy(state.parent_)231 markChanged(state.parent_)232 }233 }234}235240// TODO: duplicate with ES5241function prepareCopy(state: ProxyState) {236export 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 lines
5 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"2122type ES5State = ES5ArrayState | ES5ObjectState23
23 unmodified lines
47 }4849 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 lines
75 const isArray = Array.isArray(base)76 const draft = createES5Draft(isArray, base)7778 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 lines
101 })102 return draft103 }104104 // 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 }116117 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 }133134 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 }145146 function markChangedES5_(state: ImmerState) {147 if (!state.modified_) {148 state.modified_ = true149 if (state.parent_) markChangedES5_(state.parent_)150 }151 }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 state158 // 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_)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 }194105 // 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 lines
117 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 desc
9 unmodified lines
144 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 lines
170 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 lines
181 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 }193194 if (draft_.length < base_.length) {
60 unmodified lines
255 // For all other cases, we don't have to compare, as they would have been picked up by the index setters256 return false257 }258343 /*#__PURE__*/344 function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {345 const desc = Object.getOwnPropertyDescriptor(base, prop)346 return desc && desc.enumerable ? true : false347 }348259 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {260 if (state.revoked_) die(3, JSON.stringify(latest(state)))261 }262263 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 lines
81 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 lines
9798 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 line
107 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 lines
242 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 lines
256257 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 lines
269 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 }276277 p.values = function(): IterableIterator<any> {
60 unmodified lines
modified:src/utils/plugins.ts+2 added-5 removed
3 unmodified lines
4 ImmerScope,5 Drafted,6 AnyObject,7 ImmerBaseState,8 AnyArray,8 AnyMap,9 AnySet,10 ProxyTypeES5Array,11 ProxyTypeES5Object,
24 unmodified lines
36 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): T
38 unmodified lines
8283export interface ES5ArrayState extends ES5BaseState {84 type_: typeof ProxyTypeES5Array85 draft_: Drafted<AnyObject, ES5ArrayState>89 base_: AnyArray90 copy_: AnyArray | null86 base_: any87 copy_: any88}8990/** Map / Set plugin */91
21 unmodified lines
modified:jest.config.js+2 added-1 removed
3 unmodified lines
4 __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