Przeglądaj źródła

refactor(runtime-core): extract promise check into shared (#325)

Dmitry Sharshakov 6 lat temu
rodzic
commit
7305f693b1

+ 3 - 6
packages/runtime-core/src/component.ts

@@ -23,7 +23,8 @@ import {
   isArray,
   isArray,
   isObject,
   isObject,
   NO,
   NO,
-  makeMap
+  makeMap,
+  isPromise
 } from '@vue/shared'
 } from '@vue/shared'
 import { SuspenseBoundary } from './suspense'
 import { SuspenseBoundary } from './suspense'
 import {
 import {
@@ -281,11 +282,7 @@ export function setupStatefulComponent(
     currentInstance = null
     currentInstance = null
     currentSuspense = null
     currentSuspense = null
 
 
-    if (
-      setupResult &&
-      isFunction(setupResult.then) &&
-      isFunction(setupResult.catch)
-    ) {
+    if (isPromise(setupResult)) {
       if (__FEATURE_SUSPENSE__) {
       if (__FEATURE_SUSPENSE__) {
         // async setup returned Promise.
         // async setup returned Promise.
         // bail here and wait for re-entry.
         // bail here and wait for re-entry.

+ 3 - 2
packages/runtime-core/src/errorHandling.ts

@@ -1,6 +1,7 @@
 import { VNode } from './vnode'
 import { VNode } from './vnode'
 import { ComponentInternalInstance, LifecycleHooks } from './component'
 import { ComponentInternalInstance, LifecycleHooks } from './component'
 import { warn, pushWarningContext, popWarningContext } from './warning'
 import { warn, pushWarningContext, popWarningContext } from './warning'
+import { isPromise } from '@vue/shared'
 
 
 // contexts where user provided function may be executed, in addition to
 // contexts where user provided function may be executed, in addition to
 // lifecycle hooks.
 // lifecycle hooks.
@@ -71,8 +72,8 @@ export function callWithAsyncErrorHandling(
   args?: any[]
   args?: any[]
 ) {
 ) {
   const res = callWithErrorHandling(fn, instance, type, args)
   const res = callWithErrorHandling(fn, instance, type, args)
-  if (res != null && !res._isVue && typeof res.then === 'function') {
-    res.catch((err: any) => {
+  if (res != null && !res._isVue && isPromise(res)) {
+    res.catch((err: Error) => {
       handleError(err, instance, type)
       handleError(err, instance, type)
     })
     })
   }
   }

+ 4 - 0
packages/shared/src/index.ts

@@ -41,6 +41,10 @@ export const isSymbol = (val: any): val is symbol => typeof val === 'symbol'
 export const isObject = (val: any): val is Record<any, any> =>
 export const isObject = (val: any): val is Record<any, any> =>
   val !== null && typeof val === 'object'
   val !== null && typeof val === 'object'
 
 
+export function isPromise<T = any>(val: any): val is Promise<T> {
+  return isObject(val) && isFunction(val.then) && isFunction(val.catch)
+}
+
 export const objectToString = Object.prototype.toString
 export const objectToString = Object.prototype.toString
 export const toTypeString = (value: unknown): string =>
 export const toTypeString = (value: unknown): string =>
   objectToString.call(value)
   objectToString.call(value)