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): shallowReactive map "unwraps" the nested refs #8503

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/reactivity/__tests__/shallowReactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ describe('shallowReactive', () => {
shallowSet.forEach(x => expect(isReactive(x)).toBe(false))
})

test('Setting a reactive object on a shallowReactive map', () => {
const msg = ref('ads')
const bar = reactive({ msg })
const foo = shallowReactive(new Map([['foo1', bar]]))
foo.set('foo2', bar)

expect(isReactive(foo.get('foo2'))).toBe(true)
expect(isReactive(foo.get('foo1'))).toBe(true)
})

// #1210
test('onTrack on called on objectSpread', () => {
const onTrackFn = vi.fn()
Expand Down
16 changes: 10 additions & 6 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function size(target: IterableCollections, isReadonly = false) {
return Reflect.get(target, 'size', target)
}

function add(this: SetTypes, value: unknown) {
value = toRaw(value)
function add(this: SetTypes, value: unknown, isShallow = false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a boolean flag to a function is generally something that sets off alarm bells for me. There's a lot of stuff been written on this topic, e.g. https://martinfowler.com/bliki/FlagArgument.html, but that isn't to say it's necessarily wrong.

When I first looked over this PR a few weeks ago, I felt like the boolean flag was justified here. But it kept nagging away at me, and I'm now wondering whether there's a better way.

First, I'd like to consider this example:

It's a silly example, but it shows how the flag can be passed accidentally by client code. In that example, the index is being passed by forEach as the isShallow value.

Does this matter? Maybe not, but it motivated me to ponder alternative implementations.

I'm now wondering whether this might be a better way to implement it:

  1. Remove the call to toRaw in add and set. Have them effectively be shallow by default.

  2. Override the calls to add and set in mutableInstrumentations to include the toRaw call. Something like:

    add(this: SetTypes, value: unknown) {
      return add.call(this, toRaw(value))
    },
    set(this: MapTypes, key: unknown, value: unknown) {
      return set.call(this, key, toRaw(value))
    },

This avoids the need for the boolean flag. I tried this locally and it seemed to pass the test cases.

Copy link
Contributor Author

@liuseen-l liuseen-l Jun 28, 2024

Choose a reason for hiding this comment

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

Method 1 I don't think is advisable, it will lead to the contamination of the raw data, as shown in the code below, modifying the state through the raw data will cause the effect to be re-executed, and toRaw is used to avoid this

 const m = new Map()
 const o1 = reactive(m)
 const o2 = reactive(new Map())
 o1.set('o2', o2)
 effect(() => {
 console.log(m.get('o2').size)
 })
 m.get('o2').set('foo', 1)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you misunderstood what I meant. I wasn't proposing two separate methods for fixing the problem, those were two parts of a single method.

I've put together an example to show the changes I had in mind:

skirtles-code@073ab1c

The calls to toRaw are still present, they're just moved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, I'm sorry, I misunderstood

Copy link
Member

Choose a reason for hiding this comment

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

I think adding the isShallow arg is fine in this case because it is consistent with how other functions in the same file are structured. The isShallow flag is used only for value unwrapping in add and set, but affects more things in other functions - in general, these handlers have to be aware of the context they are called in.

value = isShallow ? value : toRaw(value)
const target = toRaw(this)
const proto = getProto(target)
const hadKey = proto.has.call(target, value)
Expand All @@ -78,8 +78,8 @@ function add(this: SetTypes, value: unknown) {
return this
}

function set(this: MapTypes, key: unknown, value: unknown) {
value = toRaw(value)
function set(this: MapTypes, key: unknown, value: unknown, isShallow = false) {
value = isShallow ? value : toRaw(value)
const target = toRaw(this)
const { has, get } = getProto(target)

Expand Down Expand Up @@ -251,8 +251,12 @@ function createInstrumentations() {
return size(this as unknown as IterableCollections)
},
has,
add,
set,
add(this: SetTypes, value: unknown) {
return add.call(this, value, true)
},
set(this: MapTypes, key: unknown, value: unknown) {
return set.call(this, key, value, true)
},
delete: deleteEntry,
clear,
forEach: createForEach(false, true)
Expand Down
Loading