Skip to content

Commit 575d100

Browse files
authoredJun 19, 2020
fix: not unwrapping markRaw objects (#386)
* fix: not unwrapping `markRaw` objects * chore: add falsy check
1 parent ce932bf commit 575d100

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
 

‎src/reactivity/unwrap.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isRef } from './ref'
22
import { proxy, isFunction, isPlainObject, isArray } from '../utils'
3-
import { isReactive } from './reactive'
3+
import { isReactive, isRaw } from './reactive'
44

55
export function unwrapRefProxy(value: any, map = new WeakMap()) {
66
if (map.has(value)) {
@@ -13,7 +13,8 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {
1313
isReactive(value) ||
1414
!isPlainObject(value) ||
1515
!Object.isExtensible(value) ||
16-
isRef(value)
16+
isRef(value) ||
17+
isRaw(value)
1718
) {
1819
return value
1920
}
@@ -28,8 +29,12 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {
2829

2930
for (const k of Object.keys(value)) {
3031
const r = value[k]
32+
// don't process on falsy or raw
33+
if (!r || isRaw(r)) {
34+
obj[k] = r
35+
}
3136
// if is a ref, create a proxy to retrieve the value,
32-
if (isRef(r)) {
37+
else if (isRef(r)) {
3338
const set = (v: any) => (r.value = v)
3439
const get = () => r.value
3540

‎test/setup.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
inject,
88
reactive,
99
toRefs,
10+
markRaw,
1011
} = require('../src')
1112

1213
describe('setup', () => {
@@ -622,6 +623,30 @@ describe('setup', () => {
622623
vm.$el.querySelector('#recursive_b_recursive_recursive_r').textContent
623624
).toBe('r')
624625
})
626+
627+
// #384
628+
it('not unwrap when is raw', () => {
629+
const vm = new Vue({
630+
setup() {
631+
const xx = {
632+
ref: ref('r'),
633+
}
634+
const r = markRaw(xx)
635+
return {
636+
r,
637+
}
638+
},
639+
template: `<div>
640+
<p id="r">{{r}}</p>
641+
</div>`,
642+
}).$mount()
643+
644+
expect(JSON.parse(vm.$el.querySelector('#r').textContent)).toMatchObject({
645+
ref: {
646+
value: 'r',
647+
},
648+
})
649+
})
625650
})
626651

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

0 commit comments

Comments
 (0)
Please sign in to comment.