File tree Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Expand file tree Collapse file tree 2 files changed +36
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { reactive , Ref , UnwrapRef } from '.'
2
2
import { isArray , isPlainObject , warn } from '../utils'
3
3
import { readonlySet } from '../utils/sets'
4
+ import { isRef } from './ref'
4
5
5
6
export function isReadonly ( obj : any ) : boolean {
6
7
return readonlySet . has ( obj )
@@ -47,7 +48,10 @@ export function readonly<T extends object>(
47
48
48
49
export function shallowReadonly < T extends object > ( obj : T ) : Readonly < T >
49
50
export function shallowReadonly ( obj : any ) : any {
50
- if ( ! ( isPlainObject ( obj ) || isArray ( obj ) ) || ! Object . isExtensible ( obj ) ) {
51
+ if (
52
+ ! ( isPlainObject ( obj ) || isArray ( obj ) ) ||
53
+ ( ! Object . isExtensible ( obj ) && ! isRef ( obj ) )
54
+ ) {
51
55
return obj
52
56
}
53
57
@@ -60,7 +64,7 @@ export function shallowReadonly(obj: any): any {
60
64
let val = obj [ key ]
61
65
let getter : ( ( ) => any ) | undefined
62
66
const property = Object . getOwnPropertyDescriptor ( obj , key )
63
- if ( property ) {
67
+ if ( property && ! isRef ( obj ) ) {
64
68
if ( property . configurable === false ) {
65
69
continue
66
70
}
Original file line number Diff line number Diff line change 1
1
import { mockWarn } from '../../helpers/mockWarn'
2
- import { shallowReadonly , isReactive } from '../../../src'
2
+ import { shallowReadonly , isReactive , ref , reactive } from '../../../src'
3
+
4
+ const Vue = require ( 'vue/dist/vue.common.js' )
3
5
4
6
// /**
5
7
// * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html
@@ -372,5 +374,32 @@ describe('reactivity/readonly', () => {
372
374
`Set operation on key "foo" failed: target is readonly.`
373
375
) . not . toHaveBeenWarned ( )
374
376
} )
377
+
378
+ // #669
379
+ test ( 'shallowReadonly should work for refs' , ( ) => {
380
+ const vm = new Vue ( {
381
+ template : '<div>{{ count }} {{ countRef }}</div>' ,
382
+ setup ( ) {
383
+ const count = reactive ( { number : 0 } )
384
+ const countRef = ref ( 0 )
385
+
386
+ const countReadonly = shallowReadonly ( count )
387
+ const countRefReadonly = shallowReadonly ( countRef )
388
+
389
+ setTimeout ( ( ) => {
390
+ // @ts -expect-error
391
+ countReadonly . number ++
392
+ // @ts -expect-error
393
+ countRefReadonly . value ++
394
+ } , 2000 )
395
+ return {
396
+ count,
397
+ countRef,
398
+ }
399
+ } ,
400
+ } ) . $mount ( )
401
+
402
+ expect ( vm . $el . textContent ) . toBe ( `{\n "number": 0\n} 0` )
403
+ } )
375
404
} )
376
405
} )
You can’t perform that action at this time.
0 commit comments