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: not unwrapping markRaw objects #386

Merged
merged 2 commits into from Jun 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/reactivity/unwrap.ts
@@ -1,6 +1,6 @@
import { isRef } from './ref'
import { proxy, isFunction, isPlainObject, isArray } from '../utils'
import { isReactive } from './reactive'
import { isReactive, isRaw } from './reactive'

export function unwrapRefProxy(value: any, map = new WeakMap()) {
if (map.has(value)) {
Expand All @@ -13,7 +13,8 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {
isReactive(value) ||
!isPlainObject(value) ||
!Object.isExtensible(value) ||
isRef(value)
isRef(value) ||
isRaw(value)
) {
return value
}
Expand All @@ -28,8 +29,12 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {

for (const k of Object.keys(value)) {
const r = value[k]
// don't unwrap or do anything to raw
if (isRaw(r)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I found that when r is null or undefined, it causes TypeError: Cannot convert undefined or null to object.

obj[k] = r
}
// if is a ref, create a proxy to retrieve the value,
if (isRef(r)) {
else if (isRef(r)) {
const set = (v: any) => (r.value = v)
const get = () => r.value

Expand Down
25 changes: 25 additions & 0 deletions test/setup.spec.js
Expand Up @@ -7,6 +7,7 @@ const {
inject,
reactive,
toRefs,
markRaw,
} = require('../src')

describe('setup', () => {
Expand Down Expand Up @@ -622,6 +623,30 @@ describe('setup', () => {
vm.$el.querySelector('#recursive_b_recursive_recursive_r').textContent
).toBe('r')
})

// #384
it('not unwrap when is raw', () => {
const vm = new Vue({
setup() {
const xx = {
ref: ref('r'),
}
const r = markRaw(xx)
return {
r,
}
},
template: `<div>
<p id="r">{{r}}</p>
</div>`,
}).$mount()

expect(JSON.parse(vm.$el.querySelector('#r').textContent)).toMatchObject({
ref: {
value: 'r',
},
})
})
})

it('should not unwrap built-in objects on the template', () => {
Expand Down