fix(reactivity): fix markRaw error on already marked object (#11864)

close #11862
This commit is contained in:
Konv Suu 2024-09-10 15:40:43 +08:00 committed by GitHub
parent d13cd22eef
commit 67d6596d40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View File

@ -293,6 +293,13 @@ describe('reactivity/reactive', () => {
expect(() => markRaw(obj)).not.toThrowError()
})
test('markRaw should not redefine on an marked object', () => {
const obj = markRaw({ foo: 1 })
const raw = markRaw(obj)
expect(raw).toBe(obj)
expect(() => markRaw(obj)).not.toThrowError()
})
test('should not observe non-extensible objects', () => {
const obj = reactive({
foo: Object.preventExtensions({ a: 1 }),

View File

@ -1,4 +1,4 @@
import { def, isObject, toRawType } from '@vue/shared'
import { def, hasOwn, isObject, toRawType } from '@vue/shared'
import {
mutableHandlers,
readonlyHandlers,
@ -405,7 +405,7 @@ export type Raw<T> = T & { [RawSymbol]?: true }
* @see {@link https://vuejs.org/api/reactivity-advanced.html#markraw}
*/
export function markRaw<T extends object>(value: T): Raw<T> {
if (Object.isExtensible(value)) {
if (!hasOwn(value, ReactiveFlags.SKIP) && Object.isExtensible(value)) {
def(value, ReactiveFlags.SKIP, true)
}
return value