Skip to content

Commit

Permalink
refactor: use utils isFunction (#768)
Browse files Browse the repository at this point in the history
Co-authored-by: webfansplz <>
  • Loading branch information
webfansplz authored Jul 19, 2021
1 parent d467b8f commit 2ba5a66
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/apis/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
noopFn,
defineComponentInstance,
getVueInternalClasses,
isFunction,
} from '../utils'

export interface ComputedRef<T = any> extends WritableComputedRef<T> {
Expand Down Expand Up @@ -35,7 +36,7 @@ export function computed<T>(
let getter: ComputedGetter<T>
let setter: ComputedSetter<T> | undefined

if (typeof getterOrOptions === 'function') {
if (isFunction(getterOrOptions)) {
getter = getterOrOptions
} else {
getter = getterOrOptions.get
Expand Down
2 changes: 1 addition & 1 deletion src/apis/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export function watch<T = any>(
options?: WatchOptions
): WatchStopHandle {
let callback: WatchCallback<unknown> | null = null
if (typeof cb === 'function') {
if (isFunction(cb)) {
// source watch
callback = cb as WatchCallback<unknown>
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions src/runtimeContext.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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() {
Expand Down

0 comments on commit 2ba5a66

Please sign in to comment.