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

test: reactive proto #1108

Merged
merged 2 commits into from
May 3, 2020
Merged
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
14 changes: 14 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ describe('reactivity/reactive', () => {
expect(Object.keys(observed)).toEqual(['foo'])
})

test('proto', () => {
const obj = {}
const reactiveObj = reactive(obj)
expect(isReactive(reactiveObj)).toBe(true)
// read prop of reactiveObject will cause reactiveObj[prop] to be reactive
// @ts-ignore
const prototype = reactiveObj['__proto__']
const otherObj = { data: ['a'] }
expect(isReactive(otherObj)).toBe(false)
const reactiveOther = reactive(otherObj)
expect(isReactive(reactiveOther)).toBe(true)
expect(reactiveOther.data[0]).toBe('a')
})

test('nested reactives', () => {
const original = {
nested: {
Expand Down
17 changes: 9 additions & 8 deletions packages/reactivity/src/reactive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject, toRawType, def } from '@vue/shared'
import { isObject, toRawType, def, hasOwn } from '@vue/shared'
import {
mutableHandlers,
readonlyHandlers,
Expand Down Expand Up @@ -116,18 +116,19 @@ function createReactiveObject(
return target
}
// target already has corresponding Proxy
let observed = isReadonly ? target.__v_readonly : target.__v_reactive
if (observed !== void 0) {
return observed
if (
hasOwn(target, isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive)
) {
return isReadonly ? target.__v_readonly : target.__v_reactive
}
// only a whitelist of value types can be observed.
if (!canObserve(target)) {
return target
}
const handlers = collectionTypes.has(target.constructor)
? collectionHandlers
: baseHandlers
observed = new Proxy(target, handlers)
const observed = new Proxy(
target,
collectionTypes.has(target.constructor) ? collectionHandlers : baseHandlers
)
def(
target,
isReadonly ? ReactiveFlags.readonly : ReactiveFlags.reactive,
Expand Down