All files / utils createSubmit.ts

100% Statements 24/24
75% Branches 3/4
100% Functions 15/15
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                                                                                                                                          23x                                                   8x   3x     4x 4x       4x 4x       4x 4x 4x   3x     1x 1x   1x     4x 4x 4x 4x    
import { AsyncOrSync } from '../types'
import { run } from './run'
import { wait } from './wait'
 
export interface CreateSubmitOptions<T = string> {
  /**
   * 开始回调。
   *
   * @param message 提示信息
   */
  start(message: T | undefined, id: number): AsyncOrSync<any>
 
  /**
   * 失败回调。
   *
   * @param message 提示信息
   * @param duration 持续时间(毫秒)
   */
  fail(message: T, duration: number, id: number): AsyncOrSync<any>
 
  /**
   * 成功回调。
   *
   * @param message 提示信息
   * @param duration 持续时间(毫秒)
   */
  success(message: T, duration: number, id: number): AsyncOrSync<any>
 
  /**
   * 完成回调。
   */
  complete(id: number): AsyncOrSync<any>
 
  /**
   * 异常回调。
   */
  throw?(error: unknown, id: number): AsyncOrSync<any>
}
 
export interface SubmitActionPayload<T = string> {
  /**
   * 开始提示。
   *
   * @param message 提示信息
   */
  start(message?: T): Promise<any>
 
  /**
   * 失败提示。
   *
   * @param message 提示信息
   * @param duration 持续时间(毫秒),默认 1500
   */
  fail(message: T, duration?: number): Promise<any>
 
  /**
   * 成功提示。
   *
   * @param message 提示信息
   * @param duration 持续时间(毫秒),默认 1500
   */
  success(message: T, duration?: number): Promise<any>
}
 
export type CreateSubmitResult<T = string> = (<TResult>(
  action: (payload: SubmitActionPayload<T>) => Promise<TResult>,
) => Promise<TResult>) &
  Pick<SubmitActionPayload<T>, 'fail' | 'success'>
 
let idCounter = 1
 
/**
 * 创建提交类行为。
 *
 * @param options 选项
 */
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 getPayload = (id: number): SubmitActionPayload<T> => ({
    start(message) {
      return run(() => options.start(message, id))
    },
    fail(message, duration = 1500) {
      return run(() => options.fail(message, duration, id)).then(() =>
        wait(duration),
      )
    },
    success(message, duration = 1500) {
      return run(() => options.success(message, duration, id)).then(() =>
        wait(duration),
      )
    },
  })
  const res: CreateSubmitResult<T> = action => {
    const id = idCounter++
    return action(getPayload(id))
      .then(res => {
        return run(() => options.complete(id)).then(() => res)
      })
      .catch((error: unknown) => {
        if (options.throw) {
          options.throw(error, id)
        }
        return Promise.reject(error)
      })
  }
  const globalAction = getPayload(0)
  res.success = globalAction.success
  res.fail = globalAction.fail
  return res
}