diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index 6ad693c26c6..836a7d08413 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -15,6 +15,9 @@ import { warn } from './warning' export enum EffectFlags { ALLOW_RECURSE = 1 << 2, + PAUSED = 1 << 3, + NOTIFIED = 1 << 4, + STOP = 1 << 5, } export type EffectScheduler = (...args: any[]) => any @@ -47,13 +50,6 @@ export interface ReactiveEffectRunner { effect: ReactiveEffect } -export enum PauseLevels { - None = 0, - Paused = 1, - Notify = 2, - Stop = 3, -} - export class ReactiveEffect implements IEffect, ReactiveEffectOptions { nextNotify: IEffect | undefined = undefined @@ -62,8 +58,6 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { depsTail: Link | undefined = undefined flags: number = SubscriberFlags.Dirty - pauseLevel: PauseLevels = PauseLevels.None - /** * @internal */ @@ -83,31 +77,32 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { } get active(): boolean { - return this.pauseLevel !== PauseLevels.Stop + return !(this.flags & EffectFlags.STOP) } pause(): void { - if (this.pauseLevel === PauseLevels.None) { - this.pauseLevel = PauseLevels.Paused + if (!(this.flags & EffectFlags.PAUSED)) { + this.flags |= EffectFlags.PAUSED } } resume(): void { - const pauseLevel = this.pauseLevel - if (pauseLevel === PauseLevels.Notify) { - this.pauseLevel = PauseLevels.None + const flags = this.flags + if (flags & EffectFlags.PAUSED) { + this.flags &= ~EffectFlags.PAUSED + } + if (flags & EffectFlags.NOTIFIED) { + this.flags &= ~EffectFlags.NOTIFIED this.notify() - } else if (pauseLevel === PauseLevels.Paused) { - this.pauseLevel = PauseLevels.None } } notify(): void { - const pauseLevel = this.pauseLevel - if (pauseLevel === PauseLevels.None) { + const flags = this.flags + if (!(flags & EffectFlags.PAUSED)) { this.scheduler() - } else if (pauseLevel === PauseLevels.Paused) { - this.pauseLevel = PauseLevels.Notify + } else { + this.flags |= EffectFlags.NOTIFIED } } @@ -157,7 +152,7 @@ export class ReactiveEffect implements IEffect, ReactiveEffectOptions { endTrack(this) cleanupEffect(this) this.onStop && this.onStop() - this.pauseLevel = PauseLevels.Stop + this.flags |= EffectFlags.STOP } } diff --git a/packages/reactivity/src/effectScope.ts b/packages/reactivity/src/effectScope.ts index 76af1bddb80..b03cbc2800f 100644 --- a/packages/reactivity/src/effectScope.ts +++ b/packages/reactivity/src/effectScope.ts @@ -1,4 +1,4 @@ -import { PauseLevels, type ReactiveEffect, nextTrackId } from './effect' +import { EffectFlags, type ReactiveEffect, nextTrackId } from './effect' import { type Link, type Subscriber, @@ -14,7 +14,7 @@ export class EffectScope implements Subscriber { // Subscriber: In order to collect orphans computeds deps: Link | undefined = undefined depsTail: Link | undefined = undefined - flags: SubscriberFlags = SubscriberFlags.None + flags: number = SubscriberFlags.None trackId: number = nextTrackId() @@ -27,8 +27,6 @@ export class EffectScope implements Subscriber { */ cleanups: (() => void)[] = [] - private pauseLevel: PauseLevels = PauseLevels.None - /** * only assigned by undetached scope * @internal @@ -57,12 +55,12 @@ export class EffectScope implements Subscriber { } get active(): boolean { - return this.pauseLevel !== PauseLevels.Stop + return !(this.flags & EffectFlags.STOP) } pause(): void { - if (this.pauseLevel === PauseLevels.None) { - this.pauseLevel = PauseLevels.Paused + if (!(this.flags & EffectFlags.PAUSED)) { + this.flags |= EffectFlags.PAUSED let i, l if (this.scopes) { for (i = 0, l = this.scopes.length; i < l; i++) { @@ -79,8 +77,8 @@ export class EffectScope implements Subscriber { * Resumes the effect scope, including all child scopes and effects. */ resume(): void { - if (this.pauseLevel === PauseLevels.Paused) { - this.pauseLevel = PauseLevels.None + if (this.flags & EffectFlags.PAUSED) { + this.flags &= ~EffectFlags.PAUSED let i, l if (this.scopes) { for (i = 0, l = this.scopes.length; i < l; i++) { @@ -125,7 +123,7 @@ export class EffectScope implements Subscriber { stop(fromParent?: boolean): void { if (this.active) { - this.pauseLevel = PauseLevels.Stop + this.flags |= EffectFlags.STOP startTrack(this) endTrack(this) let i, l diff --git a/packages/reactivity/src/system.ts b/packages/reactivity/src/system.ts index 69c1bb20157..7fed1602264 100644 --- a/packages/reactivity/src/system.ts +++ b/packages/reactivity/src/system.ts @@ -39,14 +39,15 @@ export enum SubscriberFlags { Tracking = 1 << 0, CanPropagate = 1 << 1, // RunInnerEffects = 1 << 2, // Not used in Vue - ToCheckDirty = 1 << 3, - Dirty = 1 << 4, + // 2~5 are using in EffectFlags + ToCheckDirty = 1 << 6, + Dirty = 1 << 7, Dirtys = SubscriberFlags.ToCheckDirty | SubscriberFlags.Dirty, All = SubscriberFlags.Tracking | SubscriberFlags.CanPropagate | SubscriberFlags.Dirtys, - DirtyFlagsIndex = 3, + DirtyFlagsIndex = 6, } let batchDepth = 0