Skip to content

Commit

Permalink
fix: fix effect scope tracking for manually created instances
Browse files Browse the repository at this point in the history
fix #12705
  • Loading branch information
yyx990803 committed Aug 18, 2022
1 parent 165a14a commit 7161176
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/core/instance/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function initMixin(Vue: typeof Component) {
vm.__v_skip = true
// effect scope
vm._scope = new EffectScope(true /* detached */)
vm._scope._vm = true
// merge options
if (options && options._isComponent) {
// optimize internal component instantiation
Expand Down
2 changes: 0 additions & 2 deletions src/core/instance/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,13 @@ export function mountComponent(
// we set this to vm._watcher inside the watcher's constructor
// since the watcher's initial patch may call $forceUpdate (e.g. inside child
// component's mounted hook), which relies on vm._watcher being already defined
vm._scope.on()
new Watcher(
vm,
updateComponent,
noop,
watcherOptions,
true /* isRenderWatcher */
)
vm._scope.off()
hydrating = false

// flush buffer for flush: "pre" watchers queued in setup()
Expand Down
11 changes: 10 additions & 1 deletion src/core/observer/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ export default class Watcher implements DepTarget {
options?: WatcherOptions | null,
isRenderWatcher?: boolean
) {
recordEffectScope(this, activeEffectScope || (vm ? vm._scope : undefined))
recordEffectScope(
this,
// if the active effect scope is manually created (not a component scope),
// prioritize it
activeEffectScope && !activeEffectScope._vm
? activeEffectScope
: vm
? vm._scope
: undefined
)
if ((this.vm = vm) && isRenderWatcher) {
vm._watcher = this
}
Expand Down
6 changes: 5 additions & 1 deletion src/v3/reactivity/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export class EffectScope {
* @internal
*/
scopes: EffectScope[] | undefined
/**
* indicates this being a component root scope
* @internal
*/
_vm?: boolean
/**
* track a child scope's index in its parent's scopes array for optimized
* removal
* @internal
*/
private index: number | undefined

Expand Down

0 comments on commit 7161176

Please sign in to comment.