diff --git a/packages/core/src/componentComputed.ts b/packages/core/src/componentComputed.ts index ab88958b..04d9ccd1 100644 --- a/packages/core/src/componentComputed.ts +++ b/packages/core/src/componentComputed.ts @@ -1,5 +1,5 @@ import { EMPTY_OBJ } from './utils' -import { computed, ComputedGetter } from '@vue/observer' +import { computed, stop, ComputedGetter } from '@vue/observer' import { Component, ComponentClass } from './component' import { ComponentComputedOptions } from './componentOptions' @@ -59,7 +59,7 @@ export function teardownComputed(instance: Component) { const handles = instance._computedGetters if (handles !== null) { for (const key in handles) { - handles[key].stop() + stop(handles[key].runner) } } } diff --git a/packages/observer/__tests__/computed.spec.ts b/packages/observer/__tests__/computed.spec.ts index f969b65d..973e3c0f 100644 --- a/packages/observer/__tests__/computed.spec.ts +++ b/packages/observer/__tests__/computed.spec.ts @@ -1,4 +1,4 @@ -import { computed, observable, autorun } from '../src' +import { computed, observable, autorun, stop } from '../src' describe('observer/computed', () => { it('should return updated value', () => { @@ -132,7 +132,7 @@ describe('observer/computed', () => { expect(dummy).toBe(undefined) value.foo = 1 expect(dummy).toBe(1) - cValue.stop() + stop(cValue.runner) value.foo = 2 expect(dummy).toBe(1) }) diff --git a/packages/observer/src/computed.ts b/packages/observer/src/computed.ts index c4eb6e94..d63588d0 100644 --- a/packages/observer/src/computed.ts +++ b/packages/observer/src/computed.ts @@ -1,9 +1,9 @@ -import { autorun, stop } from './index' +import { autorun } from './index' import { Autorun, activeAutorunStack } from './autorun' export interface ComputedGetter { (): any - stop: () => void + runner: Autorun } export function computed(getter: Function, context?: any): ComputedGetter { @@ -15,8 +15,6 @@ export function computed(getter: Function, context?: any): ComputedGetter { dirty = true } }) - // mark runner as computed so that it gets priority during trigger - runner.computed = true const computedGetter = (() => { if (dirty) { value = runner() @@ -28,7 +26,10 @@ export function computed(getter: Function, context?: any): ComputedGetter { trackChildRun(runner) return value }) as ComputedGetter - computedGetter.stop = () => stop(runner) + // expose runner so computed can be stopped + computedGetter.runner = runner + // mark runner as computed so that it gets priority during trigger + runner.computed = true return computedGetter }