Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 11eebcb

Browse files
committed
refactor(reactivity): use class for Link
1 parent 7fe6c79 commit 11eebcb

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed

packages/reactivity/src/computed.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import {
33
type DebuggerEvent,
44
type DebuggerOptions,
55
EffectFlags,
6-
type Link,
76
type Subscriber,
87
activeSub,
98
refreshComputed,
109
} from './effect'
1110
import type { Ref } from './ref'
1211
import { warn } from './warning'
13-
import { Dep, globalVersion } from './dep'
12+
import { Dep, type Link, globalVersion } from './dep'
1413
import { ReactiveFlags, TrackOpTypes } from './constants'
1514

1615
declare const ComputedRefSymbol: unique symbol

packages/reactivity/src/dep.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { type TrackOpTypes, TriggerOpTypes } from './constants'
44
import {
55
type DebuggerEventExtraInfo,
66
EffectFlags,
7-
type Link,
7+
type Subscriber,
88
activeSub,
99
endBatch,
1010
shouldTrack,
@@ -18,6 +18,49 @@ import {
1818
*/
1919
export let globalVersion = 0
2020

21+
/**
22+
* Represents a link between a source (Dep) and a subscriber (Effect or Computed).
23+
* Deps and subs have a many-to-many relationship - each link between a
24+
* dep and a sub is represented by a Link instance.
25+
*
26+
* A Link is also a node in two doubly-linked lists - one for the associated
27+
* sub to track all its deps, and one for the associated dep to track all its
28+
* subs.
29+
*
30+
* @internal
31+
*/
32+
export class Link {
33+
/**
34+
* - Before each effect run, all previous dep links' version are reset to -1
35+
* - During the run, a link's version is synced with the source dep on access
36+
* - After the run, links with version -1 (that were never used) are cleaned
37+
* up
38+
*/
39+
version: number
40+
41+
/**
42+
* Pointers for doubly-linked lists
43+
*/
44+
nextDep?: Link
45+
prevDep?: Link
46+
nextSub?: Link
47+
prevSub?: Link
48+
prevActiveLink?: Link
49+
50+
constructor(
51+
public sub: Subscriber,
52+
public dep: Dep,
53+
) {
54+
this.version = dep.version
55+
this.nextDep =
56+
this.prevDep =
57+
this.nextSub =
58+
this.prevSub =
59+
this.prevActiveLink =
60+
undefined
61+
}
62+
}
63+
2164
/**
2265
* @internal
2366
*/
@@ -52,16 +95,7 @@ export class Dep {
5295

5396
let link = this.activeLink
5497
if (link === undefined || link.sub !== activeSub) {
55-
link = this.activeLink = {
56-
dep: this,
57-
sub: activeSub,
58-
version: this.version,
59-
nextDep: undefined,
60-
prevDep: undefined,
61-
nextSub: undefined,
62-
prevSub: undefined,
63-
prevActiveLink: undefined,
64-
}
98+
link = this.activeLink = new Link(activeSub, this)
6599

66100
// add the link to the activeEffect as a dep (as tail)
67101
if (!activeSub.deps) {

packages/reactivity/src/effect.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { extend, hasChanged } from '@vue/shared'
22
import type { ComputedRefImpl } from './computed'
33
import type { TrackOpTypes, TriggerOpTypes } from './constants'
4-
import { type Dep, globalVersion } from './dep'
4+
import { type Link, globalVersion } from './dep'
55
import { activeEffectScope } from './effectScope'
66
import { warn } from './warning'
77

@@ -72,41 +72,6 @@ export interface Subscriber extends DebuggerOptions {
7272
notify(): void
7373
}
7474

75-
/**
76-
* Represents a link between a source (Dep) and a subscriber (Effect or Computed).
77-
* Deps and subs have a many-to-many relationship - each link between a
78-
* dep and a sub is represented by a Link instance.
79-
*
80-
* A Link is also a node in two doubly-linked lists - one for the associated
81-
* sub to track all its deps, and one for the associated dep to track all its
82-
* subs.
83-
*
84-
* @internal
85-
*/
86-
export interface Link {
87-
dep: Dep
88-
sub: Subscriber
89-
90-
/**
91-
* - Before each effect run, all previous dep links' version are reset to -1
92-
* - During the run, a link's version is synced with the source dep on access
93-
* - After the run, links with version -1 (that were never used) are cleaned
94-
* up
95-
*/
96-
version: number
97-
98-
/**
99-
* Pointers for doubly-linked lists
100-
*/
101-
nextDep?: Link
102-
prevDep?: Link
103-
104-
nextSub?: Link
105-
prevSub?: Link
106-
107-
prevActiveLink?: Link
108-
}
109-
11075
const pausedQueueEffects = new WeakSet<ReactiveEffect>()
11176

11277
export class ReactiveEffect<T = any>

0 commit comments

Comments
 (0)