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

fix(component): clean up memory leak after loading async component completes (fix #8740) #8755

Merged
merged 3 commits into from
Nov 30, 2018
Merged
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
12 changes: 8 additions & 4 deletions src/core/vdom/helpers/resolve-async-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ export function resolveAsyncComponent (
const contexts = factory.contexts = [context]
let sync = true

const forceRender = () => {
const forceRender = (renderCompleted: boolean) => {
for (let i = 0, l = contexts.length; i < l; i++) {
contexts[i].$forceUpdate()
}

if (renderCompleted) {
contexts.length = 0
}
}

const resolve = once((res: Object | Class<Component>) => {
Expand All @@ -73,7 +77,7 @@ export function resolveAsyncComponent (
// invoke callbacks only if this is not a synchronous resolve
// (async resolves are shimmed as synchronous during SSR)
if (!sync) {
forceRender()
forceRender(true)
}
})

Expand All @@ -84,7 +88,7 @@ export function resolveAsyncComponent (
)
if (isDef(factory.errorComp)) {
factory.error = true
forceRender()
forceRender(true)
}
})

Expand All @@ -111,7 +115,7 @@ export function resolveAsyncComponent (
setTimeout(() => {
if (isUndef(factory.resolved) && isUndef(factory.error)) {
factory.loading = true
forceRender()
forceRender(false)
}
}, res.delay || 200)
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/features/transition/transition-mode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ if (!isIE9) {
setTimeout(() => {
resolve({ template: '<div><h1>component B</h1></div>' })
Vue.nextTick(next)
}, (duration + buffer) * 1.5)
}, (duration + buffer) * 1.7)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was noticing timing issues while running the vue tests on my laptop locally. This test would pass if part of the entire suite, but if it was marked as a focused test, it would fail due to timing issues. I encountered this with the code prior to my change as well

}
},
data: {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/modules/vdom/create-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('create-component', () => {
vnode = createComponent(async, data, vm, vm)
expect(vnode.isComment).toBe(true) // not to be loaded yet.
expect(vnode.asyncFactory).toBe(async)
expect(vnode.asyncFactory.contexts.length).toEqual(1)
}
function loaded () {
vnode = createComponent(async, data, vm, vm)
Expand All @@ -68,6 +69,7 @@ describe('create-component', () => {
expect(vnode.elm).toBeUndefined()
expect(vnode.ns).toBeUndefined()
expect(vnode.context).toEqual(vm)
expect(vnode.asyncFactory.contexts.length).toEqual(0)
expect(vm.$forceUpdate).toHaveBeenCalled()
done()
}
Expand Down