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(watch): memory leak #590

Merged
merged 5 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions packages/runtime-core/src/apiReactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ import {

import { currentInstance } from './component'

type RemoveRecord = () => void
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
type RemoveRecord = () => void


// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordEffect(effect: ReactiveEffect) {
export function recordEffect(effect: ReactiveEffect): RemoveRecord | void {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export function recordEffect(effect: ReactiveEffect): RemoveRecord | void {
export function recordEffect(effect: ReactiveEffect): (() => void) | void {

if (currentInstance) {
;(currentInstance.effects || (currentInstance.effects = [])).push(effect)
const effects = currentInstance.effects || (currentInstance.effects = [])
effects.push(effect)
return () => {
const index = effects.findIndex(_effect => _effect === effect)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const index = effects.findIndex(_effect => _effect === effect)
const index = effects.indexOf(effect)

if (index !== -1) {
effects.splice(index, 1)
}
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,12 @@ function doWatch(
}
}

recordEffect(runner)
const removeRecord = recordEffect(runner)
return () => {
stop(runner)
if (removeRecord) {
removeRecord()
}
}
}

Expand Down