refactor(reactivity): prefer to use effect symbol and add isEffect() (#169)
This commit is contained in:
parent
0478ca4cdb
commit
dadfb27eed
@ -2,9 +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 const effectSymbol = Symbol(__DEV__ ? 'effect' : void 0)
|
||||||
|
|
||||||
export interface ReactiveEffect<T = any> {
|
export interface ReactiveEffect<T = any> {
|
||||||
(): T
|
(): T
|
||||||
isEffect: true
|
[effectSymbol]: true
|
||||||
active: boolean
|
active: boolean
|
||||||
raw: () => T
|
raw: () => T
|
||||||
deps: Array<Dep>
|
deps: Array<Dep>
|
||||||
@ -35,12 +37,16 @@ export const activeReactiveEffectStack: ReactiveEffect[] = []
|
|||||||
|
|
||||||
export const ITERATE_KEY = Symbol('iterate')
|
export const ITERATE_KEY = Symbol('iterate')
|
||||||
|
|
||||||
|
export function isEffect(fn: any): fn is ReactiveEffect {
|
||||||
|
return fn != null && fn[effectSymbol] === true
|
||||||
|
}
|
||||||
|
|
||||||
export function effect<T = any>(
|
export function effect<T = any>(
|
||||||
fn: () => T,
|
fn: () => T,
|
||||||
options: ReactiveEffectOptions = EMPTY_OBJ
|
options: ReactiveEffectOptions = EMPTY_OBJ
|
||||||
): ReactiveEffect<T> {
|
): ReactiveEffect<T> {
|
||||||
if ((fn as ReactiveEffect).isEffect) {
|
if (isEffect(fn)) {
|
||||||
fn = (fn as ReactiveEffect).raw
|
fn = fn.raw
|
||||||
}
|
}
|
||||||
const effect = createReactiveEffect(fn, options)
|
const effect = createReactiveEffect(fn, options)
|
||||||
if (!options.lazy) {
|
if (!options.lazy) {
|
||||||
@ -63,10 +69,10 @@ function createReactiveEffect<T = any>(
|
|||||||
fn: () => T,
|
fn: () => T,
|
||||||
options: ReactiveEffectOptions
|
options: ReactiveEffectOptions
|
||||||
): ReactiveEffect<T> {
|
): ReactiveEffect<T> {
|
||||||
const effect: ReactiveEffect = function effect(...args: any[]): any {
|
const effect = function reactiveEffect(...args: any[]): any {
|
||||||
return run(effect as ReactiveEffect, fn, args)
|
return run(effect, fn, args)
|
||||||
}
|
} as ReactiveEffect
|
||||||
effect.isEffect = true
|
effect[effectSymbol] = true
|
||||||
effect.active = true
|
effect.active = true
|
||||||
effect.raw = fn
|
effect.raw = fn
|
||||||
effect.scheduler = options.scheduler
|
effect.scheduler = options.scheduler
|
||||||
|
Loading…
Reference in New Issue
Block a user