Skip to content

refactor: don't run components in detached effectScopes #13303

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 1 commit into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
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
35 changes: 24 additions & 11 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@ export class EffectScope implements Subscriber {
*/
private index: number | undefined

constructor(public detached = false) {
this.parent = activeEffectScope
if (!detached && activeEffectScope) {
/**
* @param relationship - Defines the parent scope of the current effect scope.
* - If `true`, the scope is detached from any parent.
* - If an instance of `EffectScope`, it becomes the parent scope.
* - Defaults to `false`, indicating the effect scope is attached to the parent if it exists.
*/
constructor(relationship: EffectScope | boolean = false) {
this.parent =
relationship === true ? undefined : relationship || activeEffectScope

if (this.parent) {
this.index =
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
this,
) - 1
(this.parent.scopes || (this.parent.scopes = [])).push(this) - 1
}
}

get detached(): boolean {
return !this.parent
}

get active(): boolean {
return !(this.flags & EffectFlags.STOP)
}
Expand Down Expand Up @@ -153,9 +163,9 @@ export class EffectScope implements Subscriber {
}

// nested scope, dereference from parent to avoid memory leaks
if (!this.detached && this.parent && !fromParent) {
if (!this.detached && this.parent && this.parent.scopes && !fromParent) {
// optimized O(1) removal
const last = this.parent.scopes!.pop()
const last = this.parent.scopes.pop()
if (last && last !== this) {
this.parent.scopes![this.index!] = last
last.index = this.index!
Expand All @@ -172,11 +182,14 @@ export class EffectScope implements Subscriber {
* disposed together. For detailed use cases of this API, please consult its
* corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
*
* @param detached - Can be used to create a "detached" effect scope.
* @param relationship - Defines the parent scope of the current effect scope.
* - If `true`, the scope is detached from any parent.
* - If an instance of `EffectScope`, it becomes the parent scope.
* - Defaults to `false`, indicating the effect scope is attached to the parent if it exists.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
*/
export function effectScope(detached?: boolean): EffectScope {
return new EffectScope(detached)
export function effectScope(relationship?: EffectScope | boolean): EffectScope {
return new EffectScope(relationship)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ export function createComponentInstance(
effect: null!,
update: null!, // will be set synchronously right after creation
job: null!,
scope: new EffectScope(true /* detached */),
scope: parent ? new EffectScope(parent.scope) : new EffectScope(true),
render: null,
proxy: null,
exposed: null,
Expand Down