Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): Array.prototype.push causes infinite recursion #2135

Closed
wants to merge 5 commits into from
Closed

fix(reactivity): Array.prototype.push causes infinite recursion #2135

wants to merge 5 commits into from

Conversation

unbyte
Copy link
Contributor

@unbyte unbyte commented Sep 16, 2020

Calling Array.prototype.push, which gets .length and sets values at the same time, may lead to an infinite recursion

for example

  const a = reactive([])

  watchEffect(() => {
      a.push(1)
  })

  watchEffect(() => {
      a.push(1)
  })

@yyx990803
Copy link
Member

This is fixed by 3810de7

@yyx990803 yyx990803 closed this Sep 16, 2020
@unbyte
Copy link
Contributor Author

unbyte commented Sep 16, 2020

@yyx990803 This fix has nothing to do with 3810de7

@unbyte
Copy link
Contributor Author

unbyte commented Sep 16, 2020

I've pull the latest code and run the test, and it failed

  it('should avoid infinite recursive loops when use Array.prototype.push', () => {
    const arr = reactive<number[]>([])

    const counterSpy1 = jest.fn(() => arr.push(1))
    const counterSpy2 = jest.fn(() => arr.push(2))
    effect(counterSpy1)
    effect(counterSpy2)
    expect(arr.length).toBe(2)
    expect(counterSpy1).toHaveBeenCalledTimes(1)
    arr.push(3)
    expect(arr.length).toBe(3)
    expect(counterSpy1).toHaveBeenCalledTimes(1)
  })

This recursion is not due to the self-calls of the effect, but that push changes length while it depends on length.

@dsonet
Copy link
Contributor

dsonet commented Sep 17, 2020

@unbyte If so, unshift may should also be included.

@unbyte
Copy link
Contributor Author

unbyte commented Sep 17, 2020

@dsonet and pop and shift, they all cause infinite recursion cuz they all depend on .length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants