Skip to content

Commit 8c9a189

Browse files
committed
refactor: inline getLink, releaseLink logic
1 parent c988257 commit 8c9a189

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

src/system.ts

+44-49
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,7 @@ export function endBatch(): void {
6969
}
7070
}
7171

72-
let pool: Link | undefined = undefined;
73-
74-
export function getLink(dep: Dependency, sub: Subscriber, nextDep: Link | undefined): Link {
75-
if (pool !== undefined) {
76-
const newLink = pool;
77-
pool = newLink.nextDep;
78-
newLink.nextDep = nextDep;
79-
newLink.dep = dep;
80-
newLink.sub = sub;
81-
newLink.trackId = sub.trackId;
82-
return newLink;
83-
}
84-
return {
85-
dep,
86-
sub,
87-
trackId: sub.trackId,
88-
nextDep: nextDep,
89-
prevSub: undefined,
90-
nextSub: undefined,
91-
};
92-
}
93-
94-
export function releaseLink(link: Link): void {
95-
const nextSub = link.nextSub;
96-
const prevSub = link.prevSub;
97-
98-
if (nextSub !== undefined) {
99-
nextSub.prevSub = prevSub;
100-
} else {
101-
link.dep.subsTail = prevSub;
102-
}
103-
104-
if (prevSub !== undefined) {
105-
prevSub.nextSub = nextSub;
106-
} else {
107-
link.dep.subs = nextSub;
108-
}
109-
110-
// @ts-expect-error
111-
link.dep = undefined;
112-
// @ts-expect-error
113-
link.sub = undefined;
114-
link.prevSub = undefined;
115-
link.nextSub = undefined;
116-
link.nextDep = pool;
117-
pool = link;
118-
}
72+
let linkPool: Link | undefined = undefined;
11973

12074
export function link(dep: Dependency, sub: Subscriber): void {
12175
const depsTail = sub.depsTail;
@@ -124,7 +78,25 @@ export function link(dep: Dependency, sub: Subscriber): void {
12478
: sub.deps;
12579

12680
if (old === undefined || old.dep !== dep) {
127-
const newLink = getLink(dep, sub, old);
81+
let newLink: Link;
82+
83+
if (linkPool !== undefined) {
84+
newLink = linkPool;
85+
linkPool = newLink.nextDep;
86+
newLink.nextDep = old;
87+
newLink.dep = dep;
88+
newLink.sub = sub;
89+
newLink.trackId = sub.trackId;
90+
} else {
91+
newLink = {
92+
dep,
93+
sub,
94+
trackId: sub.trackId,
95+
nextDep: old,
96+
prevSub: undefined,
97+
nextSub: undefined,
98+
};
99+
}
128100

129101
if (depsTail === undefined) {
130102
sub.deps = newLink;
@@ -378,7 +350,30 @@ export function clearTrack(link: Link): void {
378350
do {
379351
const dep = link.dep;
380352
const nextDep = link.nextDep;
381-
releaseLink(link);
353+
const nextSub = link.nextSub;
354+
const prevSub = link.prevSub;
355+
356+
if (nextSub !== undefined) {
357+
nextSub.prevSub = prevSub;
358+
} else {
359+
link.dep.subsTail = prevSub;
360+
}
361+
362+
if (prevSub !== undefined) {
363+
prevSub.nextSub = nextSub;
364+
} else {
365+
link.dep.subs = nextSub;
366+
}
367+
368+
// @ts-expect-error
369+
link.dep = undefined;
370+
// @ts-expect-error
371+
link.sub = undefined;
372+
link.prevSub = undefined;
373+
link.nextSub = undefined;
374+
link.nextDep = linkPool;
375+
linkPool = link;
376+
382377
if (dep.subs === undefined && 'deps' in dep) {
383378
if ('notify' in dep) {
384379
dep.dirtyLevel = DirtyLevels.None;

0 commit comments

Comments
 (0)