Skip to content

Commit 7582a83

Browse files
Thorsten LuenborgLinusBorg
authored andcommitted
fix(reactivity): ensure computed wrapped in readonly still works
close vuejs#3376
1 parent 6cb9475 commit 7582a83

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/reactivity/__tests__/readonly.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
effect,
99
ref,
1010
shallowReadonly,
11-
isProxy
11+
isProxy,
12+
computed
1213
} from '../src'
1314

1415
/**
@@ -435,6 +436,21 @@ describe('reactivity/readonly', () => {
435436
).toHaveBeenWarned()
436437
})
437438

439+
// https://github.com/vuejs/vue-next/issues/3376
440+
test('calling readonly on computed should allow computed to set its private properties', () => {
441+
const r = ref<boolean>(false)
442+
const c = computed(() => r.value)
443+
const rC = readonly(c)
444+
445+
r.value = true
446+
447+
expect(rC.value).toBe(true)
448+
;(rC as any).randomProperty = true
449+
450+
expect(
451+
'Set operation on key "randomProperty" failed: target is readonly.'
452+
).toHaveBeenWarned()
453+
})
438454
describe('shallowReadonly', () => {
439455
test('should not make non-reactive properties reactive', () => {
440456
const props = shallowReadonly({ n: { foo: 1 } })

packages/reactivity/src/baseHandlers.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@ export const mutableHandlers: ProxyHandler<object> = {
197197

198198
export const readonlyHandlers: ProxyHandler<object> = {
199199
get: readonlyGet,
200-
set(target, key) {
200+
set(target, key, value, receiver) {
201+
// is computed()
202+
if ((target as any).__v_isRef && (target as any).effect) {
203+
// computed should be able to set its own private properties
204+
if (key === '_dirty' || key === '_value') {
205+
return set(target, key, value, receiver)
206+
}
207+
}
201208
if (__DEV__) {
202209
console.warn(
203210
`Set operation on key "${String(key)}" failed: target is readonly.`,

0 commit comments

Comments
 (0)