1
1
import { AnyObject } from '../types/basic'
2
2
import { getVueConstructor } from '../runtimeContext'
3
- import { isPlainObject , def , hasOwn , warn , isObject } from '../utils'
3
+ import { isPlainObject , def , warn } from '../utils'
4
4
import { 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'
12
6
import { isRef , UnwrapRef } from './ref'
13
-
14
- const AccessControlIdentifier = { }
15
- const ReactiveIdentifier = { }
16
- const RawIdentifier = { }
7
+ import { rawSet , readonlySet , reactiveSet } from '../utils/sets'
17
8
18
9
export function isRaw ( obj : any ) : boolean {
19
- return (
20
- hasOwn ( obj , RawIdentifierKey ) && obj [ RawIdentifierKey ] === RawIdentifier
21
- )
10
+ return rawSet . has ( obj )
22
11
}
23
12
24
13
export function isReadonly ( obj : any ) : boolean {
25
- return hasOwn ( obj , ReadonlyIdentifierKey ) && obj [ ReadonlyIdentifierKey ]
14
+ return readonlySet . has ( obj )
26
15
}
27
16
28
17
export 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 )
35
19
}
36
20
37
21
/**
@@ -45,20 +29,9 @@ function setupAccessControl(target: AnyObject): void {
45
29
Array . isArray ( target ) ||
46
30
isRef ( target ) ||
47
31
isComponentInstance ( target )
48
- ) {
49
- return
50
- }
51
-
52
- if (
53
- hasOwn ( target , AccessControlIdentifierKey ) &&
54
- target [ AccessControlIdentifierKey ] === AccessControlIdentifier
55
- ) {
32
+ )
56
33
return
57
- }
58
34
59
- if ( Object . isExtensible ( target ) ) {
60
- def ( target , AccessControlIdentifierKey , AccessControlIdentifier )
61
- }
62
35
const keys = Object . keys ( target )
63
36
for ( let i = 0 ; i < keys . length ; i ++ ) {
64
37
defineAccessControl ( target , keys [ i ] )
@@ -203,29 +176,32 @@ export function shallowReactive<T extends object = any>(obj: T): T {
203
176
204
177
export function markReactive ( target : any , shallow = false ) {
205
178
if (
206
- ! isPlainObject ( target ) ||
179
+ ! ( isPlainObject ( target ) || Array . isArray ( target ) ) ||
180
+ // !isPlainObject(target) ||
207
181
isRaw ( target ) ||
208
- Array . isArray ( target ) ||
182
+ // Array.isArray(target) ||
209
183
isRef ( target ) ||
210
184
isComponentInstance ( target )
211
185
) {
212
186
return
213
187
}
214
188
215
- if (
216
- hasOwn ( target , ReactiveIdentifierKey ) &&
217
- target [ ReactiveIdentifierKey ] === ReactiveIdentifier
218
- ) {
189
+ if ( isReactive ( target ) || ! Object . isExtensible ( target ) ) {
219
190
return
220
191
}
221
192
222
- if ( Object . isExtensible ( target ) ) {
223
- def ( target , ReactiveIdentifierKey , ReactiveIdentifier )
224
- }
193
+ reactiveSet . add ( target )
225
194
226
195
if ( shallow ) {
227
196
return
228
197
}
198
+
199
+ if ( Array . isArray ( target ) ) {
200
+ // TODO way to track new array items
201
+ target . forEach ( ( x ) => markReactive ( x ) )
202
+ return
203
+ }
204
+
229
205
const keys = Object . keys ( target )
230
206
for ( let i = 0 ; i < keys . length ; i ++ ) {
231
207
markReactive ( target [ keys [ i ] ] )
@@ -264,9 +240,7 @@ export function shallowReadonly<T extends object>(obj: T): Readonly<T> {
264
240
return obj // just typing
265
241
}
266
242
267
- const readonlyObj = {
268
- [ ReadonlyIdentifierKey ] : true ,
269
- }
243
+ const readonlyObj = { }
270
244
271
245
const source = reactive ( { } )
272
246
const ob = ( source as any ) . __ob__
@@ -306,6 +280,8 @@ export function shallowReadonly<T extends object>(obj: T): Readonly<T> {
306
280
} )
307
281
}
308
282
283
+ readonlySet . add ( readonlyObj )
284
+
309
285
return readonlyObj as any
310
286
}
311
287
@@ -320,7 +296,7 @@ export function markRaw<T extends object>(obj: T): T {
320
296
// set the vue observable flag at obj
321
297
def ( obj , '__ob__' , ( observe ( { } ) as any ) . __ob__ )
322
298
// mark as Raw
323
- def ( obj , RawIdentifierKey , RawIdentifier )
299
+ rawSet . add ( obj )
324
300
325
301
return obj
326
302
}
0 commit comments