11import { AnyObject } from '../types/basic'
22import { getVueConstructor } from '../runtimeContext'
3- import { isPlainObject , def , hasOwn , warn , isObject } from '../utils'
3+ import { isPlainObject , def , warn } from '../utils'
44import { isComponentInstance , defineComponentInstance } from '../utils/helper'
5- import {
6- AccessControlIdentifierKey ,
7- ReactiveIdentifierKey ,
8- RawIdentifierKey ,
9- ReadonlyIdentifierKey ,
10- RefKey ,
11- } from '../utils/symbols'
5+ import { RefKey } from '../utils/symbols'
126import { isRef , UnwrapRef } from './ref'
13-
14- const AccessControlIdentifier = { }
15- const ReactiveIdentifier = { }
16- const RawIdentifier = { }
7+ import { rawSet , readonlySet , reactiveSet } from '../utils/sets'
178
189export function isRaw ( obj : any ) : boolean {
19- return (
20- hasOwn ( obj , RawIdentifierKey ) && obj [ RawIdentifierKey ] === RawIdentifier
21- )
10+ return rawSet . has ( obj )
2211}
2312
2413export function isReadonly ( obj : any ) : boolean {
25- return hasOwn ( obj , ReadonlyIdentifierKey ) && obj [ ReadonlyIdentifierKey ]
14+ return readonlySet . has ( obj )
2615}
2716
2817export function isReactive ( obj : any ) : boolean {
29- return (
30- isObject ( obj ) &&
31- Object . isExtensible ( obj ) &&
32- hasOwn ( obj , ReactiveIdentifierKey ) &&
33- obj [ ReactiveIdentifierKey ] === ReactiveIdentifier
34- )
18+ return reactiveSet . has ( obj )
3519}
3620
3721/**
@@ -45,20 +29,9 @@ function setupAccessControl(target: AnyObject): void {
4529 Array . isArray ( target ) ||
4630 isRef ( target ) ||
4731 isComponentInstance ( target )
48- ) {
49- return
50- }
51-
52- if (
53- hasOwn ( target , AccessControlIdentifierKey ) &&
54- target [ AccessControlIdentifierKey ] === AccessControlIdentifier
55- ) {
32+ )
5633 return
57- }
5834
59- if ( Object . isExtensible ( target ) ) {
60- def ( target , AccessControlIdentifierKey , AccessControlIdentifier )
61- }
6235 const keys = Object . keys ( target )
6336 for ( let i = 0 ; i < keys . length ; i ++ ) {
6437 defineAccessControl ( target , keys [ i ] )
@@ -203,29 +176,32 @@ export function shallowReactive<T extends object = any>(obj: T): T {
203176
204177export function markReactive ( target : any , shallow = false ) {
205178 if (
206- ! isPlainObject ( target ) ||
179+ ! ( isPlainObject ( target ) || Array . isArray ( target ) ) ||
180+ // !isPlainObject(target) ||
207181 isRaw ( target ) ||
208- Array . isArray ( target ) ||
182+ // Array.isArray(target) ||
209183 isRef ( target ) ||
210184 isComponentInstance ( target )
211185 ) {
212186 return
213187 }
214188
215- if (
216- hasOwn ( target , ReactiveIdentifierKey ) &&
217- target [ ReactiveIdentifierKey ] === ReactiveIdentifier
218- ) {
189+ if ( isReactive ( target ) || ! Object . isExtensible ( target ) ) {
219190 return
220191 }
221192
222- if ( Object . isExtensible ( target ) ) {
223- def ( target , ReactiveIdentifierKey , ReactiveIdentifier )
224- }
193+ reactiveSet . add ( target )
225194
226195 if ( shallow ) {
227196 return
228197 }
198+
199+ if ( Array . isArray ( target ) ) {
200+ // TODO way to track new array items
201+ target . forEach ( ( x ) => markReactive ( x ) )
202+ return
203+ }
204+
229205 const keys = Object . keys ( target )
230206 for ( let i = 0 ; i < keys . length ; i ++ ) {
231207 markReactive ( target [ keys [ i ] ] )
@@ -264,9 +240,7 @@ export function shallowReadonly<T extends object>(obj: T): Readonly<T> {
264240 return obj // just typing
265241 }
266242
267- const readonlyObj = {
268- [ ReadonlyIdentifierKey ] : true ,
269- }
243+ const readonlyObj = { }
270244
271245 const source = reactive ( { } )
272246 const ob = ( source as any ) . __ob__
@@ -306,6 +280,8 @@ export function shallowReadonly<T extends object>(obj: T): Readonly<T> {
306280 } )
307281 }
308282
283+ readonlySet . add ( readonlyObj )
284+
309285 return readonlyObj as any
310286}
311287
@@ -320,7 +296,7 @@ export function markRaw<T extends object>(obj: T): T {
320296 // set the vue observable flag at obj
321297 def ( obj , '__ob__' , ( observe ( { } ) as any ) . __ob__ )
322298 // mark as Raw
323- def ( obj , RawIdentifierKey , RawIdentifier )
299+ rawSet . add ( obj )
324300
325301 return obj
326302}
0 commit comments