refactor: expose runner on computed

This commit is contained in:
Evan You 2018-09-20 18:36:34 -04:00
parent fa9b3df5ab
commit 9b50a5abb9
3 changed files with 10 additions and 9 deletions

View File

@ -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)
}
}
}

View File

@ -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)
})

View File

@ -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
}