Unverified Commit cf590739 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

refactor: collect all isThenable/isPromise definitions (#70293)

This collects the few different isThenable and isPromise definitions
into one shared function
parent cd333000
...@@ -272,15 +272,3 @@ export type ReducerActions = Readonly< ...@@ -272,15 +272,3 @@ export type ReducerActions = Readonly<
| HmrRefreshAction | HmrRefreshAction
| ServerActionAction | ServerActionAction
> >
export function isThenable(value: any): value is Promise<AppRouterState> {
// TODO: We don't gain anything from this abstraction. It's unsound, and only
// makes sense in the specific places where we use it. So it's better to keep
// the type coercion inline, instead of leaking this to other places in
// the codebase.
return (
value &&
(typeof value === 'object' || typeof value === 'function') &&
typeof value.then === 'function'
)
}
import type { Dispatch } from 'react' import type { Dispatch } from 'react'
import React, { use } from 'react' import React, { use } from 'react'
import { useRef, useEffect, useCallback } from 'react' import { useRef, useEffect, useCallback } from 'react'
import { import type {
isThenable, AppRouterState,
type AppRouterState, ReducerActions,
type ReducerActions, ReducerState,
type ReducerState,
} from './router-reducer/router-reducer-types' } from './router-reducer/router-reducer-types'
import type { AppRouterActionQueue } from '../../shared/lib/router/action-queue' import type { AppRouterActionQueue } from '../../shared/lib/router/action-queue'
import { isThenable } from '../../shared/lib/is-thenable'
export type ReduxDevtoolsSyncFn = (state: AppRouterState) => void export type ReduxDevtoolsSyncFn = (state: AppRouterState) => void
......
...@@ -7,6 +7,7 @@ import { ResponseCookies } from '../web/spec-extension/cookies' ...@@ -7,6 +7,7 @@ import { ResponseCookies } from '../web/spec-extension/cookies'
import type { RequestLifecycleOpts } from '../base-server' import type { RequestLifecycleOpts } from '../base-server'
import type { AfterCallback, AfterTask } from './after' import type { AfterCallback, AfterTask } from './after'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { isThenable } from '../../shared/lib/is-thenable'
export type AfterContextOpts = { export type AfterContextOpts = {
waitUntil: RequestLifecycleOpts['waitUntil'] | undefined waitUntil: RequestLifecycleOpts['waitUntil'] | undefined
...@@ -36,7 +37,7 @@ export class AfterContext { ...@@ -36,7 +37,7 @@ export class AfterContext {
} }
public after(task: AfterTask): void { public after(task: AfterTask): void {
if (isPromise(task)) { if (isThenable(task)) {
task.catch(() => {}) // avoid unhandled rejection crashes task.catch(() => {}) // avoid unhandled rejection crashes
if (!this.waitUntil) { if (!this.waitUntil) {
errorWaitUntilNotAvailable() errorWaitUntilNotAvailable()
...@@ -141,12 +142,3 @@ function wrapRequestStoreForAfterCallbacks( ...@@ -141,12 +142,3 @@ function wrapRequestStoreForAfterCallbacks(
serverComponentsHmrCache: requestStore.serverComponentsHmrCache, serverComponentsHmrCache: requestStore.serverComponentsHmrCache,
} }
} }
function isPromise(p: unknown): p is Promise<unknown> {
return (
p !== null &&
typeof p === 'object' &&
'then' in p &&
typeof p.then === 'function'
)
}
...@@ -11,6 +11,7 @@ import type { ...@@ -11,6 +11,7 @@ import type {
AttributeValue, AttributeValue,
TextMapGetter, TextMapGetter,
} from 'next/dist/compiled/@opentelemetry/api' } from 'next/dist/compiled/@opentelemetry/api'
import { isThenable } from '../../../shared/lib/is-thenable'
let api: typeof import('next/dist/compiled/@opentelemetry/api') let api: typeof import('next/dist/compiled/@opentelemetry/api')
...@@ -34,10 +35,6 @@ if (process.env.NEXT_RUNTIME === 'edge') { ...@@ -34,10 +35,6 @@ if (process.env.NEXT_RUNTIME === 'edge') {
const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } =
api api
const isPromise = <T>(p: any): p is Promise<T> => {
return p !== null && typeof p === 'object' && typeof p.then === 'function'
}
export class BubbledError extends Error { export class BubbledError extends Error {
constructor( constructor(
public readonly bubble?: boolean, public readonly bubble?: boolean,
...@@ -352,7 +349,7 @@ class NextTracerImpl implements NextTracer { ...@@ -352,7 +349,7 @@ class NextTracerImpl implements NextTracer {
} }
const result = fn(span) const result = fn(span)
if (isPromise(result)) { if (isThenable(result)) {
// If there's error make sure it throws // If there's error make sure it throws
return result return result
.then((res) => { .then((res) => {
......
/**
* Check to see if a value is Thenable.
*
* @param promise the maybe-thenable value
* @returns true if the value is thenable
*/
export function isThenable<T = unknown>(
promise: Promise<T> | T
): promise is Promise<T> {
return (
promise !== null &&
typeof promise === 'object' &&
'then' in promise &&
typeof promise.then === 'function'
)
}
import { import {
isThenable,
type AppRouterState, type AppRouterState,
type ReducerActions, type ReducerActions,
type ReducerState, type ReducerState,
...@@ -11,6 +10,7 @@ import { ...@@ -11,6 +10,7 @@ import {
import type { ReduxDevToolsInstance } from '../../../client/components/use-reducer-with-devtools' import type { ReduxDevToolsInstance } from '../../../client/components/use-reducer-with-devtools'
import { reducer } from '../../../client/components/router-reducer/router-reducer' import { reducer } from '../../../client/components/router-reducer/router-reducer'
import { startTransition } from 'react' import { startTransition } from 'react'
import { isThenable } from '../is-thenable'
export type DispatchStatePromise = React.Dispatch<ReducerState> export type DispatchStatePromise = React.Dispatch<ReducerState>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment