diff --git a/packages-private/dts-test/reactivity.test-d.ts b/packages-private/dts-test/reactivity.test-d.ts
index 270b83da8f4..428d6aa9813 100644
--- a/packages-private/dts-test/reactivity.test-d.ts
+++ b/packages-private/dts-test/reactivity.test-d.ts
@@ -1,4 +1,5 @@
import {
+ EffectScope,
type Ref,
markRaw,
reactive,
@@ -146,3 +147,10 @@ describe('shallowReactive type should accept plain object assignment', () => {
let values: typeof shallow
values = { a: 1, b: 2 }
})
+
+describe('EffectScope can be constructed', () => {
+ new EffectScope()
+ new EffectScope(true)
+ // @ts-expect-error the parent is internal
+ new EffectScope(true, new EffectScope())
+})
diff --git a/packages/reactivity/__tests__/effectScope.spec.ts b/packages/reactivity/__tests__/effectScope.spec.ts
index a949645100f..db8ceef8111 100644
--- a/packages/reactivity/__tests__/effectScope.spec.ts
+++ b/packages/reactivity/__tests__/effectScope.spec.ts
@@ -801,6 +801,138 @@ describe('reactivity/effect/scope', () => {
scope.resume()
expect(log).toEqual(['last 0', 'queued', 'last 1'])
})
+
+ it('a scope linked to a parent for pausing follows it but is not stopped with it', () => {
+ const count = ref(0)
+ const log: number[] = []
+ const parent = effectScope()
+ const child = new EffectScope(true, parent)
+ child.run(() => effect(() => log.push(count.value)))
+ parent.pause()
+ count.value++
+ expect(log).toEqual([0])
+
+ parent.resume()
+ expect(log).toEqual([0, 1])
+
+ parent.stop()
+ expect(child.prevDetached).toBeUndefined()
+ parent.pause()
+ count.value++
+ expect(log).toEqual([0, 1, 2])
+ })
+
+ it('a scope linked to a paused parent starts paused', () => {
+ const count = ref(0)
+ const log: number[] = []
+ const parent = effectScope()
+ parent.pause()
+ const child = new EffectScope(true, parent)
+ child.run(() => effect(() => log.push(count.value)))
+ count.value++
+ expect(log).toEqual([0])
+
+ parent.resume()
+ expect(log).toEqual([0, 1])
+ })
+
+ it('a scope linked for pausing leaves its parent when it stops', () => {
+ const count = ref(0)
+ const log: string[] = []
+ const parent = effectScope()
+ const children = ['a', 'b', 'c', 'd'].map(name => {
+ const child = new EffectScope(true, parent)
+ child.run(() => effect(() => log.push(name + count.value)))
+ return child
+ })
+ children[0].stop()
+ children[2].stop()
+ children[3].stop()
+ parent.pause()
+ count.value++
+ expect(log).toEqual(['a0', 'b0', 'c0', 'd0'])
+
+ parent.resume()
+ expect(log).toEqual(['a0', 'b0', 'c0', 'd0', 'b1'])
+ expect(parent.detachedScopes).toBe(children[1])
+ expect(children[1].prevDetached).toBe(parent)
+ expect(children[1].nextDetached).toBeUndefined()
+
+ children[1].stop()
+ expect(parent.detachedScopes).toBeUndefined()
+ expect(children[1].prevDetached).toBeUndefined()
+ })
+
+ it('resumes the effects of scopes linked for pausing in creation order', () => {
+ const count = ref(0)
+ const log: string[] = []
+ const parent = effectScope()
+ for (const name of ['a', 'b', 'c']) {
+ new EffectScope(true, parent).run(() =>
+ effect(() => log.push(name + count.value)),
+ )
+ }
+ parent.pause()
+ count.value++
+ log.length = 0
+
+ parent.resume()
+ expect(log).toEqual(['a1', 'b1', 'c1'])
+ })
+
+ it('a scope linked to a parent that stops while paused stays paused', () => {
+ const count = ref(0)
+ const log: number[] = []
+ const parent = effectScope()
+ const child = new EffectScope(true, parent)
+ child.run(() => effect(() => log.push(count.value)))
+ parent.pause()
+ count.value++
+ parent.stop()
+ expect(log).toEqual([0])
+ expect(child.active).toBe(true)
+ })
+
+ it('resumes the scopes linked after one that stops itself and the next', () => {
+ const count = ref(0)
+ const log: number[] = []
+ const parent = effectScope()
+ const first = new EffectScope(true, parent)
+ const second = new EffectScope(true, parent)
+ first.run(() =>
+ watch(
+ count,
+ () => {
+ first.stop()
+ second.stop()
+ },
+ { flush: 'sync' },
+ ),
+ )
+ new EffectScope(true, parent).run(() =>
+ effect(() => log.push(count.value)),
+ )
+ parent.pause()
+ count.value++
+ parent.resume()
+ expect(log).toEqual([0, 1])
+ })
+
+ it('stops resuming the scopes linked to a parent stopped on the way', () => {
+ const count = ref(0)
+ const log: number[] = []
+ const parent = effectScope()
+ new EffectScope(true, parent).run(() =>
+ watch(count, () => parent.stop(), { flush: 'sync' }),
+ )
+ new EffectScope(true, parent).run(() =>
+ effect(() => log.push(count.value)),
+ )
+ parent.pause()
+ count.value++
+ parent.resume()
+ expect(log).toEqual([0])
+ })
})
})
diff --git a/packages/reactivity/src/effectScope.ts b/packages/reactivity/src/effectScope.ts
index 0810ea9e2bf..9a98ae0866d 100644
--- a/packages/reactivity/src/effectScope.ts
+++ b/packages/reactivity/src/effectScope.ts
@@ -27,10 +27,45 @@ export class EffectScope implements ReactiveNode {
*/
cleanupsLength = 0
- constructor(detached = false) {
- if (!detached && activeEffectScope) {
- link(this, activeEffectScope)
- if (activeEffectScope.flags & EffectFlags.PAUSED) {
+ /**
+ * The detached scopes given this one as their parent, which follow its
+ * pause until they stop. An attached scope is linked through `deps` instead.
+ * @internal
+ */
+ detachedScopes: EffectScope | undefined = undefined
+ /**
+ * The previous scope in its parent's `detachedScopes`; for the first one,
+ * the parent itself.
+ * @internal
+ */
+ prevDetached: EffectScope | undefined = undefined
+ /**
+ * @internal
+ */
+ nextDetached: EffectScope | undefined = undefined
+
+ constructor(detached?: boolean)
+ /**
+ * @param parent - a detached scope given a parent still follows its pause,
+ * but is not stopped with it
+ * @internal
+ */
+ constructor(detached: true, parent?: EffectScope)
+ constructor(detached = false, parent?: EffectScope) {
+ const scope = detached ? parent : activeEffectScope
+ if (scope) {
+ if (detached) {
+ const head = scope.detachedScopes
+ if (head !== undefined) {
+ head.prevDetached = this
+ this.nextDetached = head
+ }
+ this.prevDetached = scope
+ scope.detachedScopes = this
+ } else {
+ link(this, scope)
+ }
+ if (scope.flags & EffectFlags.PAUSED) {
this.flags = EffectFlags.INHERITED_PAUSED
}
}
@@ -74,6 +109,28 @@ export class EffectScope implements ReactiveNode {
if (sub !== undefined) {
unlink(sub)
}
+ const prev = this.prevDetached
+ if (prev !== undefined) {
+ const next = this.nextDetached
+ if (next !== undefined) {
+ next.prevDetached = prev
+ }
+ if (prev.detachedScopes === this) {
+ prev.detachedScopes = next
+ } else {
+ prev.nextDetached = next
+ }
+ this.prevDetached = this.nextDetached = undefined
+ }
+ // the scopes linked to this one keep their pause: they are expected to
+ // stop right after it
+ let child = this.detachedScopes
+ this.detachedScopes = undefined
+ while (child !== undefined) {
+ const next: EffectScope | undefined = child.nextDetached
+ child.prevDetached = child.nextDetached = undefined
+ child = next
+ }
}
/**
@@ -114,6 +171,13 @@ function pauseScope(scope: EffectScope, by: EffectFlags): void {
pauseScope(node as EffectScope, EffectFlags.INHERITED_PAUSED)
}
}
+ for (
+ let child = scope.detachedScopes;
+ child !== undefined;
+ child = child.nextDetached
+ ) {
+ pauseScope(child, EffectFlags.INHERITED_PAUSED)
+ }
}
// Lifts one of the two pauses; the subtree resumes once neither is left. An
@@ -147,6 +211,30 @@ function resumeScope(scope: EffectScope, by: EffectFlags): void {
}
link = link.nextDep
}
+ // The list starts with the newest: walk back from the oldest, to resume them
+ // in creation order.
+ let child = oldestDetached(scope)
+ while (child !== scope) {
+ if (child.flags & EffectFlags.INHERITED_PAUSED) {
+ resumeScope(child, EffectFlags.INHERITED_PAUSED)
+ // a stopped parent leaves the rest of its scopes paused
+ if (scope.flags & (EffectFlags.PAUSED | EffectFlags.STOP)) return
+ if (child.flags & EffectFlags.STOP) {
+ child = oldestDetached(scope)
+ continue
+ }
+ }
+ child = child.prevDetached!
+ }
+}
+
+function oldestDetached(scope: EffectScope): EffectScope {
+ let child = scope.detachedScopes
+ if (child === undefined) return scope
+ while (child.nextDetached !== undefined) {
+ child = child.nextDetached
+ }
+ return child
}
/**
diff --git a/packages/runtime-core/__tests__/component.spec.ts b/packages/runtime-core/__tests__/component.spec.ts
index 20c68badfb2..9fed4b5e571 100644
--- a/packages/runtime-core/__tests__/component.spec.ts
+++ b/packages/runtime-core/__tests__/component.spec.ts
@@ -1,9 +1,15 @@
import {
type ComponentInternalInstance,
+ type EffectScope,
getCurrentInstance,
+ getCurrentScope,
h,
+ nextTick,
nodeOps,
+ ref,
render,
+ serializeInner,
+ watch,
} from '@vue/runtime-test'
import { type GenericComponent, formatComponentName } from '../src/component'
@@ -161,3 +167,60 @@ describe('formatComponentName', () => {
expect(formatComponentName(instance, Comp)).toBe('CompF')
})
})
+
+describe('component scope pause', () => {
+ test('pausing a component scope pauses its descendants until it resumes', async () => {
+ const count = ref(0)
+ const log: string[] = []
+ let scope!: EffectScope
+ const Leaf = {
+ setup: () => () => (log.push('leaf'), h('i', count.value)),
+ }
+ const Parent = {
+ setup() {
+ scope = getCurrentScope()!
+ return () => h('div', [h(Leaf)])
+ },
+ }
+ const root = nodeOps.createElement('div')
+ render(h(Parent), root)
+ log.length = 0
+
+ scope.pause()
+ count.value++
+ await nextTick()
+ expect(log).toEqual([])
+ expect(serializeInner(root)).toBe('
0
')
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual(['leaf'])
+ expect(serializeInner(root)).toBe('1
')
+ })
+
+ test('a render queued before its scope pauses waits for the resume', async () => {
+ const count = ref(0)
+ const log: string[] = []
+ let scope!: EffectScope
+ const Comp = {
+ setup() {
+ scope = getCurrentScope()!
+ watch(count, () => log.push('watcher'))
+ return () => (log.push('render'), h('div', count.value))
+ },
+ }
+ const root = nodeOps.createElement('div')
+ render(h(Comp), root)
+ log.length = 0
+
+ count.value++
+ scope.pause()
+ await nextTick()
+ expect(log).toEqual([])
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual(['watcher', 'render'])
+ expect(serializeInner(root)).toBe('1
')
+ })
+})
diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts
index 5176b4e5ac2..8197a26da15 100644
--- a/packages/runtime-core/src/apiWatch.ts
+++ b/packages/runtime-core/src/apiWatch.ts
@@ -159,7 +159,8 @@ class RenderWatcherEffect extends WatcherEffect {
super(source, cb, options)
const job: SchedulerJob = () => {
- if (this.dirty) {
+ // the job may already be queued when its scope is paused
+ if (!(this.flags & EffectFlags.PAUSED) && this.dirty) {
this.run()
}
}
diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts
index 30b3084f552..78ab7bece2a 100644
--- a/packages/runtime-core/src/component.ts
+++ b/packages/runtime-core/src/component.ts
@@ -768,7 +768,7 @@ export function createComponentInstance(
effect: null!,
update: null!, // will be set synchronously right after creation
job: null!,
- scope: new EffectScope(true /* detached */),
+ scope: new EffectScope(true, parent ? parent.scope : undefined),
render: null,
proxy: null,
exposed: null,
diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts
index 242a5a0aa2b..ca7a35015df 100644
--- a/packages/runtime-core/src/renderer.ts
+++ b/packages/runtime-core/src/renderer.ts
@@ -1464,7 +1464,8 @@ function baseCreateRenderer(
setCurrentScope(prevScope)
this.job = instance.job = () => {
- if (this.dirty) {
+ // the job may already be queued when its scope is paused
+ if (!(this.flags & EffectFlags.PAUSED) && this.dirty) {
this.run()
}
}
diff --git a/packages/runtime-vapor/__tests__/component.spec.ts b/packages/runtime-vapor/__tests__/component.spec.ts
index a4bcf59b148..f47a9ce7830 100644
--- a/packages/runtime-vapor/__tests__/component.spec.ts
+++ b/packages/runtime-vapor/__tests__/component.spec.ts
@@ -1166,6 +1166,224 @@ describe('component', () => {
})
})
+describe('component scope pause', () => {
+ test('pausing a component scope pauses its descendants until it resumes', async () => {
+ const data = ref({ count: 0, list: [1, 2] })
+ const log: string[] = []
+ const components: any = { log: (s: string) => void log.push(s) }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ components.Leaf = compile(
+ `
+ {{ components.log('leaf') || data.count }}
+ `,
+ data,
+ components,
+ )
+ const Parent = compile(
+ `
+
+
+
+ {{ components.log('row') || i * 10 + data.count }}
+
+
+
+ `,
+ data,
+ components,
+ )
+ const { host } = define(Parent).render()
+ log.length = 0
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(log).toEqual([])
+ expect(host.textContent).toBe('10200')
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual(expect.arrayContaining(['row', 'leaf']))
+ expect(host.textContent).toBe('11211')
+ })
+
+ test('pausing a component scope pauses the slot fallback of a child', async () => {
+ const data = ref({ count: 0, show: false })
+ const log: string[] = []
+ const components: any = { log: (s: string) => void log.push(s) }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ components.Child = compile(
+ `
+ {{ components.log('fallback') || data.count }}
+ `,
+ data,
+ components,
+ )
+ const Parent = compile(
+ `
+
+
+ content
+
+ `,
+ data,
+ components,
+ )
+ const { host } = define(Parent).render()
+ log.length = 0
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(log).toEqual([])
+ expect(host.textContent).toBe('0')
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual(['fallback'])
+ expect(host.textContent).toBe('1')
+ })
+
+ test('pausing a component scope pauses the directives on a child', async () => {
+ const data = ref({ count: 0 })
+ const log: string[] = []
+ const components: any = {
+ dir: (_el: Element, value: () => number) =>
+ void watchEffect(() => log.push('dir ' + value())),
+ }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ components.Child = compile(
+ `
+ child
+ `,
+ data,
+ components,
+ )
+ const Parent = compile(
+ `
+
+
+ `,
+ data,
+ components,
+ )
+ define(Parent).render()
+ log.length = 0
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(log).toEqual([])
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual(['dir 1'])
+ })
+
+ test('pausing a component scope pauses the effects on a component row', async () => {
+ const data = ref({ show: true, list: [1] })
+ const components: any = {}
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ components.Child = compile(
+ `
+ child
+ `,
+ data,
+ components,
+ )
+ const Parent = compile(
+ `
+
+
+
+
+ `,
+ data,
+ components,
+ )
+ const { host } = define(Parent).render()
+
+ scope.pause()
+ data.value.show = false
+ await nextTick()
+ expect(host.innerHTML).not.toContain('display: none')
+
+ scope.resume()
+ await nextTick()
+ expect(host.innerHTML).toContain('display: none')
+ })
+
+ test('pausing a component scope pauses the inputs of a KeepAlive child', async () => {
+ const data = ref({ count: 0 })
+ const log: number[] = []
+ const components: any = { read: (n: number) => (log.push(n), n) }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ components.Child = compile(
+ `
+
+ {{ n }}
+ `,
+ data,
+ components,
+ )
+ const Parent = compile(
+ `
+
+
+
+
+ `,
+ data,
+ components,
+ )
+ const { host } = define(Parent).render()
+ log.length = 0
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(log).toEqual([])
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual([1])
+ expect(host.textContent).toBe('1')
+ })
+})
+
function getEffectsCount(scope: EffectScope): number {
let n = 0
for (let dep = scope.deps; dep !== undefined; dep = dep.nextDep) {
diff --git a/packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts b/packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts
index 75bdb362eb1..691d0e77bde 100644
--- a/packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts
+++ b/packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts
@@ -14,6 +14,7 @@ import {
ref,
shallowRef,
vModelText,
+ watchEffect,
withDirectives,
} from 'vue'
import {
@@ -6208,3 +6209,57 @@ describe('KeepAlive caching a component that ends without a node', () => {
})
})
})
+
+describe('KeepAlive with a directive on the cached root', () => {
+ test('pauses the directive with the deactivated branch', async () => {
+ const data = ref({ count: 0, ok: true })
+ const log: string[] = []
+ const components: any = {
+ dir: (_el: Element, value: () => number) =>
+ void watchEffect(() => log.push(`dir ${value()}`)),
+ }
+ components.A = compile(
+ `
+ a
+ `,
+ data,
+ components,
+ )
+ components.B = compile(
+ `
+ b
+ `,
+ data,
+ components,
+ )
+ const App = compile(
+ `
+
+
+
+
+
+ `,
+ data,
+ components,
+ )
+ const { host } = define(App).render()
+ expect(log).toEqual(['dir 0'])
+
+ data.value.ok = false
+ await nextTick()
+ data.value.count++
+ await nextTick()
+ expect(host.textContent).toBe('b')
+ expect(log).toEqual(['dir 0'])
+
+ data.value.ok = true
+ await nextTick()
+ expect(host.textContent).toBe('a')
+ expect(log).toEqual(['dir 0', 'dir 1'])
+ })
+})
diff --git a/packages/runtime-vapor/__tests__/vdomInterop.spec.ts b/packages/runtime-vapor/__tests__/vdomInterop.spec.ts
index 43f11891dd0..c1aef0c6240 100644
--- a/packages/runtime-vapor/__tests__/vdomInterop.spec.ts
+++ b/packages/runtime-vapor/__tests__/vdomInterop.spec.ts
@@ -20288,3 +20288,166 @@ describe('interop root el', () => {
},
)
})
+
+describe('component scope pause across the interop boundary', () => {
+ test('pausing a vapor child pauses the fallback of a slot a vdom parent passes', async () => {
+ const data = ref({ count: 0 })
+ const log: string[] = []
+ const components: any = { log: (s: string) => void log.push(s) }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ const VaporChild = compile(
+ `
+
+ {{ components.log('fallback') || data.count }}
+ `,
+ data,
+ components,
+ )
+ const Parent = defineComponent({
+ setup: () => () => h(VaporChild as any, null, { default: () => [] }),
+ })
+ const { host } = define(Parent).render()
+ expect(host.innerHTML).toContain('0')
+ log.length = 0
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(log).toEqual([])
+ expect(host.innerHTML).toContain('0')
+
+ scope.resume()
+ await nextTick()
+ expect(log).toEqual(['fallback'])
+ expect(host.innerHTML).toContain('1')
+ })
+
+ test('pausing a vapor parent holds a slot update queued before the pause', async () => {
+ const data = ref({ count: 0 })
+ const log: string[] = []
+ const components: any = { log: (s: string) => void log.push(s) }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ components.VdomChild = compile(
+ `
+
+
+ `,
+ data,
+ components,
+ { vapor: false },
+ )
+ const Parent = compile(
+ `
+
+
+ {{ data.count }}
+
+ `,
+ data,
+ components,
+ )
+ const { html } = define(Parent).render()
+
+ data.value.count++
+ scope.pause()
+ await nextTick()
+ expect(log).toEqual([])
+ expect(html()).toBe('0
')
+
+ scope.resume()
+ await nextTick()
+ expect(html()).toBe('1
')
+ expect(log).toEqual(['child update'])
+ })
+
+ test('pausing a vapor parent pauses the props of a resolved async vdom child', async () => {
+ const data = ref({ count: 0 })
+ const components: any = {}
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ const VdomChild = compile(
+ `
+
+ {{ n }}
+ `,
+ data,
+ {},
+ { vapor: false },
+ )
+ components.Child = defineAsyncComponent(() => Promise.resolve(VdomChild))
+ const Parent = compile(
+ `
+
+
+ `,
+ data,
+ components,
+ )
+ const { html } = define(Parent).render()
+ await new Promise(r => setTimeout(r))
+ await nextTick()
+ expect(html()).toContain('0')
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(html()).toContain('0')
+
+ scope.resume()
+ await nextTick()
+ expect(html()).toContain('1')
+ })
+
+ test('pausing a vapor parent pauses the props it adds to a vnode it renders', async () => {
+ const data = ref({ count: 0 })
+ const components: any = { vnode: h('b', 'x') }
+ let scope: any
+ components.capture = (s: any) => (scope = s)
+ const Parent = compile(
+ `
+
+
+ `,
+ data,
+ components,
+ )
+ const { html } = define(Parent).render()
+
+ scope.pause()
+ data.value.count++
+ await nextTick()
+ expect(html()).toContain('title="0"')
+
+ scope.resume()
+ await nextTick()
+ expect(html()).toContain('title="1"')
+ })
+})
diff --git a/packages/runtime-vapor/src/apiCreateFor.ts b/packages/runtime-vapor/src/apiCreateFor.ts
index 7fc2d625496..d04c330761d 100644
--- a/packages/runtime-vapor/src/apiCreateFor.ts
+++ b/packages/runtime-vapor/src/apiCreateFor.ts
@@ -471,7 +471,7 @@ export const createFor = (
const indexRef = needIndex ? shallowRef(keys ? idx : undefined) : undefined
let nodes: Block
- const scope = new EffectScope(true)
+ const scope = new EffectScope(true, instance.scope)
const prevScope = setCurrentScope(scope)
let ok = false
try {
diff --git a/packages/runtime-vapor/src/component.ts b/packages/runtime-vapor/src/component.ts
index 4329d427ae8..d81e624f0fc 100644
--- a/packages/runtime-vapor/src/component.ts
+++ b/packages/runtime-vapor/src/component.ts
@@ -564,7 +564,7 @@ export function createComponent(
let inputScope: EffectScope | undefined
if (keepAliveCtx && !once && !managedMount) {
- inputScope = new EffectScope(true)
+ inputScope = new EffectScope(true, currentInstance!.scope)
}
// The VDOM bridge delivers inputs through the same cache-owned scope.
@@ -1219,7 +1219,10 @@ export class VaporComponentInstance<
}
this.block = null! // to be set
- this.scope = new EffectScope(true)
+ this.scope = new EffectScope(
+ true,
+ currentInstance ? currentInstance.scope : undefined,
+ )
this.emit = emit.bind(null, this) as EmitFn
this.expose = exposeVapor.bind(null, this) as any
diff --git a/packages/runtime-vapor/src/directives/custom.ts b/packages/runtime-vapor/src/directives/custom.ts
index cf5a7d29dd7..9099a9dbb0e 100644
--- a/packages/runtime-vapor/src/directives/custom.ts
+++ b/packages/runtime-vapor/src/directives/custom.ts
@@ -1,4 +1,4 @@
-import { EffectScope } from '@vue/reactivity'
+import { EffectScope, getCurrentScope } from '@vue/reactivity'
import {
type DirectiveModifiers,
ErrorCodes,
@@ -76,6 +76,8 @@ export function withVaporDirectives(
}
const instance = currentInstance
+ // the scope the directives are bound to, and paused with
+ const parentScope = getCurrentScope()
// Deferred (re)application keeps the once ambient it was created under.
const once = inOnce
let currentElement: Element | null | undefined = null
@@ -217,7 +219,7 @@ export function withVaporDirectives(
// The fragment makes the root element mutable without disposing the owner scope
// So directive effects and cleanup need a replaceable detached scope
- directiveScope = new EffectScope(true)
+ directiveScope = new EffectScope(true, parentScope)
// Re-apply in the original directive owner's component context
const prev = setCurrentInstance(instance, directiveScope)
try {
diff --git a/packages/runtime-vapor/src/fragment.ts b/packages/runtime-vapor/src/fragment.ts
index 007efefbf10..7ea34d01bfc 100644
--- a/packages/runtime-vapor/src/fragment.ts
+++ b/packages/runtime-vapor/src/fragment.ts
@@ -333,6 +333,7 @@ export function createSlotBoundary(
getParent,
getFallback,
run: (fn, scope) => runWithRenderCtx(fragment, fn, scope),
+ hostInstance: fragment.renderInstance,
getScopeIds,
markDirty,
isHolding:
diff --git a/packages/runtime-vapor/src/slotBoundary.ts b/packages/runtime-vapor/src/slotBoundary.ts
index 19112cd9f48..3002af37b1b 100644
--- a/packages/runtime-vapor/src/slotBoundary.ts
+++ b/packages/runtime-vapor/src/slotBoundary.ts
@@ -1,4 +1,5 @@
import type { EffectScope } from '@vue/reactivity'
+import type { GenericComponentInstance } from '@vue/runtime-dom'
import type { Block, BlockFn } from './block'
import type { FallthroughOwner } from './component'
import {
@@ -23,6 +24,8 @@ export interface SlotBoundaryContext {
// late renders such as fallback bodies, and runs them in the provided effect
// scope when one is provided.
run(fn: () => R, scope?: EffectScope): R
+ // The slot host's instance: the fallbacks pause with it.
+ hostInstance?: GenericComponentInstance | null
// The slot scope ids this provider's fallback renders under: the providing
// outlet's cell (see renderSlotFallbacks).
getScopeIds?: () => string[] | null
diff --git a/packages/runtime-vapor/src/slotFragment.ts b/packages/runtime-vapor/src/slotFragment.ts
index 7e954c46cc6..e40cbbcca83 100644
--- a/packages/runtime-vapor/src/slotFragment.ts
+++ b/packages/runtime-vapor/src/slotFragment.ts
@@ -464,7 +464,8 @@ function renderAndCommitSlotFallback(state: SlotResolutionState): void {
// A dedicated detached scope: the fallbacks must not die with whatever
// branch scope happens to be active when a recheck fires. The host stops
// it on replacement or disposal; a failed render is stopped here.
- const scope = new EffectScope(true)
+ const host = state.boundary.hostInstance
+ const scope = new EffectScope(true, host ? host.scope : undefined)
let rendered: RenderedSlotFallback[]
try {
rendered = renderSlotFallbacks(state.boundary, scope)
diff --git a/packages/runtime-vapor/src/vdomInterop.ts b/packages/runtime-vapor/src/vdomInterop.ts
index 65b9d9b9e79..d2987b54bdd 100644
--- a/packages/runtime-vapor/src/vdomInterop.ts
+++ b/packages/runtime-vapor/src/vdomInterop.ts
@@ -90,7 +90,7 @@ import {
withCtx,
} from '@vue/runtime-dom'
import {
- type EffectScope,
+ EffectScope,
effectScope,
endBatch,
setActiveSub,
@@ -2025,7 +2025,10 @@ function mountVNode(
// happens before the mount and only establishes the dependency.
// A KeepAlive hit reuses this fragment without rebinding, so the effect
// is detached from the branch render; unmount() stops it once mounted.
- const inputScope = (frag.inputScope = effectScope(true))
+ const inputScope = (frag.inputScope = new EffectScope(
+ true,
+ parentComponent ? parentComponent.scope : undefined,
+ ))
inputScope.run(() => renderEffect(update))
onScopeDispose(() => {
if (!isMounted) inputScope.stop()
@@ -2103,7 +2106,12 @@ function createVDOMComponent(
let hasDynamicProps =
!once &&
(!!cells || hasDynamicPropsSource((rawProps || EMPTY_OBJ) as RawProps))
- if (hasDynamicProps) inputScope ||= effectScope(true)
+ if (hasDynamicProps) {
+ inputScope ||= new EffectScope(
+ true,
+ parentComponent ? parentComponent.scope : undefined,
+ )
+ }
const prevInstance = setCurrentInstance(parentComponent, inputScope)
let vnode: VNode
try {
@@ -4518,7 +4526,9 @@ function renderVaporSlot(
const renderContent = (): void => {
content.nodes =
withRenderContext(frag.ctx, () =>
- withSlotBoundary(rootBoundary, () => invokeVaporSlot(vnode)),
+ withSlotBoundary(rootBoundary, () =>
+ invokeVaporSlot(vnode, parentComponent),
+ ),
) || EMPTY_BLOCK
if (fallthrough) {
const scope = vnode.vs!.scope
@@ -4606,11 +4616,18 @@ function stopVaporSlotScope(vnode: VNode): void {
* Slot functions can create renderEffects while evaluating their block.
* Those effects live in this detached scope, which only the vnode's re-mount
* and unmount stop: after the vapor component passing the slot unmounted, not
- * with the scope that happens to render the vdom outlet.
+ * with the scope that happens to render the vdom outlet, though it pauses with
+ * the outlet's instance.
*/
-function invokeVaporSlot(vnode: VNode): Block {
+function invokeVaporSlot(
+ vnode: VNode,
+ parentComponent: ComponentInternalInstance | null,
+): Block {
const propsRef = (vnode.vs!.ref = shallowRef(vnode.vs!.props || vnode.props))
- const scope = effectScope(true)
+ const scope = new EffectScope(
+ true,
+ parentComponent ? parentComponent.scope : undefined,
+ )
vnode.vs!.scope = scope
try {
const invoke = () =>
diff --git a/packages/runtime-vapor/src/vdomSlotOwner.ts b/packages/runtime-vapor/src/vdomSlotOwner.ts
index 0cd27717c83..8e9e590f23b 100644
--- a/packages/runtime-vapor/src/vdomSlotOwner.ts
+++ b/packages/runtime-vapor/src/vdomSlotOwner.ts
@@ -38,8 +38,14 @@ export function initVdomOwner(owner: ComponentInternalInstance): void {
updating: false,
update: () => {
// the owner may have been removed in this flush: its effect stops at
- // once, while `isUnmounted` waits for the post flush
- if (state.stale && owner.effect.active) owner.update()
+ // once, while `isUnmounted` waits for the post flush. A paused owner
+ // is queued again by the content's effects when it resumes
+ if (
+ state.stale &&
+ !(owner.effect.flags & (EffectFlags.STOP | EffectFlags.PAUSED))
+ ) {
+ owner.update()
+ }
},
}
state.update.i = owner