Skip to content

Commit

Permalink
feat(utils): createSubmit 支持泛型
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 8, 2023
1 parent 2cf905a commit e5f1e8a
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/utils/createSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ import { AsyncOrSync } from '../types'
import { run } from './run'
import { wait } from './wait'

export interface CreateSubmitOptions {
export interface CreateSubmitOptions<T> {
/**
* 开始回调。
*
* @param message 提示信息
*/
start(message?: string): AsyncOrSync<any>
start(message?: T): AsyncOrSync<any>

/**
* 失败回调。
*
* @param message 提示信息
* @param duration 持续时间(毫秒)
*/
fail(message: string, duration: number): AsyncOrSync<any>
fail(message: T, duration: number): AsyncOrSync<any>

/**
* 成功回调。
*
* @param message 提示信息
* @param duration 持续时间(毫秒)
*/
success(message: string, duration: number): AsyncOrSync<any>
success(message: T, duration: number): AsyncOrSync<any>

/**
* 完成回调。
Expand All @@ -37,42 +37,60 @@ export interface CreateSubmitOptions {
throw?(error: unknown): AsyncOrSync<any>
}

export interface SubmitActionPayload {
export interface SubmitActionPayload<T> {
/**
* 开始提示。
*
* @param message 提示信息
*/
start(message?: string): Promise<any>
start(message?: T): Promise<any>

/**
* 失败提示。
*
* @param message 提示信息
* @param duration 持续时间(毫秒),默认 1500
*/
fail(message: string, duration?: number): Promise<any>
fail(message: T, duration?: number): Promise<any>

/**
* 成功提示。
*
* @param message 提示信息
* @param duration 持续时间(毫秒),默认 1500
*/
success(message: string, duration?: number): Promise<any>
success(message: T, duration?: number): Promise<any>
}

export type CreateSubmitResult = <TResult>(
action: (payload: SubmitActionPayload) => Promise<TResult>,
export type CreateSubmitResult<T> = <TResult>(
action: (payload: SubmitActionPayload<T>) => Promise<TResult>,
) => Promise<TResult>

/**
* 创建提交类行为。
*
* @param options 选项
*/
export function createSubmit(options: CreateSubmitOptions): CreateSubmitResult {
const payload: SubmitActionPayload = {
export function createSubmit(
options: CreateSubmitOptions<string>,
): CreateSubmitResult<string>
/**
* 创建提交类行为。
*
* @param options 选项
*/
export function createSubmit<T>(
options: CreateSubmitOptions<T>,
): CreateSubmitResult<T>
/**
* 创建提交类行为。
*
* @param options 选项
*/
export function createSubmit<T>(
options: CreateSubmitOptions<T>,
): CreateSubmitResult<T> {
const payload: SubmitActionPayload<T> = {
start(message) {
return run(() => options.start(message))
},
Expand Down

0 comments on commit e5f1e8a

Please sign in to comment.