Skip to content

feat(computed): add cleanup method #2389

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

Closed
wants to merge 2 commits into from
Closed
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
58 changes: 57 additions & 1 deletion packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
stop,
ref,
WritableComputedRef,
isReadonly
isReadonly,
ReactiveEffect,
ComputedRef
} from '../src'

describe('reactivity/computed', () => {
Expand Down Expand Up @@ -193,4 +195,58 @@ describe('reactivity/computed', () => {
expect(isReadonly(z)).toBe(false)
expect(isReadonly(z.value.a)).toBe(false)
})

describe('cleanup', () => {
let value: { foo: number }
let cValue: ComputedRef<number>
let onTrigger: any
let effect: ReactiveEffect
beforeEach(() => {
value = reactive<{ foo: number }>({ foo: 123 })
cValue = computed(() => value.foo)

onTrigger = jest.fn(() => {})
effect = cValue.effect as ReactiveEffect
})

it('should be dereferenced after cleanup', () => {
// Initially, no references should be present.
expect(effect.deps.length).toBe(0)

expect(cValue.value).toBe(123)

// Should create the dependency.
expect(effect.deps.length).toBe(1)

const reactiveDeps = effect.deps[0]
expect(reactiveDeps).toContain(effect)

onTrigger.mockReset()

// After cleanup, the dependencies should be cleared.
cValue.cleanup()

expect(reactiveDeps).not.toContain(effect)
expect(effect.deps).toEqual([])
})

it('should recover when used after cleanup', () => {
expect(cValue.value).toBe(123)

const reactiveDeps = effect.deps[0]
expect(reactiveDeps).toContain(effect)

cValue.cleanup()

// Should still be 'active' as it still fully functional (can recover).
expect(effect.active).toBe(true)

value.foo = 4
expect(cValue.value).toBe(4)

// After invoking, should be linked again.
expect(reactiveDeps).toContain(effect)
expect(effect.deps).toEqual([reactiveDeps])
})
})
})
9 changes: 8 additions & 1 deletion packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { effect, ReactiveEffect, trigger, track } from './effect'
import { effect, ReactiveEffect, trigger, track, cleanup } from './effect'
import { TriggerOpTypes, TrackOpTypes } from './operations'
import { Ref } from './ref'
import { isFunction, NOOP } from '@vue/shared'
import { ReactiveFlags, toRaw } from './reactive'

export interface ComputedRef<T = any> extends WritableComputedRef<T> {
readonly value: T
cleanup(): void
}

export interface WritableComputedRef<T> extends Ref<T> {
Expand Down Expand Up @@ -59,6 +60,12 @@ class ComputedRefImpl<T> {
set value(newValue: T) {
this._setter(newValue)
}

cleanup() {
cleanup(this.effect)
this._dirty = true
Copy link
Member

Choose a reason for hiding this comment

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

why is it necessary to set dirty here?

Copy link
Contributor Author

@basvanmeurs basvanmeurs Oct 19, 2020

Choose a reason for hiding this comment

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

By setting the dirty flag upon cleanup, the computed will stay fully functional. When getting the computed value later, all dependencies will simply be recovered and the correct value will be returned. This way, it is possible to temporarily cleanup deps which in some cases can become a performance bottleneck.

this._value = undefined!
}
}

export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function createReactiveEffect<T = any>(
return effect
}

function cleanup(effect: ReactiveEffect) {
export function cleanup(effect: ReactiveEffect) {
const { deps } = effect
if (deps.length) {
for (let i = 0; i < deps.length; i++) {
Expand Down