From 571ba051d6e279212627766f90a856146f14211e Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 14 Nov 2024 07:41:56 +0800 Subject: [PATCH] refactor: remove namespace usage --- packages/reactivity/src/computed.ts | 26 +- packages/reactivity/src/dep.ts | 18 +- packages/reactivity/src/effect.ts | 604 ++++++++++++------------- packages/reactivity/src/effectScope.ts | 9 +- packages/reactivity/src/ref.ts | 18 +- 5 files changed, 345 insertions(+), 330 deletions(-) diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index 8c9ad216eee..fd2dbbfad88 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -3,12 +3,17 @@ import { ReactiveFlags, TrackOpTypes } from './constants' import { onTrack, setupDirtyLevelHandler } from './debug' import type { DebuggerEvent, DebuggerOptions } from './effect' import { - Dependency, + type Dependency, DirtyLevels, type IComputed, type Link, - Subscriber, - System, + activeSub, + activeTrackId, + endTrack, + link, + propagate, + resolveMaybeDirty, + startTrack, } from './effect' import type { Ref } from './ref' import { warn } from './warning' @@ -85,7 +90,7 @@ export class ComputedRefImpl implements IComputed { get _dirty(): boolean { let dirtyLevel = this.dirtyLevel if (dirtyLevel === DirtyLevels.MaybeDirty) { - Subscriber.resolveMaybeDirty(this) + resolveMaybeDirty(this) dirtyLevel = this.dirtyLevel } return dirtyLevel >= DirtyLevels.Dirty @@ -123,21 +128,20 @@ export class ComputedRefImpl implements IComputed { if (this._dirty) { this.update() } - const activeTrackId = System.activeTrackId if (activeTrackId !== 0) { const subsTail = this.subsTail if (subsTail === undefined || subsTail.trackId !== activeTrackId) { if (__DEV__) { - onTrack(System.activeSub!, { + onTrack(activeSub!, { target: this, type: TrackOpTypes.GET, key: 'value', }) } - Dependency.link(this, System.activeSub!) + link(this, activeSub!) } } else if (activeEffectScope !== undefined) { - Dependency.link(this, activeEffectScope) + link(this, activeEffectScope) } return this._value! } @@ -151,19 +155,19 @@ export class ComputedRefImpl implements IComputed { } update(): void { - const prevSub = Subscriber.startTrack(this) + const prevSub = startTrack(this) const oldValue = this._value let newValue: T try { newValue = this.fn(oldValue) } finally { - Subscriber.endTrack(this, prevSub) + endTrack(this, prevSub) } if (hasChanged(oldValue, newValue)) { this._value = newValue const subs = this.subs if (subs !== undefined) { - Dependency.propagate(subs) + propagate(subs) } } } diff --git a/packages/reactivity/src/dep.ts b/packages/reactivity/src/dep.ts index 497163c1d1d..2b06fc2a8a9 100644 --- a/packages/reactivity/src/dep.ts +++ b/packages/reactivity/src/dep.ts @@ -1,7 +1,16 @@ import { isArray, isIntegerKey, isMap, isSymbol } from '@vue/shared' import { type TrackOpTypes, TriggerOpTypes } from './constants' import { onTrack, triggerEventInfos } from './debug' -import { Dependency, type Link, System, endBatch, startBatch } from './effect' +import { + type Dependency, + type Link, + activeSub, + activeTrackId, + endBatch, + link, + propagate, + startBatch, +} from './effect' class Dep implements Dependency { _subs: Link | undefined = undefined @@ -53,7 +62,6 @@ export const ARRAY_ITERATE_KEY: unique symbol = Symbol( * @param key - Identifier of the reactive property to track. */ export function track(target: object, type: TrackOpTypes, key: unknown): void { - const activeTrackId = System.activeTrackId if (activeTrackId > 0) { let depsMap = targetMap.get(target) if (!depsMap) { @@ -66,13 +74,13 @@ export function track(target: object, type: TrackOpTypes, key: unknown): void { const subsTail = dep.subsTail if (subsTail === undefined || subsTail.trackId !== activeTrackId) { if (__DEV__) { - onTrack(System.activeSub!, { + onTrack(activeSub!, { target, type, key, }) } - Dependency.link(dep, System.activeSub!) + link(dep, activeSub!) } } } @@ -111,7 +119,7 @@ export function trigger( oldTarget, }) } - Dependency.propagate(dep.subs) + propagate(dep.subs) if (__DEV__) { triggerEventInfos.pop() } diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index 0128df65348..599bbed7577 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -112,7 +112,7 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { runIfDirty(): void { let dirtyLevel = this.dirtyLevel if (dirtyLevel === DirtyLevels.MaybeDirty) { - Subscriber.resolveMaybeDirty(this) + resolveMaybeDirty(this) dirtyLevel = this.dirtyLevel } if (dirtyLevel >= DirtyLevels.Dirty) { @@ -128,18 +128,18 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { return this.fn() } cleanupEffect(this) - const prevSub = Subscriber.startTrack(this) + const prevSub = startTrack(this) try { return this.fn() } finally { - if (__DEV__ && System.activeSub !== this) { + if (__DEV__ && activeSub !== this) { warn( 'Active effect was not restored correctly - ' + 'this is likely a Vue internal bug.', ) } - Subscriber.endTrack(this, prevSub) + endTrack(this, prevSub) if (this.canPropagate && this.allowRecurse) { this.canPropagate = false this.notify() @@ -150,7 +150,7 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { stop(): void { if (this.active) { if (this.deps !== undefined) { - Subscriber.clearTrack(this.deps) + clearTrack(this.deps) this.deps = undefined this.depsTail = undefined } @@ -162,7 +162,7 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { get dirty(): boolean { if (this.dirtyLevel === DirtyLevels.MaybeDirty) { - Subscriber.resolveMaybeDirty(this) + resolveMaybeDirty(this) } return this.dirtyLevel === DirtyLevels.Dirty } @@ -205,22 +205,21 @@ export function stop(runner: ReactiveEffectRunner): void { runner.effect.stop() } -const resetTrackingStack: (typeof System.activeSub)[] = [] +const resetTrackingStack: (typeof activeSub)[] = [] /** * Temporarily pauses tracking. */ export function pauseTracking(): void { - resetTrackingStack.push(System.activeSub) - System.activeSub = undefined - System.activeTrackId = 0 + resetTrackingStack.push(activeSub) + activeSub = undefined + activeTrackId = 0 } /** * Re-enables effect tracking (if it was paused). */ export function enableTracking(): void { - const activeSub = System.activeSub const isPaused = activeSub === undefined if (!isPaused) { // Add the current active effect to the trackResetStack so it can be @@ -238,8 +237,8 @@ export function enableTracking(): void { } } if (prevSub !== undefined) { - System.activeSub = prevSub - System.activeTrackId = prevSub.trackId + activeSub = prevSub + activeTrackId = prevSub.trackId } } } @@ -256,11 +255,11 @@ export function resetTracking(): void { } const prevSub = resetTrackingStack.pop() if (prevSub !== undefined) { - System.activeTrackId = prevSub.trackId + activeTrackId = prevSub.trackId } else { - System.activeTrackId = 0 + activeTrackId = 0 } - System.activeSub = prevSub + activeSub = prevSub } /** @@ -276,8 +275,8 @@ export function resetTracking(): void { * an active effect. */ export function onEffectCleanup(fn: () => void, failSilently = false): void { - if (System.activeSub instanceof ReactiveEffect) { - System.activeSub.cleanup = fn + if (activeSub instanceof ReactiveEffect) { + activeSub.cleanup = fn } else if (__DEV__ && !failSilently) { warn( `onEffectCleanup() was called when there was no active effect` + @@ -291,17 +290,17 @@ function cleanupEffect(e: ReactiveEffect) { e.cleanup = undefined if (cleanup !== undefined) { // run cleanup without active effect - const prevSub = System.activeSub - System.activeSub = undefined + const prevSub = activeSub + activeSub = undefined try { cleanup() } finally { - System.activeSub = prevSub + activeSub = prevSub } } } -//#region Ported from https://github.com/stackblitz/alien-signals/blob/2f3656041a183956a91f805582fcd33026ed46a3/src/system.ts +//#region Ported from https://github.com/stackblitz/alien-signals/blob/2f3656041a183956a91f805582fcd33026ed46a3/src/ts export interface IEffect extends Subscriber { nextNotify: IEffect | undefined notify(): void @@ -341,289 +340,272 @@ export enum DirtyLevels { Dirty, } -export namespace System { - export let activeSub: Subscriber | undefined = undefined - export let activeTrackId = 0 - export let batchDepth = 0 - export let lastTrackId = 0 - export let queuedEffects: IEffect | undefined = undefined - export let queuedEffectsTail: IEffect | undefined = undefined -} +//#region System +export let activeSub: Subscriber | undefined = undefined +export let activeTrackId = 0 +export let lastTrackId = 0 +export const nextTrackId = (): number => ++lastTrackId +let batchDepth = 0 +let queuedEffects: IEffect | undefined = undefined +let queuedEffectsTail: IEffect | undefined = undefined export function startBatch(): void { - System.batchDepth++ + batchDepth++ } export function endBatch(): void { - System.batchDepth-- - if (System.batchDepth === 0) { - while (System.queuedEffects !== undefined) { - const effect = System.queuedEffects - const queuedNext = System.queuedEffects.nextNotify + batchDepth-- + if (batchDepth === 0) { + while (queuedEffects !== undefined) { + const effect = queuedEffects + const queuedNext = queuedEffects.nextNotify if (queuedNext !== undefined) { - System.queuedEffects.nextNotify = undefined - System.queuedEffects = queuedNext + queuedEffects.nextNotify = undefined + queuedEffects = queuedNext } else { - System.queuedEffects = undefined - System.queuedEffectsTail = undefined + queuedEffects = undefined + queuedEffectsTail = undefined } effect.notify() } } } - -export namespace Link { - let pool: Link | undefined = undefined - - export function get( - dep: Dependency, - sub: Subscriber, - nextDep: Link | undefined, - ): Link { - if (pool !== undefined) { - const newLink = pool - pool = newLink.nextDep - newLink.nextDep = nextDep - newLink.dep = dep - newLink.sub = sub - newLink.trackId = sub.trackId - return newLink - } else { - return { - dep, - sub, - trackId: sub.trackId, - nextDep: nextDep, - prevSub: undefined, - nextSub: undefined, - } +//#endregion System + +//#region Link +let pool: Link | undefined = undefined + +function getLink( + dep: Dependency, + sub: Subscriber, + nextDep: Link | undefined, +): Link { + if (pool !== undefined) { + const newLink = pool + pool = newLink.nextDep + newLink.nextDep = nextDep + newLink.dep = dep + newLink.sub = sub + newLink.trackId = sub.trackId + return newLink + } else { + return { + dep, + sub, + trackId: sub.trackId, + nextDep: nextDep, + prevSub: undefined, + nextSub: undefined, } } +} - export function release(link: Link): void { - const dep = link.dep - const nextSub = link.nextSub - const prevSub = link.prevSub - - if (nextSub !== undefined) { - nextSub.prevSub = prevSub - } - if (prevSub !== undefined) { - prevSub.nextSub = nextSub - } - - if (nextSub === undefined) { - dep.subsTail = prevSub - } - if (prevSub === undefined) { - dep.subs = nextSub - } +function release(link: Link): void { + const dep = link.dep + const nextSub = link.nextSub + const prevSub = link.prevSub - // @ts-expect-error - link.dep = undefined - // @ts-expect-error - link.sub = undefined - link.prevSub = undefined - link.nextSub = undefined - link.nextDep = pool - pool = link + if (nextSub !== undefined) { + nextSub.prevSub = prevSub + } + if (prevSub !== undefined) { + prevSub.nextSub = nextSub } -} -export namespace Dependency { - const system = System + if (nextSub === undefined) { + dep.subsTail = prevSub + } + if (prevSub === undefined) { + dep.subs = nextSub + } - export function link(dep: Dependency, sub: Subscriber): void { - const depsTail = sub.depsTail - const old = depsTail !== undefined ? depsTail.nextDep : sub.deps + // @ts-expect-error + link.dep = undefined + // @ts-expect-error + link.sub = undefined + link.prevSub = undefined + link.nextSub = undefined + link.nextDep = pool + pool = link +} +//#endregion Link - if (old === undefined || old.dep !== dep) { - const newLink = Link.get(dep, sub, old) +//#region Dependency +export function link(dep: Dependency, sub: Subscriber): void { + const depsTail = sub.depsTail + const old = depsTail !== undefined ? depsTail.nextDep : sub.deps - if (depsTail === undefined) { - sub.deps = newLink - } else { - depsTail.nextDep = newLink - } + if (old === undefined || old.dep !== dep) { + const newLink = getLink(dep, sub, old) - if (dep.subs === undefined) { - dep.subs = newLink - } else { - const oldTail = dep.subsTail! - newLink.prevSub = oldTail - oldTail.nextSub = newLink - } + if (depsTail === undefined) { + sub.deps = newLink + } else { + depsTail.nextDep = newLink + } - sub.depsTail = newLink - dep.subsTail = newLink + if (dep.subs === undefined) { + dep.subs = newLink } else { - old.trackId = sub.trackId - sub.depsTail = old + const oldTail = dep.subsTail! + newLink.prevSub = oldTail + oldTail.nextSub = newLink } + + sub.depsTail = newLink + dep.subsTail = newLink + } else { + old.trackId = sub.trackId + sub.depsTail = old } +} - export function propagate(subs: Link): void { - let link: Link | undefined = subs - let dep = subs.dep - let dirtyLevel = DirtyLevels.Dirty - let remainingQuantity = 0 - - do { - if (link !== undefined) { - const sub: Link['sub'] = link.sub - const subTrackId = sub.trackId - - if (subTrackId > 0) { - if (subTrackId === link.trackId) { - const subDirtyLevel = sub.dirtyLevel - if (subDirtyLevel < dirtyLevel) { - sub.dirtyLevel = dirtyLevel - if (subDirtyLevel === DirtyLevels.None) { - sub.canPropagate = true - - if ('subs' in sub && sub.subs !== undefined) { - sub.depsTail!.nextDep = link - dep = sub - link = sub.subs - dirtyLevel = DirtyLevels.MaybeDirty - remainingQuantity++ - - continue - } - } - } - } - } else if (subTrackId === -link.trackId) { - const subDirtyLevel = sub.dirtyLevel - const notDirty = subDirtyLevel === DirtyLevels.None +export function propagate(subs: Link): void { + let link: Link | undefined = subs + let dep = subs.dep + let dirtyLevel = DirtyLevels.Dirty + let remainingQuantity = 0 + do { + if (link !== undefined) { + const sub: Link['sub'] = link.sub + const subTrackId = sub.trackId + + if (subTrackId > 0) { + if (subTrackId === link.trackId) { + const subDirtyLevel = sub.dirtyLevel if (subDirtyLevel < dirtyLevel) { sub.dirtyLevel = dirtyLevel - } + if (subDirtyLevel === DirtyLevels.None) { + sub.canPropagate = true - if (notDirty || sub.canPropagate) { - if (!notDirty) { - sub.canPropagate = false - } - - if ('subs' in sub && sub.subs !== undefined) { - sub.depsTail!.nextDep = link - dep = sub - link = sub.subs - dirtyLevel = DirtyLevels.MaybeDirty - remainingQuantity++ + if ('subs' in sub && sub.subs !== undefined) { + sub.depsTail!.nextDep = link + dep = sub + link = sub.subs + dirtyLevel = DirtyLevels.MaybeDirty + remainingQuantity++ - continue - } else if ('notify' in sub) { - const queuedEffectsTail = system.queuedEffectsTail - if (queuedEffectsTail !== undefined) { - queuedEffectsTail.nextNotify = sub - } else { - system.queuedEffects = sub + continue } - system.queuedEffectsTail = sub } } } + } else if (subTrackId === -link.trackId) { + const subDirtyLevel = sub.dirtyLevel + const notDirty = subDirtyLevel === DirtyLevels.None - link = link.nextSub - continue - } - - if (remainingQuantity !== 0) { - const depsTail = (dep as IComputed | IEffect).depsTail! - const prevLink = depsTail.nextDep! - const prevSub = prevLink.sub + if (subDirtyLevel < dirtyLevel) { + sub.dirtyLevel = dirtyLevel + } - depsTail.nextDep = undefined - dep = prevLink.dep - link = prevLink.nextSub - remainingQuantity-- + if (notDirty || sub.canPropagate) { + if (!notDirty) { + sub.canPropagate = false + } - if (remainingQuantity === 0) { - dirtyLevel = DirtyLevels.Dirty - } + if ('subs' in sub && sub.subs !== undefined) { + sub.depsTail!.nextDep = link + dep = sub + link = sub.subs + dirtyLevel = DirtyLevels.MaybeDirty + remainingQuantity++ - if ('notify' in prevSub) { - const queuedEffectsTail = system.queuedEffectsTail - if (queuedEffectsTail !== undefined) { - queuedEffectsTail.nextNotify = prevSub - } else { - system.queuedEffects = prevSub + continue + } else if ('notify' in sub) { + if (queuedEffectsTail !== undefined) { + queuedEffectsTail.nextNotify = sub + } else { + queuedEffects = sub + } + queuedEffectsTail = sub } - system.queuedEffectsTail = prevSub } + } - continue + link = link.nextSub + continue + } + + if (remainingQuantity !== 0) { + const depsTail = (dep as IComputed | IEffect).depsTail! + const prevLink = depsTail.nextDep! + const prevSub = prevLink.sub + + depsTail.nextDep = undefined + dep = prevLink.dep + link = prevLink.nextSub + remainingQuantity-- + + if (remainingQuantity === 0) { + dirtyLevel = DirtyLevels.Dirty } - break - } while (true) - } -} + if ('notify' in prevSub) { + if (queuedEffectsTail !== undefined) { + queuedEffectsTail.nextNotify = prevSub + } else { + queuedEffects = prevSub + } + queuedEffectsTail = prevSub + } -export namespace Subscriber { - const system = System + continue + } - export function resolveMaybeDirty(sub: IComputed | IEffect): void { - let link = sub.deps - let remaining = 0 + break + } while (true) +} +//#endregion Dependency - do { - if (link !== undefined) { - const dep = link.dep +//#region Subscriber +export function resolveMaybeDirty(sub: IComputed | IEffect): void { + let link = sub.deps + let remaining = 0 - if ('update' in dep) { - const depDirtyLevel = dep.dirtyLevel + do { + if (link !== undefined) { + const dep = link.dep - if (depDirtyLevel === DirtyLevels.MaybeDirty) { - dep.subs!.prevSub = link - sub = dep - link = dep.deps - remaining++ + if ('update' in dep) { + const depDirtyLevel = dep.dirtyLevel - continue - } else if (depDirtyLevel === DirtyLevels.Dirty) { - dep.update() - - if (sub.dirtyLevel === DirtyLevels.Dirty) { - if (remaining !== 0) { - const subSubs = (sub as IComputed).subs! - const prevLink = subSubs.prevSub! - ;(sub as IComputed).update() - subSubs.prevSub = undefined - sub = prevLink.sub as IComputed | IEffect - link = prevLink.nextDep - remaining-- - continue - } + if (depDirtyLevel === DirtyLevels.MaybeDirty) { + dep.subs!.prevSub = link + sub = dep + link = dep.deps + remaining++ - break + continue + } else if (depDirtyLevel === DirtyLevels.Dirty) { + dep.update() + + if (sub.dirtyLevel === DirtyLevels.Dirty) { + if (remaining !== 0) { + const subSubs = (sub as IComputed).subs! + const prevLink = subSubs.prevSub! + ;(sub as IComputed).update() + subSubs.prevSub = undefined + sub = prevLink.sub as IComputed | IEffect + link = prevLink.nextDep + remaining-- + continue } + + break } } - - link = link.nextDep - continue } - const dirtyLevel = sub.dirtyLevel - - if (dirtyLevel === DirtyLevels.MaybeDirty) { - sub.dirtyLevel = DirtyLevels.None - if (remaining !== 0) { - const subSubs = (sub as IComputed).subs! - const prevLink = subSubs.prevSub! - subSubs.prevSub = undefined - sub = prevLink.sub as IComputed | IEffect - link = prevLink.nextDep - remaining-- - continue - } - } else if (remaining !== 0) { - if (dirtyLevel === DirtyLevels.Dirty) { - ;(sub as IComputed).update() - } + link = link.nextDep + continue + } + + const dirtyLevel = sub.dirtyLevel + + if (dirtyLevel === DirtyLevels.MaybeDirty) { + sub.dirtyLevel = DirtyLevels.None + if (remaining !== 0) { const subSubs = (sub as IComputed).subs! const prevLink = subSubs.prevSub! subSubs.prevSub = undefined @@ -632,72 +614,84 @@ export namespace Subscriber { remaining-- continue } + } else if (remaining !== 0) { + if (dirtyLevel === DirtyLevels.Dirty) { + ;(sub as IComputed).update() + } + const subSubs = (sub as IComputed).subs! + const prevLink = subSubs.prevSub! + subSubs.prevSub = undefined + sub = prevLink.sub as IComputed | IEffect + link = prevLink.nextDep + remaining-- + continue + } - break - } while (true) - } + break + } while (true) +} - export function startTrack(sub: Subscriber): Subscriber | undefined { - const newTrackId = system.lastTrackId + 1 - const prevSub = system.activeSub +export function startTrack(sub: Subscriber): Subscriber | undefined { + const newTrackId = lastTrackId + 1 + const prevSub = activeSub - system.activeSub = sub - system.activeTrackId = newTrackId - system.lastTrackId = newTrackId + activeSub = sub + activeTrackId = newTrackId + lastTrackId = newTrackId - sub.depsTail = undefined - sub.trackId = newTrackId - sub.dirtyLevel = DirtyLevels.None + sub.depsTail = undefined + sub.trackId = newTrackId + sub.dirtyLevel = DirtyLevels.None - return prevSub - } + return prevSub +} - export function endTrack( - sub: Subscriber, - prevSub: Subscriber | undefined, - ): void { - if (prevSub !== undefined) { - system.activeSub = prevSub - system.activeTrackId = prevSub.trackId - } else { - system.activeSub = undefined - system.activeTrackId = 0 - } +export function endTrack( + sub: Subscriber, + prevSub: Subscriber | undefined, +): void { + if (prevSub !== undefined) { + activeSub = prevSub + activeTrackId = prevSub.trackId + } else { + activeSub = undefined + activeTrackId = 0 + } - const depsTail = sub.depsTail - if (depsTail !== undefined) { - if (depsTail.nextDep !== undefined) { - clearTrack(depsTail.nextDep) - depsTail.nextDep = undefined - } - } else if (sub.deps !== undefined) { - clearTrack(sub.deps) - sub.deps = undefined + const depsTail = sub.depsTail + if (depsTail !== undefined) { + if (depsTail.nextDep !== undefined) { + clearTrack(depsTail.nextDep) + depsTail.nextDep = undefined } - sub.trackId = -sub.trackId + } else if (sub.deps !== undefined) { + clearTrack(sub.deps) + sub.deps = undefined } + sub.trackId = -sub.trackId +} - export function clearTrack(link: Link): void { - do { - const dep = link.dep - const nextDep = link.nextDep - Link.release(link) - if (dep.subs === undefined && 'deps' in dep) { - if ('notify' in dep) { - dep.dirtyLevel = DirtyLevels.None - } else { - dep.dirtyLevel = DirtyLevels.Dirty - } - if (dep.deps !== undefined) { - link = dep.deps - dep.depsTail!.nextDep = nextDep - dep.deps = undefined - dep.depsTail = undefined - continue - } +export function clearTrack(link: Link): void { + do { + const dep = link.dep + const nextDep = link.nextDep + release(link) + if (dep.subs === undefined && 'deps' in dep) { + if ('notify' in dep) { + dep.dirtyLevel = DirtyLevels.None + } else { + dep.dirtyLevel = DirtyLevels.Dirty } - link = nextDep! - } while (link !== undefined) - } + if (dep.deps !== undefined) { + link = dep.deps + dep.depsTail!.nextDep = nextDep + dep.deps = undefined + dep.depsTail = undefined + continue + } + } + link = nextDep! + } while (link !== undefined) } -//#endregion +//#endregion Subscriber +//#endregion alien-signals diff --git a/packages/reactivity/src/effectScope.ts b/packages/reactivity/src/effectScope.ts index 9e8d9329c52..23bcb1cfa7d 100644 --- a/packages/reactivity/src/effectScope.ts +++ b/packages/reactivity/src/effectScope.ts @@ -3,8 +3,9 @@ import { type Link, PauseLevels, type ReactiveEffect, - Subscriber, - System, + type Subscriber, + clearTrack, + nextTrackId, } from './effect' import { warn } from './warning' @@ -14,7 +15,7 @@ export class EffectScope implements Subscriber { // Subscriber: In order to collect orphans computeds deps: Link | undefined = undefined depsTail: Link | undefined = undefined - trackId: number = ++System.lastTrackId + trackId: number = nextTrackId() dirtyLevel: DirtyLevels = DirtyLevels.Dirty canPropagate = false @@ -126,7 +127,7 @@ export class EffectScope implements Subscriber { stop(fromParent?: boolean): void { if (this.active) { if (this.deps !== undefined) { - Subscriber.clearTrack(this.deps) + clearTrack(this.deps) this.deps = undefined this.depsTail = undefined } diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index f22a1c28892..147aa0f6cd2 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -9,7 +9,16 @@ import type { ComputedRef, WritableComputedRef } from './computed' import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants' import { onTrack, triggerEventInfos } from './debug' import { getDepFromReactive } from './dep' -import { Dependency, type Link, System, endBatch, startBatch } from './effect' +import { + type Dependency, + type Link, + activeSub, + activeTrackId, + endBatch, + link, + propagate, + startBatch, +} from './effect' import { type Builtin, type ShallowReactiveMarker, @@ -190,24 +199,23 @@ export function triggerRef(ref: Ref): void { const dep = (ref as unknown as RefImpl).dep if (dep !== undefined && dep.subs !== undefined) { startBatch() - Dependency.propagate(dep.subs) + propagate(dep.subs) endBatch() } } function trackRef(dep: Dependency) { - const activeTrackId = System.activeTrackId if (activeTrackId !== 0) { const subsTail = dep.subsTail if (subsTail === undefined || subsTail.trackId !== activeTrackId) { if (__DEV__) { - onTrack(System.activeSub!, { + onTrack(activeSub!, { target: dep, type: TrackOpTypes.GET, key: 'value', }) } - Dependency.link(dep, System.activeSub!) + link(dep, activeSub!) } } }