1
+ import { extend } from '@vue/shared'
2
+ import type { ComputedRefImpl } from './computed'
1
3
import { DirtyLevels , TrackOpTypes , TriggerOpTypes } from './constants'
2
- import { extend , isArray , isIntegerKey , isMap } from '@vue/shared '
4
+ import type { Dep } from './dep '
3
5
import { EffectScope , recordEffectScope } from './effectScope'
4
- import { createDep , Dep } from './dep'
5
- import type { ComputedRefImpl } from './computed'
6
-
7
- // The main WeakMap that stores {target -> key -> dep} connections.
8
- // Conceptually, it's easier to think of a dependency as a Dep class
9
- // which maintains a Set of subscribers, but we simply store them as
10
- // raw Sets to reduce memory overhead.
11
- type KeyToDepMap = Map < any , Dep >
12
- const targetMap = new WeakMap < object , KeyToDepMap > ( )
13
6
14
7
export type EffectScheduler = (
15
8
onScheduled : ( cb : ( ) => void ) => void ,
@@ -31,9 +24,6 @@ export type DebuggerEventExtraInfo = {
31
24
32
25
export let activeEffect : ReactiveEffect | undefined
33
26
34
- export const ITERATE_KEY = Symbol ( __DEV__ ? 'iterate' : '' )
35
- export const MAP_KEY_ITERATE_KEY = Symbol ( __DEV__ ? 'Map key iterate' : '' )
36
-
37
27
export class ReactiveEffect < T = any > {
38
28
active = true
39
29
deps : Dep [ ] = [ ]
@@ -260,38 +250,6 @@ export function resetScheduling() {
260
250
}
261
251
}
262
252
263
- /**
264
- * Tracks access to a reactive property.
265
- *
266
- * This will check which effect is running at the moment and record it as dep
267
- * which records all effects that depend on the reactive property.
268
- *
269
- * @param target - Object holding the reactive property.
270
- * @param type - Defines the type of access to the reactive property.
271
- * @param key - Identifier of the reactive property to track.
272
- */
273
- export function track ( target : object , type : TrackOpTypes , key : unknown ) {
274
- if ( shouldTrack && activeEffect ) {
275
- let depsMap = targetMap . get ( target )
276
- if ( ! depsMap ) {
277
- targetMap . set ( target , ( depsMap = new Map ( ) ) )
278
- }
279
- let dep = depsMap . get ( key )
280
- if ( ! dep ) {
281
- depsMap . set ( key , ( dep = createDep ( ( ) => depsMap ! . delete ( key ) ) ) )
282
- }
283
- if ( __DEV__ ) {
284
- trackEffect ( activeEffect , dep , {
285
- target,
286
- type,
287
- key
288
- } )
289
- } else {
290
- trackEffect ( activeEffect , dep )
291
- }
292
- }
293
- }
294
-
295
253
export function trackEffect (
296
254
effect : ReactiveEffect ,
297
255
dep : Dep ,
@@ -314,95 +272,6 @@ export function trackEffect(
314
272
}
315
273
}
316
274
317
- /**
318
- * Finds all deps associated with the target (or a specific property) and
319
- * triggers the effects stored within.
320
- *
321
- * @param target - The reactive object.
322
- * @param type - Defines the type of the operation that needs to trigger effects.
323
- * @param key - Can be used to target a specific reactive property in the target object.
324
- */
325
- export function trigger (
326
- target : object ,
327
- type : TriggerOpTypes ,
328
- key ?: unknown ,
329
- newValue ?: unknown ,
330
- oldValue ?: unknown ,
331
- oldTarget ?: Map < unknown , unknown > | Set < unknown >
332
- ) {
333
- const depsMap = targetMap . get ( target )
334
- if ( ! depsMap ) {
335
- // never been tracked
336
- return
337
- }
338
-
339
- let deps : ( Dep | undefined ) [ ] = [ ]
340
- if ( type === TriggerOpTypes . CLEAR ) {
341
- // collection being cleared
342
- // trigger all effects for target
343
- deps = [ ...depsMap . values ( ) ]
344
- } else if ( key === 'length' && isArray ( target ) ) {
345
- const newLength = Number ( newValue )
346
- depsMap . forEach ( ( dep , key ) => {
347
- if ( key === 'length' || key >= newLength ) {
348
- deps . push ( dep )
349
- }
350
- } )
351
- } else {
352
- // schedule runs for SET | ADD | DELETE
353
- if ( key !== void 0 ) {
354
- deps . push ( depsMap . get ( key ) )
355
- }
356
-
357
- // also run for iteration key on ADD | DELETE | Map.SET
358
- switch ( type ) {
359
- case TriggerOpTypes . ADD :
360
- if ( ! isArray ( target ) ) {
361
- deps . push ( depsMap . get ( ITERATE_KEY ) )
362
- if ( isMap ( target ) ) {
363
- deps . push ( depsMap . get ( MAP_KEY_ITERATE_KEY ) )
364
- }
365
- } else if ( isIntegerKey ( key ) ) {
366
- // new index added to array -> length changes
367
- deps . push ( depsMap . get ( 'length' ) )
368
- }
369
- break
370
- case TriggerOpTypes . DELETE :
371
- if ( ! isArray ( target ) ) {
372
- deps . push ( depsMap . get ( ITERATE_KEY ) )
373
- if ( isMap ( target ) ) {
374
- deps . push ( depsMap . get ( MAP_KEY_ITERATE_KEY ) )
375
- }
376
- }
377
- break
378
- case TriggerOpTypes . SET :
379
- if ( isMap ( target ) ) {
380
- deps . push ( depsMap . get ( ITERATE_KEY ) )
381
- }
382
- break
383
- }
384
- }
385
-
386
- pauseScheduling ( )
387
- for ( const dep of deps ) {
388
- if ( dep ) {
389
- if ( __DEV__ ) {
390
- triggerEffects ( dep , DirtyLevels . Dirty , {
391
- target,
392
- type,
393
- key,
394
- newValue,
395
- oldValue,
396
- oldTarget
397
- } )
398
- } else {
399
- triggerEffects ( dep , DirtyLevels . Dirty )
400
- }
401
- }
402
- }
403
- resetScheduling ( )
404
- }
405
-
406
275
const queueEffectCbs : ( ( ) => void ) [ ] = [ ]
407
276
const pushEffectCb = queueEffectCbs . push . bind ( queueEffectCbs )
408
277
@@ -436,7 +305,3 @@ export function triggerEffects(
436
305
}
437
306
resetScheduling ( )
438
307
}
439
-
440
- export function getDepFromReactive ( object : any , key : string | number | symbol ) {
441
- return targetMap . get ( object ) ?. get ( key )
442
- }
0 commit comments