Skip to content

Commit

Permalink
Remove pauseLevel field
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 28, 2024
1 parent 4331b3e commit cf8e0e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
39 changes: 17 additions & 22 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,13 +50,6 @@ export interface ReactiveEffectRunner<T = any> {
effect: ReactiveEffect
}

export enum PauseLevels {
None = 0,
Paused = 1,
Notify = 2,
Stop = 3,
}

export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
nextNotify: IEffect | undefined = undefined

Expand All @@ -62,8 +58,6 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
depsTail: Link | undefined = undefined
flags: number = SubscriberFlags.Dirty

pauseLevel: PauseLevels = PauseLevels.None

/**
* @internal
*/
Expand All @@ -83,31 +77,32 @@ export class ReactiveEffect<T = any> 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
}
}

Expand Down Expand Up @@ -157,7 +152,7 @@ export class ReactiveEffect<T = any> implements IEffect, ReactiveEffectOptions {
endTrack(this)
cleanupEffect(this)
this.onStop && this.onStop()
this.pauseLevel = PauseLevels.Stop
this.flags |= EffectFlags.STOP
}
}

Expand Down
18 changes: 8 additions & 10 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PauseLevels, type ReactiveEffect, nextTrackId } from './effect'
import { EffectFlags, type ReactiveEffect, nextTrackId } from './effect'
import {
type Link,
type Subscriber,
Expand All @@ -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()

Expand All @@ -27,8 +27,6 @@ export class EffectScope implements Subscriber {
*/
cleanups: (() => void)[] = []

private pauseLevel: PauseLevels = PauseLevels.None

/**
* only assigned by undetached scope
* @internal
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/reactivity/src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf8e0e4

Please sign in to comment.