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: add pause/resume methods for render function #9206

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
45 changes: 45 additions & 0 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,4 +977,49 @@ describe('KeepAlive', () => {
expect(mountedB).toHaveBeenCalledTimes(1)
expect(unmountedB).toHaveBeenCalledTimes(0)
})

test('should resume/pause update in activated/deactivated', async () => {
const renderA = vi.fn(() => 'A')
const msg = ref('hello')
const A = {
render: () => h('div', [renderA(), msg.value])
}
const B = {
render: () => 'B'
}

const current = shallowRef(A)
const app = createApp({
setup() {
return () => {
return [h(KeepAlive, h(current.value))]
}
}
})

app.mount(root)

expect(serializeInner(root)).toBe(`<div>Ahello</div>`)
expect(renderA).toHaveBeenCalledTimes(1)
msg.value = 'world'
await nextTick()
expect(serializeInner(root)).toBe(`<div>Aworld</div>`)
expect(renderA).toHaveBeenCalledTimes(2)

// @ts-expect-error
current.value = B
await nextTick()
expect(serializeInner(root)).toBe(`B`)
expect(renderA).toHaveBeenCalledTimes(2)

msg.value = 'hello world'
await nextTick()
expect(serializeInner(root)).toBe(`B`)
expect(renderA).toHaveBeenCalledTimes(2)

current.value = A
await nextTick()
expect(serializeInner(root)).toBe(`<div>Ahello world</div>`)
expect(renderA).toHaveBeenCalledTimes(3)
})
})
6 changes: 3 additions & 3 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
EffectScope,
markRaw,
track,
TrackOpTypes,
ReactiveEffect
TrackOpTypes
} from '@vue/reactivity'
import {
ComponentPublicInstance,
Expand Down Expand Up @@ -79,6 +78,7 @@ import {
} from './compat/compatConfig'
import { SchedulerJob } from './scheduler'
import { LifecycleHooks } from './enums'
import { RenderEffect } from './renderEffect'

export type Data = Record<string, unknown>

Expand Down Expand Up @@ -240,7 +240,7 @@ export interface ComponentInternalInstance {
/**
* Render effect instance
*/
effect: ReactiveEffect
effect: RenderEffect
/**
* Bound effect runner to be passed to schedulers
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ const KeepAliveImpl: ComponentOptions = {

sharedContext.activate = (vnode, container, anchor, isSVG, optimized) => {
const instance = vnode.component!
// on activation, resume the effect of the component instance and immediately execute the call during the pause process
instance.effect.resume(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could have a prop for KeepAlive to toggle this behavior (and default disabling it to keep existing behaviour)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can add a prop to preserve the original behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a lazy prop to control whether it should pause in the deactivated state.

move(vnode, container, anchor, MoveType.ENTER, parentSuspense)
// in case props have changed
patch(
Expand Down Expand Up @@ -159,6 +161,8 @@ const KeepAliveImpl: ComponentOptions = {

sharedContext.deactivate = (vnode: VNode) => {
const instance = vnode.component!
// on deactivation, pause the effect of the component instance
instance.effect.pause()
move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense)
queuePostRenderEffect(() => {
if (instance.da) {
Expand Down
28 changes: 28 additions & 0 deletions packages/runtime-core/src/renderEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ReactiveEffect } from '@vue/reactivity'

/**
* Extend `ReactiveEffect` by adding `pause` and `resume` methods for controlling the execution of the `render` function.
*/
export class RenderEffect extends ReactiveEffect {
private _isPaused = false
private _isCalled = false
pause() {
this._isPaused = true
}
resume(runOnce = false) {
if (this._isPaused) {
this._isPaused = false
if (this._isCalled && runOnce) {
super.run()
}
this._isCalled = false
}
}
update() {
if (this._isPaused) {
this._isCalled = true
} else {
return super.run()
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the compatibility of pausable effect. Since this is not bound to the runtime/dom, I wonder if this could be generally available in ReactiveEffect directly.

/cc @yyx990803 WDYT? Or do you think it deserves a dedicated RFC?

Copy link
Member Author

@Alfred-Skyblue Alfred-Skyblue Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the addition of pause and resume methods in ReactiveEffect has significant implications for the existing codebase, I have initiated an RFC to discuss this feature.

vuejs/rfcs#599

/cc @yyx990803 @antfu

}
9 changes: 5 additions & 4 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
flushPreFlushCbs,
SchedulerJob
} from './scheduler'
import { pauseTracking, resetTracking, ReactiveEffect } from '@vue/reactivity'
import { pauseTracking, resetTracking } from '@vue/reactivity'
import { updateProps } from './componentProps'
import { updateSlots } from './componentSlots'
import { pushWarningContext, popWarningContext, warn } from './warning'
Expand All @@ -72,6 +72,7 @@ import { initFeatureFlags } from './featureFlags'
import { isAsyncWrapper } from './apiAsyncComponent'
import { isCompatEnabled } from './compat/compatConfig'
import { DeprecationTypes } from './compat/compatConfig'
import { RenderEffect } from './renderEffect'
import { TransitionHooks } from './components/BaseTransition'

export interface Renderer<HostElement = RendererElement> {
Expand Down Expand Up @@ -1541,14 +1542,14 @@ function baseCreateRenderer(
}
}

// create reactive effect for rendering
const effect = (instance.effect = new ReactiveEffect(
// create render effect for rendering
const effect = (instance.effect = new RenderEffect(
componentUpdateFn,
() => queueJob(update),
instance.scope // track it in component's effect scope
))

const update: SchedulerJob = (instance.update = () => effect.run())
const update: SchedulerJob = (instance.update = () => effect.update())
update.id = instance.uid
// allowRecurse
// #1801, #2043 component render effects should allow recursive updates
Expand Down