Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(reactivity): ref as a reactive parameter should return (#6358) #6359

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ describe('reactivity/reactive', () => {
expect(isReactive(observed)).toBe(false)
})

test('ref is called as an argument to reactive', () => {
const obj = reactive(ref(1))
const spy1 = vi.fn(() => obj.value)

effect(spy1)

obj.value = 2
expect(spy1).toBeCalledTimes(2)
expect(isReactive(obj)).toBe(true)
})

test('hasOwnProperty edge case: Symbol values', () => {
const key = Symbol()
const obj = reactive({ [key]: 1 }) as { [key]?: 1 }
Expand Down
11 changes: 11 additions & 0 deletions packages/reactivity/__tests__/shallowReactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ describe('shallowReactive', () => {
expect(r.deep).toBe(deep)
})

test('ref is called as an argument to shallowReactive', () => {
const obj = shallowReactive(ref(1))
const spy1 = vi.fn(() => obj.value)

effect(spy1)

obj.value = 2
expect(spy1).toBeCalledTimes(2)
expect(isReactive(obj)).toBe(true)
})

describe('collections', () => {
test('should be reactive', () => {
const shallowSet = shallowReactive(new Set())
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
protected readonly _isShallow = false,
) {}

get(target: Target, key: string | symbol, receiver: object): any {

Check failure on line 55 in packages/reactivity/src/baseHandlers.ts

View workflow job for this annotation

GitHub Actions / test / unit-test

packages/reactivity/__tests__/reactive.spec.ts > reactivity/reactive > The results of the shallow and readonly assignments are the same (Map)

RangeError: Maximum call stack size exceeded ❯ MutableReactiveHandler.get packages/reactivity/src/baseHandlers.ts:55:3 ❯ Dep.track packages/reactivity/src/dep.ts:104:30 ❯ Module.track packages/reactivity/src/dep.ts:219:11 ❯ MutableReactiveHandler.get packages/reactivity/src/baseHandlers.ts:111:7 ❯ Dep.track packages/reactivity/src/dep.ts:104:30 ❯ Module.track packages/reactivity/src/dep.ts:219:11 ❯ MutableReactiveHandler.get packages/reactivity/src/baseHandlers.ts:111:7 ❯ Dep.track packages/reactivity/src/dep.ts:104:30 ❯ Module.track packages/reactivity/src/dep.ts:219:11 ❯ MutableReactiveHandler.get packages/reactivity/src/baseHandlers.ts:111:7
const isReadonly = this._isReadonly,
isShallow = this._isShallow
if (key === ReactiveFlags.IS_REACTIVE) {
Expand Down Expand Up @@ -81,7 +81,7 @@
// early return undefined
return
}

const rawTarget = toRaw(target)
const targetIsArray = isArray(target)

if (!isReadonly) {
Expand All @@ -107,7 +107,7 @@
return res
}

if (!isReadonly) {
if (!isReadonly && !isRef(rawTarget)) {
track(target, TrackOpTypes.GET, key)
}

Expand Down
1 change: 1 addition & 0 deletions packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ function createReactiveObject(
}
return target
}

// target is already a Proxy, return it.
// exception: calling readonly() on a reactive object
if (
Expand Down
Loading