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(shallowReadonly) : shallowReadonly should work for ref. #704

Merged
merged 3 commits into from
May 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/reactivity/readonly.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { reactive, Ref, UnwrapRef } from '.'
import { isArray, isPlainObject, warn } from '../utils'
import { readonlySet } from '../utils/sets'
import { isRef } from './ref'

export function isReadonly(obj: any): boolean {
return readonlySet.has(obj)
Expand Down Expand Up @@ -47,7 +48,10 @@ export function readonly<T extends object>(

export function shallowReadonly<T extends object>(obj: T): Readonly<T>
export function shallowReadonly(obj: any): any {
if (!(isPlainObject(obj) || isArray(obj)) || !Object.isExtensible(obj)) {
if (
!(isPlainObject(obj) || isArray(obj)) ||
(!Object.isExtensible(obj) && !isRef(obj))
) {
return obj
}

Expand All @@ -60,7 +64,7 @@ export function shallowReadonly(obj: any): any {
let val = obj[key]
let getter: (() => any) | undefined
const property = Object.getOwnPropertyDescriptor(obj, key)
if (property) {
if (property && !isRef(obj)) {
if (property.configurable === false) {
continue
}
Expand Down
31 changes: 30 additions & 1 deletion test/v3/reactivity/readonly.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { mockWarn } from '../../helpers/mockWarn'
import { shallowReadonly, isReactive } from '../../../src'
import { shallowReadonly, isReactive, ref, reactive } from '../../../src'

const Vue = require('vue/dist/vue.common.js')

// /**
// * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html
Expand Down Expand Up @@ -372,5 +374,32 @@ describe('reactivity/readonly', () => {
`Set operation on key "foo" failed: target is readonly.`
).not.toHaveBeenWarned()
})

// #669
test('shallowReadonly should work for refs', () => {
const vm = new Vue({
template: '<div>{{ count }} {{ countRef }}</div>',
setup() {
const count = reactive({ number: 0 })
const countRef = ref(0)

const countReadonly = shallowReadonly(count)
const countRefReadonly = shallowReadonly(countRef)

setTimeout(() => {
// @ts-expect-error
countReadonly.number++
ygj6 marked this conversation as resolved.
Show resolved Hide resolved
// @ts-expect-error
countRefReadonly.value++
}, 2000)
return {
count,
countRef,
}
},
}).$mount()

expect(vm.$el.textContent).toBe(`{\n "number": 0\n} 0`)
})
})
})