types: improve type 'ReactiveEffect' with generics (#99)
This commit is contained in:
parent
8da5b007b1
commit
80e5b16f99
@ -2,11 +2,11 @@ import { OperationTypes } from './operations'
|
|||||||
import { Dep, targetMap } from './reactive'
|
import { Dep, targetMap } from './reactive'
|
||||||
import { EMPTY_OBJ, extend } from '@vue/shared'
|
import { EMPTY_OBJ, extend } from '@vue/shared'
|
||||||
|
|
||||||
export interface ReactiveEffect {
|
export interface ReactiveEffect<T = any> {
|
||||||
(): any
|
(): T
|
||||||
isEffect: true
|
isEffect: true
|
||||||
active: boolean
|
active: boolean
|
||||||
raw: Function
|
raw: () => T
|
||||||
deps: Array<Dep>
|
deps: Array<Dep>
|
||||||
computed?: boolean
|
computed?: boolean
|
||||||
scheduler?: (run: Function) => void
|
scheduler?: (run: Function) => void
|
||||||
@ -35,10 +35,10 @@ export const activeReactiveEffectStack: ReactiveEffect[] = []
|
|||||||
|
|
||||||
export const ITERATE_KEY = Symbol('iterate')
|
export const ITERATE_KEY = Symbol('iterate')
|
||||||
|
|
||||||
export function effect(
|
export function effect<T = any>(
|
||||||
fn: Function,
|
fn: () => T,
|
||||||
options: ReactiveEffectOptions = EMPTY_OBJ
|
options: ReactiveEffectOptions = EMPTY_OBJ
|
||||||
): ReactiveEffect {
|
): ReactiveEffect<T> {
|
||||||
if ((fn as ReactiveEffect).isEffect) {
|
if ((fn as ReactiveEffect).isEffect) {
|
||||||
fn = (fn as ReactiveEffect).raw
|
fn = (fn as ReactiveEffect).raw
|
||||||
}
|
}
|
||||||
@ -59,13 +59,13 @@ export function stop(effect: ReactiveEffect) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createReactiveEffect(
|
function createReactiveEffect<T = any>(
|
||||||
fn: Function,
|
fn: () => T,
|
||||||
options: ReactiveEffectOptions
|
options: ReactiveEffectOptions
|
||||||
): ReactiveEffect {
|
): ReactiveEffect<T> {
|
||||||
const effect = function effect(...args): any {
|
const effect: ReactiveEffect = function effect(...args: any[]): any {
|
||||||
return run(effect as ReactiveEffect, fn, args)
|
return run(effect as ReactiveEffect, fn, args)
|
||||||
} as ReactiveEffect
|
}
|
||||||
effect.isEffect = true
|
effect.isEffect = true
|
||||||
effect.active = true
|
effect.active = true
|
||||||
effect.raw = fn
|
effect.raw = fn
|
||||||
|
@ -91,7 +91,7 @@ function doWatch(
|
|||||||
const instance = currentInstance
|
const instance = currentInstance
|
||||||
const suspense = currentSuspense
|
const suspense = currentSuspense
|
||||||
|
|
||||||
let getter: Function
|
let getter: () => any
|
||||||
if (isArray(source)) {
|
if (isArray(source)) {
|
||||||
getter = () =>
|
getter = () =>
|
||||||
source.map(
|
source.map(
|
||||||
|
Loading…
Reference in New Issue
Block a user