Skip to content

fix(reactivity): trigger the ref that is created by toRef from a reactive array #12431

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

noootwo
Copy link
Contributor

@noootwo noootwo commented Nov 18, 2024

fix #12427

Copy link

pkg-pr-new bot commented Nov 18, 2024

Open in Stackblitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@12431

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@12431

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@12431

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@12431

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@12431

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@12431

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@12431

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@12431

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@12431

vue

pnpm add https://pkg.pr.new/vue@12431

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@12431

commit: eccd16e

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (+43 B) 38 kB (+20 B) 34.2 kB (-26 B)
vue.global.prod.js 158 kB (+43 B) 57.8 kB (+21 B) 51.4 kB (-4 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 47.2 kB 18.3 kB 16.8 kB
createApp 55.2 kB 21.3 kB 19.5 kB
createSSRApp 59.3 kB 23.1 kB 21 kB
defineCustomElement 60.1 kB 22.9 kB 20.8 kB
overall 69.1 kB 26.5 kB 24.1 kB

@edison1105 edison1105 added scope: reactivity 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Nov 18, 2024
@edison1105
Copy link
Member

According to the reproduction in #12427, the issue likely lies within toRef. If source is an array and the key is a number, the key should be converted to a string. However, modifying getDepFromRective broadens the impact scope.
Additionally, getDepFromReactive is not a public API, and the test case should ideally use public APIs.

@noootwo
Copy link
Contributor Author

noootwo commented Nov 19, 2024

According to the reproduction in #12427, the issue likely lies within toRef. If source is an array and the key is a number, the key should be converted to a string. However, modifying getDepFromRective broadens the impact scope. Additionally, getDepFromReactive is not a public API, and the test case should ideally use public APIs.

But for getDepFromReactive, it should indeed exhibit such behavior, as it needs to handle this case for all reactive arrays.

@edison1105 edison1105 changed the title fix(reactivity): triggerRef fails when target is an array fix(reactivity): trigger the ref that is created by toRef from a reactive array Nov 19, 2024
@edison1105 edison1105 added ready to merge The PR is ready to be merged. and removed wait changes labels Nov 19, 2024
Copy link
Contributor

@skirtles-code skirtles-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix looks reasonable to me, but I'd like to propose an alternative.

I think the underlying reason why this is happening is that JavaScript treats all property keys as either strings and symbols. Even for arrays, the numbers used as keys are treated as strings. e.g.:

Object.keys([7, 8, 9]) // returns ['0', '1', '2']

When we go through a Proxy, the key passed to the trap will either be a string or a symbol. Any other value is coerced to a string before it's passed. e.g.:

const p = new Proxy([], {
  get(target, key) {
    console.log(key, typeof key)
    return target[key]
  }
})

p[0]

This example is using a Proxy around an array, and p[0] triggers the trap with a string key of "0".

But the coercion doesn't just apply to numbers, it applies to any value that isn't a string or symbol. For example, if you tried to use an array as a property key:

p[[0]]

This would actually work, as [0] is coerced to the string "0".

So the reactive Proxy wrappers used by Vue are passed key values that are either symbols or strings, nothing else. This carries across throughout dependency tracking, leading to strings in the dependency maps, even for arrays.

Collections (e.g. Set, Map, etc.) are different, as they can have other types of key, but as they aren't relevant to toRef I'm going to ignore them.

For the most part, Vue doesn't need to worry about ensuring the keys are coerced correctly, because that's already handled by JavaScript. It's only an issue when Vue gets passed a property name directly. An example of that is hasOwnProperty, see:

if (!isSymbol(key)) key = String(key)

There we need to coerce non-symbols to strings.

So I think the toRef problem is essentially the same underlying issue as #10455. We need to ensure we coerce non-symbol keys to strings. While the primary use case is for arrays, it also ensures that we don't hit any other weird edge cases, like the example I gave earlier where I passed an array as a key.

The fix I'd propose is something like this:

class ObjectRefImpl<T extends object, K extends keyof T> {
  public readonly [ReactiveFlags.IS_REF] = true
  public _value: T[K] = undefined!
  private readonly _key: K

  constructor(
    private readonly _object: T,
    key: K,
    private readonly _defaultValue?: T[K],
  ) {
    this._key = isSymbol(key) ? key : String(key) as K
  }

The important part is isSymbol(key) ? key : String(key).

There may be a neater way to write that (I'm not entirely happy with the types), but I think this is a more appropriate place to handle the coercion. We could handle it further upstream, but this seemed more natural to me.

I tested this locally and it seemed to work. I think it's a more accurate fix and it also adds fewer bytes to the build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: reactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

triggerRef fails when target is an array
4 participants