Skip to content
Myr

Fixed #116, failed to detect array changes when made shorter, then longer

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 17d0730 · Browse files · 3 files+22 added−1 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+22 added−1 removed

  • tests:+8 added−0 removed
  • other:+14 added−1 removed

3 files+22 added−1 removed

modified:__tests__/base.js+8 added
848 unmodified lines
849 })850 }).toThrow(/An immer producer returned a new value/)851 })852853 it("should fix #116", () => {854 const nextState = produce([1, 2, 3], s => {855 s.pop()856 s.push(100)857 })858 expect(nextState).toEqual([1, 2, 100])859 })860861 afterEach(() => {862 expect(baseState).toBe(origBaseState)863 expect(baseState).toEqual(createBaseState())864 })
21 unmodified lines
modified:src/es5.js+11 added-1 removed
125 unmodified lines
126 return !shallowEqual(baseKeys, keys)127}128129function hasArrayChanges(state) {130 return state.proxy.length !== state.base.length130 const {proxy} = state131 if (proxy.length !== state.base.length) return true132 // See #116133 // If we first shorten the length, our array interceptors will be removed.134 // If after that new items are added, result in the same original length,135 // those last items will have no intercepting property.136 // So if there is no own descriptor on the last position, we know that items were removed and added137 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)138 if (descriptor && !descriptor.get) return true139 // For all other cases, we don't have to compare, as they would have been picked up by the index setters140 return false141}142143export function produceEs5(baseState, producer) {144 const prevStates = states
63 unmodified lines
modified:changelog.md+3 added