Skip to content

Commit 593ecf5

Browse files
refactor(reactivity): sync alien-signals 3.0.0 changes (#14057)
1 parent e4b147a commit 593ecf5

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

packages/reactivity/src/system.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable */
2-
// Ported from https://github.com/stackblitz/alien-signals/blob/v2.0.4/src/system.ts
2+
// Ported from https://github.com/stackblitz/alien-signals/blob/v3.0.0/src/system.ts
33
import type { ComputedRefImpl as Computed } from './computed.js'
44
import type { ReactiveEffect as Effect } from './effect.js'
55
import type { EffectScope } from './effectScope.js'
@@ -14,6 +14,7 @@ export interface ReactiveNode {
1414
}
1515

1616
export interface Link {
17+
version: number
1718
dep: ReactiveNode | Computed | Effect | EffectScope
1819
sub: ReactiveNode | Computed | Effect | EffectScope
1920
prevSub: Link | undefined
@@ -42,6 +43,7 @@ const notifyBuffer: (Effect | undefined)[] = []
4243
export let batchDepth = 0
4344
export let activeSub: ReactiveNode | undefined = undefined
4445

46+
let globalVersion = 0
4547
let notifyIndex = 0
4648
let notifyBufferLength = 0
4749

@@ -68,21 +70,25 @@ export function link(dep: ReactiveNode, sub: ReactiveNode): void {
6870
if (prevDep !== undefined && prevDep.dep === dep) {
6971
return
7072
}
71-
let nextDep: Link | undefined = undefined
72-
const recursedCheck = sub.flags & ReactiveFlags.RecursedCheck
73-
if (recursedCheck) {
74-
nextDep = prevDep !== undefined ? prevDep.nextDep : sub.deps
75-
if (nextDep !== undefined && nextDep.dep === dep) {
76-
sub.depsTail = nextDep
77-
return
78-
}
73+
const nextDep = prevDep !== undefined ? prevDep.nextDep : sub.deps
74+
if (nextDep !== undefined && nextDep.dep === dep) {
75+
nextDep.version = globalVersion
76+
sub.depsTail = nextDep
77+
return
7978
}
80-
// TODO: maybe can find a good way to check duplicate link
8179
const prevSub = dep.subsTail
80+
if (
81+
prevSub !== undefined &&
82+
prevSub.version === globalVersion &&
83+
prevSub.sub === sub
84+
) {
85+
return
86+
}
8287
const newLink =
8388
(sub.depsTail =
8489
dep.subsTail =
8590
{
91+
version: globalVersion,
8692
dep,
8793
sub,
8894
prevDep,
@@ -149,7 +155,6 @@ export function propagate(link: Link): void {
149155

150156
top: do {
151157
const sub = link.sub
152-
153158
let flags = sub.flags
154159

155160
if (flags & (ReactiveFlags.Mutable | ReactiveFlags.Watching)) {
@@ -215,6 +220,7 @@ export function propagate(link: Link): void {
215220
}
216221

217222
export function startTracking(sub: ReactiveNode): ReactiveNode | undefined {
223+
++globalVersion
218224
sub.depsTail = undefined
219225
sub.flags =
220226
(sub.flags &
@@ -343,18 +349,12 @@ export function shallowPropagate(link: Link): void {
343349
}
344350

345351
function isValidLink(checkLink: Link, sub: ReactiveNode): boolean {
346-
const depsTail = sub.depsTail
347-
if (depsTail !== undefined) {
348-
let link = sub.deps!
349-
do {
350-
if (link === checkLink) {
351-
return true
352-
}
353-
if (link === depsTail) {
354-
break
355-
}
356-
link = link.nextDep!
357-
} while (link !== undefined)
352+
let link = sub.depsTail
353+
while (link !== undefined) {
354+
if (link === checkLink) {
355+
return true
356+
}
357+
link = link.prevDep
358358
}
359359
return false
360360
}

0 commit comments

Comments
 (0)