Skip to content

refactor(effectScope): scopes doubly-linked list #13535

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@ export class EffectScope {
* record undetached scopes
* @internal
*/
scopes: EffectScope[] | undefined
scopes: EffectScope | undefined
scopesTail: EffectScope | undefined
/**
* track a child scope's index in its parent's scopes array for optimized
* removal
* @internal
* sibling scope
*/
private index: number | undefined
prevEffectScope: EffectScope | undefined
nextEffectScope: EffectScope | undefined

constructor(public detached = false) {
this.parent = activeEffectScope
if (!detached && activeEffectScope) {
this.index =
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
this,
) - 1
if (activeEffectScope.scopesTail) {
this.prevEffectScope = activeEffectScope.scopesTail
activeEffectScope.scopesTail.nextEffectScope = this
activeEffectScope.scopesTail = this
} else {
activeEffectScope.scopes = activeEffectScope.scopesTail = this
}
}
}

Expand All @@ -57,13 +60,14 @@ export class EffectScope {
pause(): void {
if (this._active) {
this._isPaused = true
let i, l
if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].pause()
}
for (
let child = this.scopes;
child != undefined;
child = child.nextEffectScope
) {
child.pause()
}
for (i = 0, l = this.effects.length; i < l; i++) {
for (let i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].pause()
}
}
Expand All @@ -76,13 +80,14 @@ export class EffectScope {
if (this._active) {
if (this._isPaused) {
this._isPaused = false
let i, l
if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].resume()
}
for (
let child = this.scopes;
child != undefined;
child = child.nextEffectScope
) {
child.resume()
}
for (i = 0, l = this.effects.length; i < l; i++) {
for (let i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].resume()
}
}
Expand Down Expand Up @@ -140,20 +145,28 @@ export class EffectScope {
}
this.cleanups.length = 0

if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].stop(true)
}
this.scopes.length = 0
for (
let child = this.scopes;
child != undefined;
child = child.nextEffectScope
) {
child.stop(true)
}
this.scopes = this.scopesTail = undefined

// nested scope, dereference from parent to avoid memory leaks
if (!this.detached && this.parent && !fromParent) {
// optimized O(1) removal
const last = this.parent.scopes!.pop()
if (last && last !== this) {
this.parent.scopes![this.index!] = last
last.index = this.index!
if (this.prevEffectScope) {
this.prevEffectScope.nextEffectScope = this.nextEffectScope
}
if (this.nextEffectScope) {
this.nextEffectScope.prevEffectScope = this.prevEffectScope
}
if (this.parent.scopes == this) {
this.parent.scopes = this.nextEffectScope
}
if (this.parent.scopesTail == this) {
this.parent.scopesTail = this.prevEffectScope
}
}
this.parent = undefined
Expand Down
Loading