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