Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Promisify type definition #3420

Merged
merged 1 commit into from Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/shared/utils/filters.ts
@@ -1,7 +1,7 @@
import { readonly, ref } from 'vue-demi'
import { toValue } from '../toValue'
import { noop } from './is'
import type { AnyFn, ArgumentsType, MaybeRefOrGetter, Pausable } from './types'
import type { AnyFn, ArgumentsType, Awaited, MaybeRefOrGetter, Pausable, Promisify } from './types'

export type FunctionArgs<Args extends any[] = any[], Return = void> = (...args: Args) => Return

Expand All @@ -14,7 +14,7 @@ export interface FunctionWrapperOptions<Args extends any[] = any[], This = any>
export type EventFilter<Args extends any[] = any[], This = any, Invoke extends AnyFn = AnyFn> = (
invoke: Invoke,
options: FunctionWrapperOptions<Args, This>
) => ReturnType<Invoke> | Promise<ReturnType<Invoke>>
) => ReturnType<Invoke> | Promisify<ReturnType<Invoke>>

export interface ConfigurableEventFilter {
/**
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface DebounceFilterOptions {
*/
export function createFilterWrapper<T extends AnyFn>(filter: EventFilter, fn: T) {
function wrapper(this: any, ...args: ArgumentsType<T>) {
return new Promise<ReturnType<T>>((resolve, reject) => {
return new Promise<Awaited<ReturnType<T>>>((resolve, reject) => {
// make sure it's a promise
Promise.resolve(filter(() => fn.apply(this, args), { fn, thisArg: this, args }))
.then(resolve)
Expand Down
15 changes: 14 additions & 1 deletion packages/shared/utils/types.ts
Expand Up @@ -69,7 +69,20 @@ export type Awaitable<T> = Promise<T> | T

export type ArgumentsType<T> = T extends (...args: infer U) => any ? U : never

export type PromisifyFn<T extends AnyFn> = (...args: ArgumentsType<T>) => Promise<ReturnType<T>>
/**
* Compatible with versions below TypeScript 4.5 Awaited
*/
export type Awaited<T> =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A suppose Awaited is builtin in TypeScript isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awaited was introduced in TypeScript 4.5, here to consider compatibility.

T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F, ...args: infer _): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: infer _) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T // non-object or non-thenable

export type Promisify<T> = Promise<Awaited<T>>

export type PromisifyFn<T extends AnyFn> = (...args: ArgumentsType<T>) => Promisify<ReturnType<T>>

export interface Pausable {
/**
Expand Down