Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(effectScope): add active property getter #6187

Merged
merged 5 commits into from Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/reactivity/__tests__/effectScope.spec.ts
Expand Up @@ -26,6 +26,14 @@ describe('reactivity/effect/scope', () => {
expect(new EffectScope().run(() => 1)).toBe(1)
})

it('should work w/ active property', () => {
const scope = new EffectScope()
scope.run(() => 1)
expect(scope.active).toBe(true)
scope.stop()
expect(scope.active).toBe(false)
})

it('should collect the effects', () => {
const scope = new EffectScope()
scope.run(() => {
Expand Down
12 changes: 8 additions & 4 deletions packages/reactivity/src/effectScope.ts
Expand Up @@ -7,7 +7,7 @@ export class EffectScope {
/**
* @internal
*/
active = true
private _active = true
/**
* @internal
*/
Expand Down Expand Up @@ -44,8 +44,12 @@ export class EffectScope {
}
}

get active() {
return this._active
}

run<T>(fn: () => T): T | undefined {
if (this.active) {
if (this._active) {
const currentEffectScope = activeEffectScope
try {
activeEffectScope = this
Expand Down Expand Up @@ -75,7 +79,7 @@ export class EffectScope {
}

stop(fromParent?: boolean) {
if (this.active) {
if (this._active) {
let i, l
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop()
Expand All @@ -97,7 +101,7 @@ export class EffectScope {
last.index = this.index!
}
}
this.active = false
this._active = false
}
}
}
Expand Down