From 6f9e9c975afa38c3e0c09b6ad20c488f148ba643 Mon Sep 17 00:00:00 2001 From: webfansplz <> Date: Mon, 19 Jul 2021 18:41:28 +0800 Subject: [PATCH] refactor: use utils isFunction --- src/apis/computed.ts | 3 ++- src/apis/watch.ts | 2 +- src/install.ts | 6 +++--- src/mixin.ts | 4 ++-- src/runtimeContext.ts | 11 +++++++++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/apis/computed.ts b/src/apis/computed.ts index 9ac0b28b..c1b65ece 100644 --- a/src/apis/computed.ts +++ b/src/apis/computed.ts @@ -5,6 +5,7 @@ import { noopFn, defineComponentInstance, getVueInternalClasses, + isFunction, } from '../utils' export interface ComputedRef extends WritableComputedRef { @@ -35,7 +36,7 @@ export function computed( let getter: ComputedGetter let setter: ComputedSetter | undefined - if (typeof getterOrOptions === 'function') { + if (isFunction(getterOrOptions)) { getter = getterOrOptions } else { getter = getterOrOptions.get diff --git a/src/apis/watch.ts b/src/apis/watch.ts index 8f8ce5d6..503c8c72 100644 --- a/src/apis/watch.ts +++ b/src/apis/watch.ts @@ -432,7 +432,7 @@ export function watch( options?: WatchOptions ): WatchStopHandle { let callback: WatchCallback | null = null - if (typeof cb === 'function') { + if (isFunction(cb)) { // source watch callback = cb as WatchCallback } else { diff --git a/src/install.ts b/src/install.ts index 09cfe42e..fd8a56e6 100644 --- a/src/install.ts +++ b/src/install.ts @@ -1,6 +1,6 @@ import type { VueConstructor } from 'vue' import { AnyObject } from './types/basic' -import { hasSymbol, hasOwn, isPlainObject, warn } from './utils' +import { isFunction, hasSymbol, hasOwn, isPlainObject, warn } from './utils' import { isRef } from './reactivity' import { setVueConstructor, isVueRegistered } from './runtimeContext' import { mixin } from './mixin' @@ -67,8 +67,8 @@ export function install(Vue: VueConstructor) { ) { return function mergedSetupFn(props: any, context: any) { return mergeData( - typeof parent === 'function' ? parent(props, context) || {} : undefined, - typeof child === 'function' ? child(props, context) || {} : undefined + isFunction(parent) ? parent(props, context) || {} : undefined, + isFunction(child) ? child(props, context) || {} : undefined ) } } diff --git a/src/mixin.ts b/src/mixin.ts index b0c3ae52..cebf5f82 100644 --- a/src/mixin.ts +++ b/src/mixin.ts @@ -60,7 +60,7 @@ export function mixin(Vue: VueConstructor) { if (!setup) { return } - if (typeof setup !== 'function') { + if (!isFunction(setup)) { if (__DEV__) { warn( 'The "setup" option should be a function that returns a object in component definitions.', @@ -74,7 +74,7 @@ export function mixin(Vue: VueConstructor) { // wrapper the data option, so we can invoke setup before data get resolved $options.data = function wrappedData() { initSetup(vm, vm.$props) - return typeof data === 'function' + return isFunction(data) ? ( data as (this: ComponentInstance, x: ComponentInstance) => object ).call(vm, vm) diff --git a/src/runtimeContext.ts b/src/runtimeContext.ts index bfdd68da..f64b2640 100644 --- a/src/runtimeContext.ts +++ b/src/runtimeContext.ts @@ -1,6 +1,13 @@ import type { VueConstructor, VNode } from 'vue' import { ComponentInstance, Data } from './component' -import { assert, hasOwn, warn, proxy, UnionToIntersection } from './utils' +import { + assert, + hasOwn, + warn, + proxy, + UnionToIntersection, + isFunction, +} from './utils' let vueDependency: VueConstructor | undefined = undefined @@ -25,7 +32,7 @@ let currentInstance: ComponentInstance | null = null const PluginInstalledFlag = '__composition_api_installed__' function isVue(obj: any): obj is VueConstructor { - return obj && typeof obj === 'function' && obj.name === 'Vue' + return obj && isFunction(obj) && obj.name === 'Vue' } export function isPluginInstalled() {